File Coverage

blib/lib/Proc/Fork.pm
Criterion Covered Total %
statement 51 51 100.0
branch 33 50 66.0
condition 6 9 66.6
subroutine 11 11 100.0
pod 5 5 100.0
total 106 126 84.1


line stmt bran cond sub pod time code
1 3     3   1390 use 5.006; use strict; use warnings;
  3     3   18  
  3     3   14  
  3         5  
  3         45  
  3         12  
  3         3  
  3         201  
2              
3             package Proc::Fork;
4             our $VERSION = '0.807';
5              
6             use Exporter::Tidy (
7 3         15 default => [ ':all' ],
8             wrapper => [ 'run_fork' ],
9             blocks => [ qw( parent child error retry ) ],
10 3     3   2684 );
  3         38  
11              
12 1     1   5 sub _croak { require Carp; goto &Carp::croak }
  1         150  
13              
14             my $do_clear = 1;
15             my ( $parent, $child, $error, $retry );
16              
17             sub run_fork(&) {
18 8     8 1 12 my $setup = shift;
19              
20 8         122 my @r = $setup->();
21 8 50       18 _croak "Garbage in Proc::Fork setup (semicolon after last block clause?)" if @r;
22 8         11 $do_clear = 1;
23              
24 8         11 my $pid;
25             my $i;
26              
27             {
28 8         43 $pid = fork;
  12         2179  
29 12 100       198 last if defined $pid;
30 7 100 100     18 redo if $retry and $retry->( ++$i );
31 3 50       20 die "Cannot fork: $!\n" if not $error;
32 3         7 $error->();
33 3         54 return;
34             }
35              
36 5 100 33     178 $_->( $pid || () ) for ( $pid ? $parent : $child ) || ();
      66        
37              
38 5         122772 return;
39             }
40              
41             for my $block ( qw( parent child error retry ) ) {
42             my $code = q{sub _BLOCK_ (&;@) {
43             $parent = $child = $error = $retry = $do_clear = undef if $do_clear;
44             _croak "Duplicate _BLOCK_ clause in Proc::Fork setup" if $_BLOCK_;
45             $_BLOCK_ = shift if 'CODE' eq ref $_[0];
46             _croak "Garbage in Proc::Fork setup (after _BLOCK_ clause)" if @_;
47             run_fork {} if not defined wantarray; # backcompat
48             ();
49             }};
50             $code =~ s/_BLOCK_/$block/g;
51 3 100   2 1 28 eval $code;
  3 50   3 1 13  
  3 50   3 1 9  
  3 50   5 1 10  
  3 50   2   21  
  3 100       3  
  3 50       117  
  3 50       18  
  3 50       22  
  3 100       6  
  3 50       11  
  3 50       48  
  5 50       156  
  5 100       15  
  5 100       13  
  5 50       16  
  4 50       15  
  4 50       39  
  2 50       7  
  2 50       4  
  2         15  
  2         5  
  2         11  
  2         3  
52             }
53              
54             1;
55              
56             __END__