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   1455 use 5.006; use strict; use warnings;
  3     3   19  
  3     3   12  
  3         5  
  3         49  
  3         10  
  3         3  
  3         195  
2              
3             package Proc::Fork;
4             our $VERSION = '0.808';
5              
6             use Exporter::Tidy (
7 3         16 default => [ ':all' ],
8             wrapper => [ 'run_fork' ],
9             blocks => [ qw( parent child error retry ) ],
10 3     3   2693 );
  3         35  
11              
12 1     1   5 sub _croak { require Carp; goto &Carp::croak }
  1         128  
13              
14             my $do_clear = 1;
15             my ( $parent, $child, $error, $retry );
16              
17             sub run_fork(&) {
18 8     8 1 9 my $setup = shift;
19              
20 8         117 my @r = $setup->();
21 8 50       17 _croak "Garbage in Proc::Fork setup (semicolon after last block clause?)" if @r;
22 8         8 $do_clear = 1;
23              
24 8         12 my $pid;
25             my $i;
26              
27             {
28 8         37 $pid = fork;
  12         1417  
29 12 100       199 last if defined $pid;
30 7 100 100     15 redo if $retry and $retry->( ++$i );
31 3 50       19 die "Cannot fork: $!\n" if not $error;
32 3         6 $error->();
33 3         52 return;
34             }
35              
36 5 100 33     198 $_->( $pid || () ) for ( $pid ? $parent : $child ) || ();
      66        
37              
38 5         112471 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   1 1 32 eval $code;
  3 50   3 1 9  
  3 50   3 1 9  
  3 50   5 1 8  
  3 50   2   16  
  3 100       3  
  3 50       106  
  3 50       13  
  3 50       24  
  3 100       7  
  3 50       9  
  3 50       31  
  5 50       142  
  5 100       10  
  5 100       16  
  5 50       11  
  4 50       15  
  4 50       36  
  2 50       6  
  2 50       4  
  2         12  
  2         5  
  2         11  
  2         3  
52             }
53              
54             1;
55              
56             __END__