blib/lib/System/Command.pm

filter: Statementstmt 95.6% 220 / 230 Branchbran 60.7% 62 / 102 Conditioncond 20.8% 5 / 24 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 60 / 60 Podpod 100% 5 / 5 totaltotal 81.3% 354 / 435 CC 110 SCAR 22.5
CC
110
Cov
81%
new
23.5
new
23.5
loop_on
16.4
CRAP 9.4
« blib/lib/System/Command/Reaper.pm ↑ Summary t/info.pl »
1 package System::Command;
2 40 $System::Command::VERSION = '1.122';
3 0 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine BEGIN: called CC=1 SCAR=1.2
4 0 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine BEGIN: called CC=1 SCAR=1.2
5 41 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 41 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8 41 ▶use Cwd qw( cwd );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
9 41 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
10 41 ▶use Symbol ();
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
11 41 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
12 41 ▶use List::Util qw( reduce );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
13 41 ▶use System::Command::Reaper;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 41 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
16 41 ▶use Fcntl qw( F_GETFD F_SETFD FD_CLOEXEC );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # MSWin32 support
19 41 ▶use constant MSWin32 => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 require IPC::Run if MSWin32;
21  
22 40 our $QUIET = 0;
23  
24 # trace setup at startup
25 1 ▶my $_trace_opts = sub {
Subroutine __ANON__: called CC=4 SCAR=23.3
26 1 my ( $trace, $file, $th ) = split /=/, shift, 2;
27 1 ▶ open $th, '>>', $file or carp "Can't open $file: $!" if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 0 0 0
Condition0%0/6
$file && (open($th, '>>', $file) || carp("Can't open ${file}: $!"))
!ll&&!rl&&r
000
open $th, ">>", $file or carp("Can't open ${file}: $!")
l!l&&r!l&&!r
000
MC/DC0%0/3
$file && (open($th, '>>', $file) || carp("Can't open ${file}: $!"))
$file open $th, ">>", $file carp("Can't open ${file}: $!")
Truth table0%0/4
A && B or C
A= $fileB= open $th, ">>", $fileC= carp("Can't open ${file}: $!")
ABCresult
0XX0
1000
11X1
1011
28 1 ▶ $th ||= *STDERR;
Condition0%0/3
$th ||= *STDERR
l!l&&r!l&&!r
000
MC/DC0%0/2
$th ||= *STDERR
$th *STDERR
Truth table0%0/3
A ||= B
A= $thB= *STDERR
ABresult
1X1
011
000
29 1 return ( $trace, $th );
30 40 };
31 40 my @trace;
32 @trace = $_trace_opts->( $ENV{SYSTEM_COMMAND_TRACE} )
33 40 ▶ if $ENV{SYSTEM_COMMAND_TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SYSTEM_COMMAND_TRACE"} 0 40 40
34  
35 40 ▶sub import {
Subroutine import: called CC=3 SCAR=12.3
36 40 my ( $class, @args ) = @_;
37 40 ▶ my %arg = ( quiet => sub { $QUIET = 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
38 40 for my $arg (@args) {
39 1 $arg =~ s/^-//; # allow dashed options
40 croak "Unknown option '$arg' in 'use System::Command'"
41 1 ▶ if !exists $arg{$arg};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $arg{$arg} 0 0 0
unless exists $arg{$arg} 0 0 0
42 1 $arg{$arg}->();
43 }
44 }
45  
46 # a few simple accessors
47 {
48 41 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
49 40 for my $attr (qw( pid stdin stdout stderr options )) {
50 465 ▶ *$attr = sub { return $_[0]{$attr} };
Subroutine __ANON__: called CC=1 SCAR=0.0
51 }
52 40 for my $attr (qw( exit signal core )) {
53 41 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
54 261 ▶ *$attr = sub { $_[0]->is_terminated(); return $_[0]{$attr} };
Subroutine __ANON__: called CC=1 SCAR=0.0
55 }
56 40 for my $attr (qw( cmdline )) {
57 38 ▶ *$attr = sub { return @{ $_[0]{$attr} } };
Subroutine __ANON__: called CC=1 SCAR=0.0
58 }
59 }
60  
61 # REALLY PRIVATE FUNCTIONS
62 # a sub-process spawning function
63 130 ▶my $_spawn = sub {
Subroutine __ANON__: called CC=15 SCAR=27.5
64 131 my ($o, @cmd) = @_;
65 131 my $pid;
66  
67 # setup filehandles
68 131 my $in = Symbol::gensym;
69 131 my $out = Symbol::gensym;
70 131 my $err = Symbol::gensym;
71  
72 # no buffering on pipes used for writing
73 131 select( ( select($in), $| = 1 )[0] );
74  
75 # start the command
76 131 if (MSWin32) {
77 $pid = IPC::Run::start(
78 [@cmd],
79 '<pipe' => $in,
80 '>pipe' => $out,
81 '2>pipe' => $err,
82 );
83 }
84 else {
85  
86 # the code below takes inspiration from IPC::Open3 and Sys::Cmd
87  
88 # create handles for the child process (using CAPITALS)
89 131 my $IN = Symbol::gensym;
90 131 my $OUT = Symbol::gensym;
91 131 my $ERR = Symbol::gensym;
92  
93 # no buffering on pipes used for writing
94 131 select( ( select($OUT), $| = 1 )[0] );
95 130 select( ( select($ERR), $| = 1 )[0] );
96  
97 # connect parent and child with pipes
98 131 ▶ pipe $IN, $in or croak "input pipe(): $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe $IN, $in or croak("input pipe(): $!") 129 1 130
99 131 ▶ pipe $out, $OUT or croak "output pipe(): $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe $out, $OUT or croak("output pipe(): $!") 129 1 130
100 131 ▶ pipe $err, $ERR or croak "errput pipe(): $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe $err, $ERR or croak("errput pipe(): $!") 130 0 130
101  
102 # an extra pipe to communicate exec() failure
103 131 pipe my ( $stat_r, $stat_w );
104  
105 # create the child process
106 131 $pid = fork;
107 130 ▶ croak "Can't fork: $!" if !defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 1 129 130
108  
109 130 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 100 29 129
110  
111 # parent won't use those handles
112 101 close $stat_w;
113 101 close $IN;
114 101 close $OUT;
115 101 close $ERR;
116  
117 # failed to fork+exec?
118 101 my $mesg = do { local $/; <$stat_r> };
119 101 ▶ die $mesg if $mesg;
Branch: 1 branch
BranchTrueFalseTotal
if $mesg 1 99 100
120 }
121 else { # kid
122  
123 # use $stat_r to communicate errors back to the parent
124 30 eval {
125  
126 # child won't use those handles
127 30 close $stat_r;
128 30 close $in;
129 30 close $out;
130 30 close $err;
131  
132 # setup process group if possible
133 30 ▶ setpgrp 0, 0 if $o->{setpgrp} && $Config{d_setpgrp};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"setpgrp"} and $System::Command::Config{"d_setpgrp"} 29 0 29
Condition33%1/3
$o->{"setpgrp"} and $System::Command::Config{"d_setpgrp"}
!ll&&!rl&&r
0029
MC/DC0%0/2
$o->{"setpgrp"} and $System::Command::Config{"d_setpgrp"}
$o->{"setpgrp"} $System::Command::Config{"d_setpgrp"}
Truth table33%1/3
A and B
A= $o->{"setpgrp"}B= $System::Command::Config{"d_setpgrp"}
ABresult
0X0
100
111
134  
135 # close $stat_w on exec
136 30 ▶ my $flags = fcntl( $stat_w, F_GETFD, 0 )
Branch: 1 branch
BranchTrueFalseTotal
my $flags = fcntl($stat_w, 1, 0) or croak("fcntl GETFD failed: $!") 29 0 29
137 or croak "fcntl GETFD failed: $!";
138 30 ▶ fcntl( $stat_w, F_SETFD, $flags | FD_CLOEXEC )
Branch: 1 branch
BranchTrueFalseTotal
fcntl $stat_w, 2, $flags | 1 or croak("fcntl SETFD failed: $!") 29 0 29
139 or croak "fcntl SETFD failed: $!";
140  
141 # associate STDIN, STDOUT and STDERR to the pipes
142 30 my ( $fd_IN, $fd_OUT, $fd_ERR )
143 = ( fileno $IN, fileno $OUT, fileno $ERR );
144 30 ▶ open \*STDIN, "<&=$fd_IN"
Branch: 1 branch
BranchTrueFalseTotal
open \*STDIN, "<&=$fd_IN" or croak("Can't open( \\*STDIN, '<&=${fd_IN}' ): $!") 29 0 29
145 or croak "Can't open( \\*STDIN, '<&=$fd_IN' ): $!";
146 30 ▶ open \*STDOUT, ">&=$fd_OUT"
Branch: 1 branch
BranchTrueFalseTotal
open \*STDOUT, ">&=$fd_OUT" or croak("Can't open( \\*STDOUT, '<&=${fd_OUT}' ): $!") 29 0 29
147 or croak "Can't open( \\*STDOUT, '<&=$fd_OUT' ): $!";
148 30 ▶ open \*STDERR, ">&=$fd_ERR"
Branch: 1 branch
BranchTrueFalseTotal
open \*STDERR, ">&=$fd_ERR" or croak("Can't open( \\*STDERR, '<&=${fd_ERR}' ): $!") 29 0 29
149 or croak "Can't open( \\*STDERR, '<&=$fd_ERR' ): $!";
150  
151 # and finally, exec into @cmd
152 30 exec( { $cmd[0] } @cmd )
153 30 ▶ or do { croak "Can't exec( @cmd ): $!"; }
Branch: 1 branch
BranchTrueFalseTotal
exec {$cmd[0];} @cmd or do { croak("Can't exec( @cmd ): $!") } 0 0 0
154 };
155  
156 # something went wrong
157 1 print $stat_w $@;
158 1 close $stat_w;
159  
160 # DIE DIE DIE
161 1 eval { require POSIX; POSIX::_exit(255); };
162 1 exit 255;
163 }
164 }
165  
166 100 return ( $pid, $in, $out, $err );
167 40 };
168  
169 1 ▶my $_dump_ref = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
170 1 require Data::Dumper; # only load if needed
171 1 local $Data::Dumper::Indent = 0;
172 1 local $Data::Dumper::Purity = 0;
173 1 local $Data::Dumper::Maxdepth = 0;
174 1 local $Data::Dumper::Quotekeys = 0;
175 1 local $Data::Dumper::Sortkeys = 1;
176 1 local $Data::Dumper::Useqq = 1;
177 1 local $Data::Dumper::Terse = 1;
178 1 return Data::Dumper->Dump( [shift] );
179 40 };
180  
181 1 ▶my $_do_trace = sub {
Subroutine __ANON__: called CC=5 SCAR=23.4
182 1 my ( $trace, $th, $pid, $cmd, $o ) = @_;
183 1 ▶ print $th "System::Command cmd[$pid]: ",
Branch: 1 branch
BranchTrueFalseTotal
/\s/ ? : 0 0 0
184 join( ' ', map /\s/ ? $_dump_ref->($_) : $_, @$cmd ), "\n";
185 print $th map "System::Command opt[$pid]: $_->[0] => $_->[1]\n",
186 map [ $_ => $_dump_ref->( $o->{$_} ) ],
187 1 ▶ grep { $_ ne 'env' } sort keys %$o
Branch: 1 branch
BranchTrueFalseTotal
if $trace > 1 0 0 0
188 if $trace > 1;
189 print $th map "System::Command env[$pid]: $_->[0] => $_->[1]\n",
190 map [ $_ => $_dump_ref->( $o->{env}{$_} ) ],
191 1 ▶ keys %{ $o->{env} || {} }
Branch: 1 branch
BranchTrueFalseTotal
if $trace > 2 0 0 0
Condition0%0/3
$o->{'env'} || {}
l!l
000
MC/DC0%0/1
$o->{'env'} || {}
$o->{'env'}
Truth table0%0/2
A
A= $o->{'env'}
Aresult
11
00
192 if $trace > 2;
193 40 };
194  
195 # module methods
196 131 ▶sub new {
Subroutine new: called CC=22 SCAR=31.6
Pod documented
197 131 my ( $class, @cmd ) = @_;
198  
199 # split the args
200 131 my @o = { setpgrp => 1 };
201 131 ▶ @cmd = grep { !( ref eq 'HASH' ? push @o, $_ : 0 ) } @cmd;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 86 374 460
202  
203 # merge the option hashes
204 87 ▶ my $o = reduce {
Subroutine __ANON__: called CC=3 SCAR=11.0
205 +{ %$a, %$b,
206 exists $a->{env} && exists $b->{env}
207 87 ▶ ? ( env => { %{ $a->{env} }, %{ $b->{env} } } )
Branch: 1 branch
BranchTrueFalseTotal
exists $System::Command::a->{'env'} && exists $System::Command::b->{'env'} ? : 10 76 86
Condition100%3/3
exists $System::Command::a->{"env"} and exists $System::Command::b->{"env"}
!ll&&!rl&&r
69710
MC/DC100%2/2
exists $System::Command::a->{"env"} and exists $System::Command::b->{"env"}
exists $System::Command::a->{"env"} exists $System::Command::b->{"env"}
Truth table100%3/3
A and B
A= exists $System::Command::a->{"env"}B= exists $System::Command::b->{"env"}
ABresult
0X0
100
111
208 : ()
209 };
210 }
211 131 @o;
212  
213 # open the trace file before changing directory
214 131 my ( $trace, $th );
215 131 ▶ ( $trace, $th ) = $_trace_opts->( $o->{trace} ) if $o->{trace};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"trace"} 0 130 130
216 131 ▶ ( $trace, $th ) = @trace if @trace; # environment override
Branch: 1 branch
BranchTrueFalseTotal
if @trace 0 130 130
217  
218 # chdir to the expected directory
219 131 my $orig = cwd;
220 131 ▶ my $dest = defined $o->{cwd} ? $o->{cwd} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $o->{'cwd'} ? : 7 123 130
221 131 ▶ if ( defined $dest ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dest) 7 123 130
222 8 ▶ chdir $dest or croak "Can't chdir to $dest: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $dest or croak("Can't chdir to ${dest}: $!") 6 1 7
223 }
224  
225 # keep changes to the environment local
226 130 local %ENV = %ENV;
227  
228 # update the environment
229 130 ▶ if ( exists $o->{env} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $o->{"env"}) 35 94 129
230 croak "ENV variables cannot be empty strings on Win32"
231 36 if MSWin32 and grep { defined and !length } values %{ $o->{env} };
232 36 @ENV{ keys %{ $o->{env} } } = values %{ $o->{env} };
233 delete $ENV{$_}
234 36 for grep { !defined $o->{env}{$_} } keys %{ $o->{env} };
235 }
236  
237 # interactive mode requested
238 130 ▶ if ( $o->{interactive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"interactive"}) 0 129 129
239 1 ▶ croak "Can't run command in interactive mode: not a terminal"
Branch: 1 branch
BranchTrueFalseTotal
unless -t STDIN 0 0 0
240 unless -t STDIN;
241  
242 1 system { $cmd[0] } @cmd;
243  
244 1 my $self = bless {
245 cmdline => [@cmd],
246 options => $o,
247 stdin => IO::Handle->new,
248 stdout => IO::Handle->new,
249 stderr => IO::Handle->new,
250 exit => $? >> 8,
251 signal => $? & 127,
252 core => $? & 128,
253 }, $class;
254  
255 defined reftype( $o->{$_} )
256 and reftype( $o->{$_} ) eq 'SCALAR'
257 1 and ${ $o->{$_} } = $self->{$_}
258 1 ▶ for qw( exit signal core );
Branch: 1 branch
BranchTrueFalseTotal
defined builtin::reftype($o->{$_}) and builtin::reftype($o->{$_}) eq "SCALAR" and ${$$o{$_};} = $self->{$_} 0 0 0
Condition0%0/3
defined builtin::reftype($o->{$_}) and builtin::reftype($o->{$_}) eq "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/2
defined builtin::reftype($o->{$_}) and builtin::reftype($o->{$_}) eq "SCALAR"
defined builtin::reftype($o->{$_}) builtin::reftype($o->{$_}) eq "SCALAR"
Truth table0%0/3
A and B
A= defined builtin::reftype($o->{$_})B= builtin::reftype($o->{$_}) eq "SCALAR"
ABresult
0X0
100
111
259  
260 1 return $self;
261 }
262  
263 # start the command
264 130 my ( $pid, $in, $out, $err ) = eval { $_spawn->( $o, @cmd ); };
265  
266 # FIXME - better check error conditions
267 101 ▶ if ( !defined $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 1 99 100
268 2 ▶ $_do_trace->( $trace, $th, '!', \@cmd, $o ) if $trace;
Branch: 1 branch
BranchTrueFalseTotal
if $trace 0 1 1
269 2 croak $@;
270 }
271  
272 # trace is mostly a debugging tool
273 100 ▶ $_do_trace->( $trace, $th, $pid, \@cmd, $o ) if $trace;
Branch: 1 branch
BranchTrueFalseTotal
if $trace 0 99 99
274  
275 # some input was provided
276 100 ▶ if ( defined $o->{input} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $o->{"input"}) 5 94 99
277 local $SIG{PIPE}
278 1 ▶ = sub { croak "Broken pipe when writing to: @cmd" }
Subroutine __ANON__: called CC=1 SCAR=0.0
279 6 ▶ if $Config{sig_name} =~ /\bPIPE\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $System::Command::Config{"sig_name"} =~ /\bPIPE\b/ 5 0 5
280 6 ▶ print {$in} $o->{input} if length $o->{input};
Branch: 1 branch
BranchTrueFalseTotal
if length $o->{"input"} 3 2 5
281 6 $in->close;
282 }
283  
284 # chdir back to origin
285 100 ▶ if ( defined $dest ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dest) 5 94 99
286 6 ▶ chdir $orig or croak "Can't chdir back to $orig: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $orig or croak("Can't chdir back to ${orig}: $!") 5 0 5
287 }
288  
289 # create the object
290 my $self = bless {
291 cmdline => [@cmd],
292 options => $o,
293 100 pid => MSWin32 ? $pid->{KIDS}[0]{PID} : $pid,
294 stdin => $in,
295 stdout => $out,
296 stderr => $err,
297 ( _ipc_run => $pid )x!! MSWin32,
298 }, $class;
299  
300 # create the subprocess reaper and link the handles and command to it
301 100 ${*$in} = ${*$out} = ${*$err} = $self->{reaper} # typeglobs FTW
302 100 = System::Command::Reaper->new( $self, { trace => $trace, th => $th } );
303  
304 100 return $self;
305 }
306  
307 3 ▶sub spawn {
Subroutine spawn: called CC=1 SCAR=0.0
Pod documented
308 2 my ( $class, @cmd ) = @_;
309 2 return @{ $class->new(@cmd) }{qw( pid stdin stdout stderr )};
310 }
311  
312 4 ▶sub loop_on {
Subroutine loop_on: called CC=14 SCAR=27.9
Pod documented
313 3 my $self = shift;
314  
315 # handle options and defaults
316 my %args = (
317 0 ▶ stderr => sub { print STDERR shift },
Subroutine __ANON__: called CC=1 SCAR=6.9
318 @_
319 3 );
320 3 for my $which ( grep exists $args{$_}, qw( stdout stderr ) ) {
321 6 ▶ if ( $args{$which} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$which}) { } 5 1 6
322 croak "'$which' option must be a CODE reference"
323 5 ▶ if reftype $args{$which} ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($args{$which}) ne "CODE" 0 5 5
324 }
325 else {
326 1 delete $args{$which};
327 }
328 }
329  
330 # create an object for the class method
331 3 ▶ if ( !ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 3 3
332 die "'command' attribute required by loop_on when used as a class method"
333 0 ▶ if !exists $args{command};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"command"} 0 0 0
334 0 $self = $self->new( @{ $args{command} } );
335 }
336  
337 3 require IO::Select;
338 3 my $select = IO::Select->new( $self->stdout, $self->stderr );
339  
340 local $/ = $args{input_record_separator}
341 3 ▶ if exists $args{input_record_separator};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"input_record_separator"} 0 3 3
342  
343 # loop until end of streams
344 3 ▶ while ( my @ready = $select->can_read ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $select->can_read) 16 2 18
345 16 for my $fh (@ready) {
346 21 ▶ my $which = $fh == $self->stdout ? 'stdout' : 'stderr';
Branch: 1 branch
BranchTrueFalseTotal
$fh == $self->stdout ? : 13 8 21
347 21 ▶ if ( defined( my $line = <$fh> ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) { } 17 4 21
348 17 my $ret = 1;
349 $ret = $args{$which}->($line)
350 17 ▶ if exists $args{$which};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$which} 13 4 17
351 17 ▶ return if !$ret;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 1 16 17
352 }
353 else {
354 4 $select->remove($fh);
355 4 $fh->close;
356 }
357 }
358 }
359  
360 # close all pipes and wait for the child to terminate
361 2 $self->close;
362  
363 # success in the Unix sense
364 2 ▶ return defined $self->exit && $self->exit == 0;
Condition33%1/3
defined $self->exit && $self->exit == 0
!ll&&!rl&&r
020
MC/DC0%0/2
defined $self->exit && $self->exit == 0
defined $self->exit $self->exit == 0
Truth table33%1/3
A && B
A= defined $self->exitB= $self->exit == 0
ABresult
0X0
100
111
365 }
366  
367 # delegate those to the reaper (when there's one)
368 334 ▶sub is_terminated {
Subroutine is_terminated: called CC=2 SCAR=7.6
Pod documented
369 return $_[0]{options}{interactive}
370 ? 1
371 333 ▶ : $_[0]{reaper}->is_terminated();
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'options'}{'interactive'} ? : 0 333 333
372 }
373  
374 70 ▶sub close {
Subroutine close: called CC=2 SCAR=7.2
Pod documented
375 69 ▶ $_[0]{reaper}->close() unless $_[0]{options}{interactive};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"options"}{"interactive"} 69 0 69
376 69 return $_[0];
377 }
378  
379 40 1;
380  
381 __END__
« blib/lib/System/Command/Reaper.pm ↑ Summary t/info.pl »