examples/callback_data.pl

filter: Statementstmt 100% 25 / 25 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 32 / 32 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« examples/callback.pl ↑ Summary
1 #!/usr/bin/perl
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use Parallel::ForkManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 my $max_procs = 2;
8 6 my @names = qw( Fred Jim );
9  
10 6 my $pm = Parallel::ForkManager->new($max_procs, @ARGV);
11  
12 # Setup a callback for when a child finishes up so we can
13 # get it's exit code and any data it collected
14 $pm->run_on_finish( sub {
15 4 ▶ my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
16 4 print "$ident just got out of the pool ".
17 "with exit code: $exit_code and data: @$data_structure_reference\n";
18 6 });
19  
20 $pm->run_on_start( sub {
21 6 ▶ my ($pid,$ident)=@_;
Subroutine __ANON__: called CC=1 SCAR=0.0
22 6 print "$ident started\n";
23 6 });
24  
25 6 foreach my $child ( 0 .. $#names ) {
26 10 ▶ my $pid = $pm->start($names[$child]) and next;
Branch: 1 branch
BranchTrueFalseTotal
my $pid = $pm->start($names[$child]) and next 6 4 10
27  
28 # This code is the child process
29 # We can do here anything and obtain any data.
30 # The result can be any array or hash.
31 4 my @result = ($names[$child], length $names[$child]);
32 4 sleep 1+rand(3);
33  
34 # pass an exit code and data stucture to finish
35 4 $pm->finish($child, \@result );
36 }
37  
38 #print "Waiting for Children...\n";
39 2 $pm->wait_all_children;
40 2 print "Everybody is out of the pool!\n";
41  
42  
« examples/callback.pl ↑ Summary