File Coverage

blib/lib/Proc/Branch.pm
Criterion Covered Total %
statement 9 49 18.3
branch 0 22 0.0
condition 0 5 0.0
subroutine 3 8 37.5
pod 4 4 100.0
total 16 88 18.1


line stmt bran cond sub pod time code
1             package Proc::Branch;
2              
3 1     1   32618 use strict;
  1         2  
  1         33  
4 1     1   3 use warnings;
  1         2  
  1         23  
5 1     1   4 use Carp;
  1         5  
  1         492  
6              
7             our $VERSION = '0.01';
8              
9             sub new {
10 0     0 1   my $class = shift;
11 0           my %arg = (
12             'branch' => 2,
13             'sleep' => 0,
14             'debug' => 0,
15             'auto_merge' => 1,
16             @_,
17             );
18              
19 0           my $debug = $arg{debug};
20              
21 0           my @pid;
22 0 0         if ( ( my $n = $arg{branch} - 1 ) > 0 ) {
23 0           while ( $n >= 1 ) {
24 0           my $pid = fork;
25 0 0         if ( not defined $pid ) {
    0          
26 0 0         carp "ERROR: cannot fork\n" if ( $debug );
27 0           return;
28             }
29             elsif ( $pid == 0 ) {
30             # child process
31 0 0         print "INFO($class): Child process($$) launches.\n"
32             if ( $debug );
33 0           $arg{proc} = int $n;
34 0           return bless( \%arg, __PACKAGE__ );
35             }
36             else {
37             # parent
38 0           push @pid, $pid;
39 0           $n--;
40 0           sleep $arg{'sleep'};
41             }
42             }
43             }
44              
45 0 0         print "INFO($class): Branching is completed.\n"
46             if ( $debug );
47 0           $arg{proc} = 0;
48 0           $arg{pid} = \@pid;
49              
50 0           return bless( \%arg, $class );
51             }
52              
53             sub proc {
54 0     0 1   my $self = shift;
55 0           return $self->{proc};
56             }
57              
58             sub merge {
59 0     0 1   my $self = shift;
60 0   0       my $exit = shift || 0;
61              
62 0           $self->{auto_merge} = 0;
63              
64 0           my $debug = $self->{debug};
65              
66 0 0         if ( $self->{proc} ) {
67 0 0         print "INFO(" . __PACKAGE__ . "): Child process($$) exits.\n"
68             if ( $debug );
69 0           exit($exit);
70             }
71             else {
72             # waitpid
73 0           for ( @{ $self->{pid} } ) {
  0            
74 0 0         print "INFO(" . __PACKAGE__ . "): Waiting for child process($_).\n"
75             if ( $debug );
76 0           waitpid $_, 0;
77             }
78             }
79             }
80              
81             sub pid {
82 0     0 1   my $self = shift;
83 0           my $proc = shift;
84              
85 0 0 0       return if ( !defined $proc or $proc < 1 );
86 0           return $self->{pid}[$proc-1];
87             }
88              
89             sub DESTROY {
90 0     0     my $self = shift;
91 0 0         if ( $self->{auto_merge} ) {
92 0           $self->merge;
93             }
94             }
95              
96             1;
97             __END__