blib/lib/Parallel/Simple.pm

filter: Statementstmt 27.2% 18 / 66 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 4 / 8 Podpod 100% 4 / 4 totaltotal 19.4% 26 / 134 CC 26 SCAR 60.8
CC
26
Cov
15%
prun
600.0
errplus
12.0
err
2.0
CRAP 437.7
↑ Summary
1 package Parallel::Simple;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 $VERSION = '0.01';
9  
10 2 @ISA = qw( Exporter );
11 2 @EXPORT = qw();
12 2 @EXPORT_OK = qw( prun );
13  
14 2 my $error;
15 my $return_values;
16  
17 0 ▶sub err { $error }
Subroutine err: not called CC=1 SCAR=6.9
Pod documented
18 0 ▶sub rv { $return_values }
Subroutine rv: not called CC=1 SCAR=6.9
Pod documented
19  
20 0 ▶sub errplus {
Subroutine errplus: not called CC=3 SCAR=24.8
Pod documented
21 0 ▶ return unless ( defined $error );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error 0 0 0
22 "$error\n" . ( ref($return_values) =~ /HASH/ ?
23 0 join( '', map { "\t$_ => $return_values->{$_}\n" } sort keys %$return_values ) :
24 0 ▶ join( '', map { "\t$_ => $return_values->[$_]\n" } 0..$#$return_values )
Branch: 1 branch
BranchTrueFalseTotal
ref($return_values) =~ /HASH/ ? : 0 0 0
25 );
26 }
27  
28 0 ▶sub prun {
Subroutine prun: not called CC=24 SCAR=64.0
Pod documented
29 0 ( $error, $return_values ) = ( undef, undef ); # reset globals
30 0 ▶ return 1 unless ( @_ ); # return true if 0 args passed
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
31 0 ▶ my %options = %{pop @_} if ( ref($_[-1]) =~ /HASH/ ); # grab options, if specified
Branch: 1 branch
BranchTrueFalseTotal
if ref($_[-1]) =~ /HASH/ 0 0 0
32 0 ▶ return 1 unless ( @_ ); # return true if 0 code blocks passed
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
33  
34 # normalize named and unnamed blocks into similar structure to simplify main loop
35 0 ▶ my $named = ref($_[0]) ? 0 : 1; # if first element is a subref, they're not named
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
36 0 my $i = 0; # used to turn array into hash with array-like keys
37 0 ▶ my %blocks = $named ? @_ : map { $i++ => $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
$named ? : 0 0 0
38  
39 # fork children
40 0 my %child_registry; # pid => { name => $name, return_value => $return_value }
41 0 ▶ while ( my ( $name, $block ) = each %blocks ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %blocks) 0 0 0
42 0 my $child = fork();
43 0 ▶ unless ( defined $child ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $child) 0 0 0
44 0 $error = "$!";
45 0 last; # something's wrong; stop trying to fork
46 }
47 0 ▶ if ( $child == 0 ) { # child
Branch: 1 branch
BranchTrueFalseTotal
if ($child == 0) 0 0 0
48 0 ▶ my ( $subref, @args ) = ref($block) =~ /ARRAY/ ? @$block : ( $block );
Branch: 1 branch
BranchTrueFalseTotal
ref($block) =~ /ARRAY/ ? : 0 0 0
49 0 my $return_value = eval { $subref->( @args ) };
50 0 ▶ warn( $@ ) if ( $@ ); # print death message, because eval doesn't
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
51 0 ▶ exit( $@ ? 255 : $options{use_return} ? $return_value : 0 );
Branch: 2 branches
BranchTrueFalseTotal
$@ ? : 0 0 0
$options{'use_return'} ? : 0 0 0
52 }
53 0 $child_registry{$child} = { name => $name, return_value => undef };
54 }
55  
56 # wait for children to finish
57 0 my $successes = 0;
58 0 my $child;
59 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$child > 0 and do { do { $child = waitpid(-1, 0); if ($child > 0 and exists $child_registry{$child}) { $child_registry{$child}{"return_value"} = $? unless defined $child_registry{$child}{"return_value"}; ++$successes if $? == 0; if ($? > 0 and $options{"abort_on_error"}) { while (() = each %child_registry) { do { unless (defined $child->{"return_value"}) { kill 9, $pid; $child->{"return_value"} = -1; } }; } ; } ; } } } 0 0 0
60 0 $child = waitpid( -1, 0 );
61 0 ▶ if ( $child > 0 and exists $child_registry{$child} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child > 0 and exists $child_registry{$child}) 0 0 0
Condition0%0/3
$child > 0 and exists $child_registry{$child}
!ll&&!rl&&r
000
MC/DC0%0/2
$child > 0 and exists $child_registry{$child}
$child > 0 exists $child_registry{$child}
Truth table0%0/3
A and B
A= $child > 0B= exists $child_registry{$child}
ABresult
0X0
100
111
62 0 ▶ $child_registry{$child}{return_value} = $? unless ( defined $child_registry{$child}{return_value} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child_registry{$child}{"return_value"} 0 0 0
63 0 ▶ $successes++ if ( $? == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $? == 0 0 0 0
64 0 ▶ if ( $? > 0 and $options{abort_on_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? > 0 and $options{"abort_on_error"}) 0 0 0
Condition0%0/3
$? > 0 and $options{"abort_on_error"}
!ll&&!rl&&r
000
MC/DC0%0/2
$? > 0 and $options{"abort_on_error"}
$? > 0 $options{"abort_on_error"}
Truth table0%0/3
A and B
A= $? > 0B= $options{"abort_on_error"}
ABresult
0X0
100
111
65 0 ▶ while ( my ( $pid, $child ) = each %child_registry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %child_registry) 0 0 0
66 0 ▶ unless ( defined $child->{return_value} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $child->{"return_value"}) 0 0 0
67 0 kill( 9, $pid );
68 0 $child->{return_value} = -1;
69 }
70 }
71 }
72 }
73 } while ( $child > 0 );
74  
75 # store return values using appropriate data type
76 $return_values = $named
77 0 ? { map { $_->{name} => $_->{return_value} } values %child_registry }
78 0 ▶ : [ map { $_->{return_value} } sort { $a->{name} <=> $b->{name} } values %child_registry ];
Branch: 1 branch
BranchTrueFalseTotal
$named ? : 0 0 0
79  
80 0 my $num_blocks = keys %blocks;
81 0 ▶ return 1 if ( $successes == $num_blocks ); # all good!
Branch: 1 branch
BranchTrueFalseTotal
if $successes == $num_blocks 0 0 0
82  
83 0 $error = "only $successes of $num_blocks blocks completed successfully";
84 0 return 0; # sorry... better luck next time
85 }
86  
87 2 1;
88  
89 __END__
↑ Summary