| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | my %options = %{pop @_} if ( ref($_[-1]) =~ /HASH/ ); # grab options, if specified | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | return 1 unless ( @_ ); # return true if 0 code blocks passed | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 42 | 0 | my $child = fork(); | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | unless ( defined $child ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | $error = "$!"; | |||||||||||||||||||||||||||
| 45 | 0 | last; # something's wrong; stop trying to fork | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ( $child == 0 ) { # child | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my ( $subref, @args ) = ref($block) =~ /ARRAY/ ? @$block : ( $block ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | my $return_value = eval { $subref->( @args ) }; | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | warn( $@ ) if ( $@ ); # print death message, because eval doesn't | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | exit( $@ ? 255 : $options{use_return} ? $return_value : 0 ); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 60 | 0 | $child = waitpid( -1, 0 ); | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ( $child > 0 and exists $child_registry{$child} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$child > 0 and exists $child_registry{$child}
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}
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | $child_registry{$child}{return_value} = $? unless ( defined $child_registry{$child}{return_value} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $successes++ if ( $? == 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if ( $? > 0 and $options{abort_on_error} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$? > 0 and $options{"abort_on_error"}
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"}
| |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | while ( my ( $pid, $child ) = each %child_registry ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | unless ( defined $child->{return_value} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $num_blocks = keys %blocks; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | return 1 if ( $successes == $num_blocks ); # all good! | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||