| 1 | package Proc::Hevy; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 25 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 25 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 25 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 25 | ▶ | use Errno qw( EWOULDBLOCK ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 25 | ▶ | use IO::Pipe; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 25 | ▶ | use IO::Select; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 25 | ▶ | use Proc::Hevy::Reader; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 25 | ▶ | use Proc::Hevy::Writer; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 103 | ▶ | sub exec { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
exec: called CC=30 SCAR=34.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 103 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 103 | ▶ | confess 'Odd number of parameters' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | unless @args % 2 == 0; | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 97 | my %args = @args; | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | confess 'command: Required parameter not defined' | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 97 | ▶ | unless defined $args{command}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 91 | ▶ | if( ref( $args{command} ) =~ /^(?:CODE)?\z/ ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 70 | $args{command} = [ $args{command} ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | elsif( ref( $args{command} ) ne 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 6 | confess 'command: Must be an ARRAY reference'; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | ref( $args{stdin} ) =~ /^(?:ARRAY|CODE|GLOB)\z/ | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | or confess 'stdin: Must be one of ARRAY, CODE or GLOB reference' | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 85 | ▶ | if exists $args{stdin} and ref $args{stdin}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition87%7/8
exists $args{'stdin'} && ref $args{'stdin'} && (ref($args{'stdin'}) =~ /^(?:ARRAY|CODE|GLOB)\z/ || confess('stdin: Must be one of ARRAY, CODE or GLOB reference'))
exists $args{"stdin"} and ref $args{"stdin"}
ref($args{"stdin"}) =~ /^(?:ARRAY|CODE|GLOB)\z/ or confess("stdin: Must be one of ARRAY, CODE or GLOB reference")
MC/DC0%0/3
exists $args{'stdin'} && ref $args{'stdin'} && (ref($args{'stdin'}) =~ /^(?:ARRAY|CODE|GLOB)\z/ || confess('stdin: Must be one of ARRAY, CODE or GLOB reference'))
exists $args{"stdin"}
ref $args{"stdin"}
ref($args{"stdin"}) =~ /^(?:ARRAY|CODE|GLOB)\z/
Truth table50%2/4
A and B && C
A= exists $args{"stdin"}B= ref $args{"stdin"}C= ref($args{"stdin"}) =~ /^(?:ARRAY|CODE|GLOB)\z/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | ref( $args{stdout} ) =~ /^(?:ARRAY|CODE|GLOB|SCALAR)\z/ | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | or confess 'stdout: Must be one of ARRAY, CODE, GLOB or SCALAR reference' | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 79 | ▶ | if exists $args{stdout}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
exists $args{'stdout'} && (ref($args{'stdout'}) =~ /^(?:ARRAY|CODE|GLOB|SCALAR)\z/ || confess('stdout: Must be one of ARRAY, CODE, GLOB or SCALAR reference'))
ref($args{"stdout"}) =~ /^(?:ARRAY|CODE|GLOB|SCALAR)\z/ or confess("stdout: Must be one of ARRAY, CODE, GLOB or SCALAR reference")
MC/DC0%0/2
exists $args{'stdout'} && (ref($args{'stdout'}) =~ /^(?:ARRAY|CODE|GLOB|SCALAR)\z/ || confess('stdout: Must be one of ARRAY, CODE, GLOB or SCALAR reference'))
exists $args{'stdout'}
ref($args{"stdout"}) =~ /^(?:ARRAY|CODE|GLOB|SCALAR)\z/
Truth table66%2/3
A && B
A= exists $args{'stdout'}B= ref($args{"stdout"}) =~ /^(?:ARRAY|CODE|GLOB|SCALAR)\z/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | ref( $args{stderr} ) =~ /^(?:ARRAY|CODE|GLOB|SCALAR)\z/ | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | or confess 'stderr: Must be one of ARRAY, CODE, GLOB or SCALAR reference' | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 73 | ▶ | if exists $args{stderr}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
exists $args{'stderr'} && (ref($args{'stderr'}) =~ /^(?:ARRAY|CODE|GLOB|SCALAR)\z/ || confess('stderr: Must be one of ARRAY, CODE, GLOB or SCALAR reference'))
ref($args{"stderr"}) =~ /^(?:ARRAY|CODE|GLOB|SCALAR)\z/ or confess("stderr: Must be one of ARRAY, CODE, GLOB or SCALAR reference")
MC/DC0%0/2
exists $args{'stderr'} && (ref($args{'stderr'}) =~ /^(?:ARRAY|CODE|GLOB|SCALAR)\z/ || confess('stderr: Must be one of ARRAY, CODE, GLOB or SCALAR reference'))
exists $args{'stderr'}
ref($args{"stderr"}) =~ /^(?:ARRAY|CODE|GLOB|SCALAR)\z/
Truth table66%2/3
A && B
A= exists $args{'stderr'}B= ref($args{"stderr"}) =~ /^(?:ARRAY|CODE|GLOB|SCALAR)\z/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | ref( $args{parent} ) eq 'CODE' | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | or confess 'parent: Must be a CODE reference' | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 67 | ▶ | if defined $args{parent}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
defined $args{'parent'} && (ref $args{'parent'} eq 'CODE' || confess('parent: Must be a CODE reference'))
ref $args{"parent"} eq "CODE" or confess("parent: Must be a CODE reference")
MC/DC0%0/2
defined $args{'parent'} && (ref $args{'parent'} eq 'CODE' || confess('parent: Must be a CODE reference'))
defined $args{'parent'}
ref $args{"parent"} eq "CODE"
Truth table33%1/3
A && B
A= defined $args{'parent'}B= ref $args{"parent"} eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | ref( $args{child} ) eq 'CODE' | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | or confess 'child: Must be a CODE reference' | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 67 | ▶ | if defined $args{child}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
defined $args{'child'} && (ref $args{'child'} eq 'CODE' || confess('child: Must be a CODE reference'))
ref $args{"child"} eq "CODE" or confess("child: Must be a CODE reference")
MC/DC0%0/2
defined $args{'child'} && (ref $args{'child'} eq 'CODE' || confess('child: Must be a CODE reference'))
defined $args{'child'}
ref $args{"child"} eq "CODE"
Truth table33%1/3
A && B
A= defined $args{'child'}B= ref $args{"child"} eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | $args{priority} = 0 | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 67 | ▶ | unless defined $args{priority}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 67 | my $std_i = Proc::Hevy::Writer->new( stdin => $args{stdin} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | 67 | my $std_o = Proc::Hevy::Reader->new( stdout => $args{stdout} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 67 | my $std_e = Proc::Hevy::Reader->new( stderr => $args{stderr} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # fork | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 67 | my $pid = fork; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | 67 | ▶ | confess "fork: $!\n" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | unless defined $pid; | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 67 | ▶ | if( $pid == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # child | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # run callback, if needed | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | $args{child}->( getppid ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 20 | ▶ | if defined $args{child}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # set filehandles | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 20 | $std_i->child( \*STDIN, 0 ); | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | 20 | $std_o->child( \*STDOUT, 1 ); | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | 20 | $std_e->child( \*STDERR, 2 ); | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # set process priority, if needed | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 20 | ▶ | if( $args{priority} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | my $priority = getpriority( 0, $$ ); | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | ▶ | defined $priority | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | or die "getpriority(): $$: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | $priority += $args{priority}; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | setpriority( 0, $$, $priority ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | or die "setpriority(): $$: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # exec | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 20 | ▶ | if( ref( $args{command}->[0] ) eq 'CODE' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 18 | my $sub = shift @{ $args{command} }; | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | 18 | $sub->( @{ $args{command} } ); | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 13 | exit 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 2 | exec @{ $args{command} }; | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | warn "exec: $args{command}->[0]: $!\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | exit 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # parent | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # run callback, if needed | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | $args{parent}->( $pid ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 47 | ▶ | if defined $args{parent}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # set filehandles | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 47 | my ( $select_w, $select_r ) = ( IO::Select->new, IO::Select->new ); | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 47 | my %handles = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 109 | $std_i->parent( $select_w ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | $std_o->parent( $select_r ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | $std_e->parent( $select_r ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # loop | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 47 | ▶ | while( $select_r->count or $select_w->count ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$select_r->count or $select_w->count
MC/DC100%2/2
$select_r->count or $select_w->count
$select_r->count
$select_w->count
Truth table100%3/3
A or B
A= $select_r->countB= $select_w->count
| |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 96 | my ( $readers, $writers ) = IO::Select->select( $select_r, $select_w ); | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | $handles{$_}->read | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 96 | for @$readers; | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | $handles{$_}->write | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 96 | for @$writers; | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # use waitpid() to avoid signal handlers | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 47 | my $rc = waitpid $pid, 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | 47 | ▶ | confess "waitpid: $!" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | if $rc == -1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 47 | return $?; | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 25 | 1 | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||