Run3.pm

filter: Statementstmt 97.8% 181 / 185 Branchbran 79.2% 103 / 130 Conditioncond 71.4% 70 / 98 MC/DCmcdc 28.3% 15 / 53 Subroutinesub 100% 21 / 21 Podpod 100% 1 / 1 totaltotal 80.1% 391 / 488 CC 96 SCAR 35.5
CC
96
Cov
86%
run3
67.1
_spool_data_to_child
18.5
_read_child_output_fh
12.1
CRAP 34.8
↑ Summary Run3/ProfLogger.pm »
1 package IPC::Run3;
2 12 ▶BEGIN { require 5.006_000; } # i.e. 5.6.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 IPC::Run3 - run a subprocess with input/ouput redirection
8  
9 =head1 VERSION
10  
11 version 0.048
12  
13 =cut
14  
15 12 our $VERSION = '0.048';
16  
17 =head1 SYNOPSIS
18  
19 use IPC::Run3; # Exports run3() by default
20  
21 run3 \@cmd, \$in, \$out, \$err;
22  
23 =head1 DESCRIPTION
24  
25 This module allows you to run a subprocess and redirect stdin, stdout,
26 and/or stderr to files and perl data structures. It aims to satisfy 99% of the
27 need for using C<system>, C<qx>, and C<open3>
28 with a simple, extremely Perlish API.
29  
30 Speed, simplicity, and portability are paramount. (That's speed of Perl code;
31 which is often much slower than the kind of buffered I/O that this module uses
32 to spool input to and output from the child command.)
33  
34 =cut
35  
36 12 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 12 our @ISA = qw(Exporter);
38 12 our @EXPORT = qw( run3 );
39 12 our %EXPORT_TAGS = ( all => \@EXPORT );
40  
41 12 ▶use constant debugging => $ENV{IPCRUN3DEBUG} || $ENV{IPCRUNDEBUG} || 0;
Subroutine BEGIN: called CC=3 SCAR=12.5
Condition40%2/5
$ENV{'IPCRUN3DEBUG'} || $ENV{'IPCRUNDEBUG'} || 0
l!l
012
$ENV{"IPCRUN3DEBUG"} or $ENV{"IPCRUNDEBUG"}
l!l&&r!l&&!r
0012
MC/DC0%0/2
$ENV{'IPCRUN3DEBUG'} || $ENV{'IPCRUNDEBUG'} || 0
$ENV{"IPCRUN3DEBUG"} $ENV{"IPCRUNDEBUG"}
Truth table33%1/3
A or B
A= $ENV{"IPCRUN3DEBUG"}B= $ENV{"IPCRUNDEBUG"}
ABresult
1X1
011
000
42 12 ▶use constant profiling => $ENV{IPCRUN3PROFILE} || $ENV{IPCRUNPROFILE} || 0;
Subroutine BEGIN: called CC=3 SCAR=11.0
Condition80%4/5
$ENV{'IPCRUN3PROFILE'} || $ENV{'IPCRUNPROFILE'} || 0
l!l
111
$ENV{"IPCRUN3PROFILE"} or $ENV{"IPCRUNPROFILE"}
l!l&&r!l&&!r
1011
MC/DC50%1/2
$ENV{'IPCRUN3PROFILE'} || $ENV{'IPCRUNPROFILE'} || 0
$ENV{"IPCRUN3PROFILE"} $ENV{"IPCRUNPROFILE"}
Truth table66%2/3
A or B
A= $ENV{"IPCRUN3PROFILE"}B= $ENV{"IPCRUNPROFILE"}
ABresult
1X1
011
000
43 12 ▶use constant is_win32 => 0 <= index $^O, "Win32";
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 BEGIN {
46 12 ▶ if ( is_win32 ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
47 eval "use Win32 qw( GetOSName ); use Win32::ShellQuote qw(quote_native); 1" or die $@;
48 }
49 }
50  
51 #use constant is_win2k => is_win32 && GetOSName() =~ /Win2000/i;
52 #use constant is_winXP => is_win32 && GetOSName() =~ /WinXP/i;
53  
54 12 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
55 12 ▶use File::Temp qw( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
56 12 ▶use POSIX qw( dup dup2 );
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 # We cache the handles of our temp files in order to
59 # keep from having to incur the (largish) overhead of File::Temp
60 12 my %fh_cache;
61 12 my $fh_cache_pid = $$;
62  
63 12 my $profiler;
64  
65 2 ▶sub _profiler { $profiler } # test suite access
Subroutine _profiler: called CC=1 SCAR=0.0
66  
67 BEGIN {
68 12 ▶ if ( profiling ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶ eval "use Time::HiRes qw( gettimeofday ); 1" or die $@;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 if ( $ENV{IPCRUN3PROFILE} =~ /\A\d+\z/ ) {
71 require IPC::Run3::ProfPP;
72 IPC::Run3::ProfPP->import;
73 $profiler = IPC::Run3::ProfPP->new(Level => $ENV{IPCRUN3PROFILE});
74 } else {
75 my ( $dest, undef, $class ) =
76 reverse split /(=)/, $ENV{IPCRUN3PROFILE}, 2;
77 $class = "IPC::Run3::ProfLogger"
78 unless defined $class && length $class;
79 if ( not eval "require $class" ) {
80 my $e = $@;
81 $class = "IPC::Run3::$class";
82 eval "require IPC::Run3::$class" or die $e;
83 }
84 $profiler = $class->new( Destination => $dest );
85 }
86 $profiler->app_call( [ $0, @ARGV ], scalar gettimeofday() );
87 }
88 }
89  
90  
91 END {
92 13 ▶ $profiler->app_exit( scalar gettimeofday() ) if profiling;
Subroutine END: called CC=1 SCAR=0.0
93 }
94  
95 sub _binmode {
96 266 ▶ my ( $fh, $mode, $what ) = @_;
Subroutine _binmode: called CC=5 SCAR=16.1
97 # if $mode is not given, then default to ":raw", except on Windows,
98 # where we default to ":crlf";
99 # otherwise if a proper layer string was given, use that,
100 # else use ":raw"
101 266 ▶ my $layer = !$mode
Branch: 2 branches
BranchTrueFalseTotal
!$mode ? : 261 6 267
$mode =~ /^:/ ? : 2 4 6
102 ? (is_win32 ? ":crlf" : ":raw")
103 : ($mode =~ /^:/ ? $mode : ":raw");
104 266 warn "binmode $what, $layer\n" if debugging >= 2;
105  
106 267 ▶ binmode $fh, ":raw" unless $layer eq ":raw"; # remove all layers first
Branch: 1 branch
BranchTrueFalseTotal
unless $layer eq ":raw" 2 265 267
107 267 ▶ binmode $fh, $layer or croak "binmode $layer failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, $layer or croak("binmode $layer failed: $!") 0 267 267
108 }
109  
110 sub _spool_data_to_child {
111 95 ▶ my ( $type, $source, $binmode_it ) = @_;
Subroutine _spool_data_to_child: called CC=17 SCAR=29.2
112  
113 # If undef (not \undef) passed, they want the child to inherit
114 # the parent's STDIN.
115 94 ▶ return undef unless defined $source;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 0 94 94
116  
117 94 my $fh;
118 94 ▶ if ( ! $type ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $type) { } 2 92 94
elsif ($type eq 'FH') { } 1 91 92
119 3 ▶ open $fh, "<", $source or croak "$!: $source";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $source or croak("$!: $source") 0 2 2
120 3 _binmode($fh, $binmode_it, "STDIN");
121 2 warn "run3(): feeding file '$source' to child STDIN\n"
122 if debugging >= 2;
123 } elsif ( $type eq "FH" ) {
124 1 $fh = $source;
125 1 warn "run3(): feeding filehandle '$source' to child STDIN\n"
126 if debugging >= 2;
127 } else {
128 91 ▶ $fh = $fh_cache{in} ||= tempfile;
Condition100%3/3
$fh_cache{"in"} ||= tempfile()
l!l&&r!l&&!r
75161
MC/DC0%0/2
$fh_cache{"in"} ||= tempfile()
$fh_cache{"in"} tempfile()
Truth table0%0/3
A ||= B
A= $fh_cache{"in"}B= tempfile()
ABresult
1X1
011
000
129 91 truncate $fh, 0;
130 91 seek $fh, 0, 0;
131 91 _binmode($fh, $binmode_it, "STDIN");
132 91 my $seekit;
133 91 ▶ if ( $type eq "SCALAR" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'SCALAR') { } 88 3 91
elsif ($type eq 'ARRAY') { } 2 1 3
elsif ($type eq 'CODE') { } 1 0 1
134  
135 # When the run3()'s caller asks to feed an empty file
136 # to the child's stdin, we want to pass a live file
137 # descriptor to an empty file (like /dev/null) so that
138 # they don't get surprised by invalid fd errors and get
139 # normal EOF behaviors.
140 88 ▶ return $fh unless defined $$source; # \undef passed
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$source 80 8 88
141  
142 8 warn "run3(): feeding SCALAR to child STDIN",
143 debugging >= 3
144 ? ( ": '", $$source, "' (", length $$source, " chars)" )
145 : (),
146 "\n"
147 if debugging >= 2;
148  
149 8 $seekit = length $$source;
150 8 ▶ print $fh $$source or die "$! writing to temp file";
Branch: 1 branch
BranchTrueFalseTotal
print $fh $$source or die "$! writing to temp file" 0 8 8
151  
152 } elsif ( $type eq "ARRAY" ) {
153 2 warn "run3(): feeding ARRAY to child STDIN",
154 debugging >= 3 ? ( ": '", @$source, "'" ) : (),
155 "\n"
156 if debugging >= 2;
157  
158 2 ▶ print $fh @$source or die "$! writing to temp file";
Branch: 1 branch
BranchTrueFalseTotal
print $fh @$source or die "$! writing to temp file" 0 2 2
159 2 $seekit = grep length, @$source;
160 } elsif ( $type eq "CODE" ) {
161 1 warn "run3(): feeding output of CODE ref '$source' to child STDIN\n"
162 if debugging >= 2;
163 1 my $parms = []; # TODO: get these from $options
164 1 while (1) {
165 3 my $data = $source->( @$parms );
166 3 ▶ last unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 1 2 3
167 2 ▶ print $fh $data or die "$! writing to temp file";
Branch: 1 branch
BranchTrueFalseTotal
print $fh $data or die "$! writing to temp file" 0 2 2
168 2 $seekit = length $data;
169 }
170 }
171  
172 11 ▶ seek $fh, 0, 0 or croak "$! seeking on temp file for child's stdin"
Branch: 1 branch
BranchTrueFalseTotal
if $seekit 11 0 11
Condition33%2/6
$seekit && (seek($fh, 0, 0) || croak("$! seeking on temp file for child's stdin"))
!ll&&!rl&&r
0110
seek $fh, 0, 0 or croak("$! seeking on temp file for child's stdin")
l!l&&r!l&&!r
1100
MC/DC0%0/3
$seekit && (seek($fh, 0, 0) || croak("$! seeking on temp file for child's stdin"))
$seekit seek $fh, 0, 0 croak("$! seeking on temp file for child's stdin")
Truth table0%0/4
A && B or C
A= $seekitB= seek $fh, 0, 0C= croak("$! seeking on temp file for child's stdin")
ABCresult
0XX0
1000
11X1
1011
173 if $seekit;
174 }
175  
176 14 ▶ croak "run3() can't redirect $type to child stdin"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 14 14
177 unless defined $fh;
178  
179 14 return $fh;
180 }
181  
182 sub _fh_for_child_output {
183 173 ▶ my ( $what, $type, $dest, $options ) = @_;
Subroutine _fh_for_child_output: called CC=9 SCAR=22.0
184  
185 173 my $fh;
186 173 ▶ if ( $type eq "SCALAR" && $dest == \undef ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'SCALAR' and $dest == \(undef)) { } 68 105 173
elsif ($type eq 'FH') { } 1 104 105
elsif (not $type) { } 2 102 104
Condition100%3/3
$type eq "SCALAR" and $dest == \(undef)
!ll&&!rl&&r
69968
MC/DC100%2/2
$type eq "SCALAR" and $dest == \(undef)
$type eq "SCALAR" $dest == \(undef)
Truth table100%3/3
A and B
A= $type eq "SCALAR"B= $dest == \(undef)
ABresult
0X0
100
111
187 68 warn "run3(): redirecting child $what to oblivion\n"
188 if debugging >= 2;
189  
190 68 ▶ $fh = $fh_cache{nul} ||= do {
Condition66%2/3
$fh_cache{"nul"} ||= do { open $fh, ">", "File::Spec"->devnull; $fh }
l!l&&r!l&&!r
53150
MC/DC0%0/2
$fh_cache{"nul"} ||= do { open $fh, ">", "File::Spec"->devnull; $fh }
$fh_cache{"nul"} do { open $fh, ">", "File::Spec"->devnull; $fh }
Truth table66%2/3
A ||= B
A= $fh_cache{"nul"}B= do { open $fh, ">", "File::Spec"->devnull; $fh }
ABresult
1X1
011
000
191 15 open $fh, ">", File::Spec->devnull;
192 15 $fh;
193 };
194 } elsif ( $type eq "FH" ) {
195 1 $fh = $dest;
196 1 warn "run3(): redirecting $what to filehandle '$dest'\n"
197 if debugging >= 3;
198 } elsif ( !$type ) {
199 2 warn "run3(): feeding child $what to file '$dest'\n"
200 if debugging >= 2;
201  
202 2 ▶ open $fh, $options->{"append_$what"} ? ">>" : ">", $dest
Branch: 2 branches
BranchTrueFalseTotal
open $fh, $$options{"append_$what"} ? ">>" : ">", $dest or croak("$!: $dest") 0 2 2
$$options{"append_$what"} ? : 1 1 2
203 or croak "$!: $dest";
204 } else {
205 102 warn "run3(): capturing child $what\n"
206 if debugging >= 2;
207  
208 102 ▶ $fh = $fh_cache{$what} ||= tempfile;
Condition66%2/3
$fh_cache{$what} ||= tempfile()
l!l&&r!l&&!r
84180
MC/DC0%0/2
$fh_cache{$what} ||= tempfile()
$fh_cache{$what} tempfile()
Truth table66%2/3
A ||= B
A= $fh_cache{$what}B= tempfile()
ABresult
1X1
011
000
209 102 seek $fh, 0, 0;
210 102 truncate $fh, 0;
211 }
212  
213 173 my $binmode_it = $options->{"binmode_$what"};
214 173 _binmode($fh, $binmode_it, uc $what);
215  
216 173 return $fh;
217 }
218  
219 sub _read_child_output_fh {
220 170 ▶ my ( $what, $type, $dest, $fh, $options ) = @_;
Subroutine _read_child_output_fh: called CC=12 SCAR=24.9
221  
222 170 ▶ return if $type eq "SCALAR" && $dest == \undef;
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "SCALAR" and $dest == \(undef) 68 102 170
Condition100%3/3
$type eq "SCALAR" and $dest == \(undef)
!ll&&!rl&&r
39968
MC/DC100%2/2
$type eq "SCALAR" and $dest == \(undef)
$type eq "SCALAR" $dest == \(undef)
Truth table100%3/3
A and B
A= $type eq "SCALAR"B= $dest == \(undef)
ABresult
0X0
100
111
223  
224 102 ▶ seek $fh, 0, 0 or croak "$! seeking on temp file for child $what";
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or croak("$! seeking on temp file for child $what") 0 102 102
225  
226 102 ▶ if ( $type eq "SCALAR" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'SCALAR') { } 99 3 102
elsif ($type eq 'ARRAY') { } 2 1 3
elsif ($type eq 'CODE') { } 1 0 1
227 99 warn "run3(): reading child $what to SCALAR\n"
228 if debugging >= 3;
229  
230 # two read()s are used instead of 1 so that the first will be
231 # logged even it reads 0 bytes; the second won't.
232 my $count = read $fh, $$dest, 10_000,
233 99 ▶ $options->{"append_$what"} ? length $$dest : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$options{"append_$what"} ? : 2 97 99
234 99 while (1) {
235 179 ▶ croak "$! reading child $what from temp file"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 0 179 179
236 unless defined $count;
237  
238 179 ▶ last unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 99 80 179
239  
240 80 warn "run3(): read $count bytes from child $what",
241 debugging >= 3 ? ( ": '", substr( $$dest, -$count ), "'" ) : (),
242 "\n"
243 if debugging >= 2;
244  
245 80 $count = read $fh, $$dest, 10_000, length $$dest;
246 }
247 } elsif ( $type eq "ARRAY" ) {
248 2 ▶ if ($options->{"append_$what"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$options{"append_$what"}) { } 1 1 2
249 1 push @$dest, <$fh>;
250 } else {
251 1 @$dest = <$fh>;
252 }
253 2 if ( debugging >= 2 ) {
254 my $count = 0;
255 $count += length for @$dest;
256 warn
257 "run3(): read ",
258 scalar @$dest,
259 " records, $count bytes from child $what",
260 debugging >= 3 ? ( ": '", @$dest, "'" ) : (),
261 "\n";
262 }
263 } elsif ( $type eq "CODE" ) {
264 1 warn "run3(): capturing child $what to CODE ref\n"
265 if debugging >= 3;
266  
267 1 local $_;
268 1 ▶ while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 2 1 3
269 2 warn
270 "run3(): read ",
271 length,
272 " bytes from child $what",
273 debugging >= 3 ? ( ": '", $_, "'" ) : (),
274 "\n"
275 if debugging >= 2;
276  
277 2 $dest->( $_ );
278 }
279 } else {
280 0 croak "run3() can't redirect child $what to a $type";
281 }
282  
283 }
284  
285 sub _type {
286 300 ▶ my ( $redir ) = @_;
Subroutine _type: called CC=3 SCAR=11.0
287  
288 300 ▶ return "FH" if eval {
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { local $SIG{"__DIE__"}; $redir->isa("IO::Handle") } } 0 300 300
289 300 local $SIG{'__DIE__'};
290 300 $redir->isa("IO::Handle")
291 };
292  
293 300 my $type = ref $redir;
294 300 ▶ return $type eq "GLOB" ? "FH" : $type;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'GLOB' ? : 2 298 300
295 }
296  
297 sub _max_fd {
298 20 ▶ my $fd = dup(0);
Subroutine _max_fd: called CC=1 SCAR=0.0
299 20 POSIX::close $fd;
300 20 return $fd;
301 }
302  
303 my $run_call_time;
304 12 my $sys_call_time;
305 12 my $sys_exit_time;
306  
307 sub run3 {
308 101 ▶ $run_call_time = gettimeofday() if profiling;
Subroutine run3: called CC=51 SCAR=42.1
Pod documented
309  
310 101 ▶ my $options = @_ && ref $_[-1] eq "HASH" ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] eq 'HASH' ? : 9 92 101
Condition100%3/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
1919
MC/DC100%2/2
@_ and ref $_[-1] eq "HASH"
@_ ref $_[-1] eq "HASH"
Truth table100%3/3
A and B
A= @_B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
311  
312 101 my ( $cmd, $stdin, $stdout, $stderr ) = @_;
313  
314 101 print STDERR "run3(): running ",
315 join( " ", map "'$_'", ref $cmd ? @$cmd : $cmd ),
316 "\n"
317 if debugging;
318  
319 101 ▶ if ( ref $cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd) { } 100 1 101
320 100 ▶ croak "run3(): empty command" unless @$cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless @$cmd 0 100 100
321 100 ▶ croak "run3(): undefined command" unless defined $cmd->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmd->[0] 0 100 100
322 100 ▶ croak "run3(): command name ('')" unless length $cmd->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless length $cmd->[0] 0 100 100
323 } else {
324 1 ▶ croak "run3(): missing command" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 0 1
325 0 ▶ croak "run3(): undefined command" unless defined $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmd 0 0 0
326 0 ▶ croak "run3(): command ('')" unless length $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless length $cmd 0 0 0
327 }
328  
329 100 foreach (qw/binmode_stdin binmode_stdout binmode_stderr/) {
330 300 ▶ if (my $mode = $options->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mode = $options->{$_}) 5 295 300
331 5 ▶ croak qq[option $_ must be a number or a proper layer string: "$mode"]
Branch: 1 branch
BranchTrueFalseTotal
unless $mode =~ /^(:|\d+$)/ 0 5 5
332 unless $mode =~ /^(:|\d+$)/;
333 }
334 }
335  
336 100 my $in_type = _type $stdin;
337 100 my $out_type = _type $stdout;
338 100 my $err_type = _type $stderr;
339  
340 100 ▶ if ($fh_cache_pid != $$) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh_cache_pid != $$) 6 94 100
341 # fork detected, close all cached filehandles and clear the cache
342 6 close $_ foreach values %fh_cache;
343 6 %fh_cache = ();
344 6 $fh_cache_pid = $$;
345 }
346  
347 # This routine proceeds in stages so that a failure in an early
348 # stage prevents later stages from running, and thus from needing
349 # cleanup.
350  
351 my $in_fh = _spool_data_to_child $in_type, $stdin,
352 100 ▶ $options->{binmode_stdin} if defined $stdin;
Branch: 1 branch
BranchTrueFalseTotal
if defined $stdin 94 6 100
353  
354 100 ▶ my $out_fh = _fh_for_child_output "stdout", $out_type, $stdout,
Branch: 1 branch
BranchTrueFalseTotal
if defined $stdout 93 7 100
355 $options if defined $stdout;
356  
357 100 ▶ my $tie_err_to_out =
Condition100%6/6
defined $stderr && defined $stdout && $stderr eq $stdout
!ll&&!rl&&r
19792
defined $stderr and defined $stdout
!ll&&!rl&&r
18181
MC/DC100%3/3
defined $stderr && defined $stdout && $stderr eq $stdout
defined $stderr defined $stdout $stderr eq $stdout
Truth table100%4/4
A and B && C
A= defined $stderrB= defined $stdoutC= $stderr eq $stdout
ABCresult
0XX0
10X0
1100
1111
358 defined $stderr && defined $stdout && $stderr eq $stdout;
359  
360 100 ▶ my $err_fh = $tie_err_to_out
Branch: 2 branches
BranchTrueFalseTotal
if defined $stderr 82 18 100
$tie_err_to_out ? : 2 80 82
361 ? $out_fh
362 : _fh_for_child_output "stderr", $err_type, $stderr,
363 $options if defined $stderr;
364  
365 # this should make perl close these on exceptions
366 # local *STDIN_SAVE;
367 100 local *STDOUT_SAVE;
368 100 local *STDERR_SAVE;
369  
370 100 ▶ my $saved_fd0 = dup( 0 ) if defined $in_fh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $in_fh 94 6 100
371  
372 # open STDIN_SAVE, "<&STDIN"# or croak "run3(): $! saving STDIN"
373 # if defined $in_fh;
374 100 ▶ open STDOUT_SAVE, ">&STDOUT" or croak "run3(): $! saving STDOUT"
Branch: 1 branch
BranchTrueFalseTotal
if defined $out_fh 93 7 100
Condition60%3/5
defined $out_fh && (open(STDOUT_SAVE, '>&STDOUT') || croak("run3(): $! saving STDOUT"))
!ll
793
open STDOUT_SAVE, ">&STDOUT" or croak("run3(): $! saving STDOUT")
l!l&&r!l&&!r
9300
MC/DC0%0/3
defined $out_fh && (open(STDOUT_SAVE, '>&STDOUT') || croak("run3(): $! saving STDOUT"))
defined $out_fh open STDOUT_SAVE, ">&STDOUT" croak("run3(): $! saving STDOUT")
Truth table0%0/4
A && B or C
A= defined $out_fhB= open STDOUT_SAVE, ">&STDOUT"C= croak("run3(): $! saving STDOUT")
ABCresult
0XX0
1000
11X1
1011
375 if defined $out_fh;
376 100 ▶ open STDERR_SAVE, ">&STDERR" or croak "run3(): $! saving STDERR"
Branch: 1 branch
BranchTrueFalseTotal
if defined $err_fh 82 18 100
Condition60%3/5
defined $err_fh && (open(STDERR_SAVE, '>&STDERR') || croak("run3(): $! saving STDERR"))
!ll
1882
open STDERR_SAVE, ">&STDERR" or croak("run3(): $! saving STDERR")
l!l&&r!l&&!r
8200
MC/DC0%0/3
defined $err_fh && (open(STDERR_SAVE, '>&STDERR') || croak("run3(): $! saving STDERR"))
defined $err_fh open STDERR_SAVE, ">&STDERR" croak("run3(): $! saving STDERR")
Truth table0%0/4
A && B or C
A= defined $err_fhB= open STDERR_SAVE, ">&STDERR"C= croak("run3(): $! saving STDERR")
ABCresult
0XX0
1000
11X1
1011
377 if defined $err_fh;
378  
379 100 my $errno;
380 100 my $ok = eval {
381 # The open() call here seems to not force fd 0 in some cases;
382 # I ran in to trouble when using this in VCP, not sure why.
383 # the dup2() seems to work.
384 100 ▶ dup2( fileno $in_fh, 0 )
Branch: 1 branch
BranchTrueFalseTotal
if defined $in_fh 94 6 100
Condition60%3/5
defined $in_fh && (dup2(fileno $in_fh, 0) || croak("run3(): $! redirecting STDIN"))
!ll
694
dup2(fileno $in_fh, 0) or croak("run3(): $! redirecting STDIN")
l!l&&r!l&&!r
9400
MC/DC0%0/3
defined $in_fh && (dup2(fileno $in_fh, 0) || croak("run3(): $! redirecting STDIN"))
defined $in_fh dup2(fileno $in_fh, 0) croak("run3(): $! redirecting STDIN")
Truth table0%0/4
A && B or C
A= defined $in_fhB= dup2(fileno $in_fh, 0)C= croak("run3(): $! redirecting STDIN")
ABCresult
0XX0
1000
11X1
1011
385 # open STDIN, "<&=" . fileno $in_fh
386 or croak "run3(): $! redirecting STDIN"
387 if defined $in_fh;
388  
389 # close $in_fh or croak "$! closing STDIN temp file"
390 # if ref $stdin;
391  
392 100 ▶ open STDOUT, ">&" . fileno $out_fh
Branch: 1 branch
BranchTrueFalseTotal
if defined $out_fh 93 7 100
Condition60%3/5
defined $out_fh && (open(STDOUT, '>&' . fileno($out_fh)) || croak("run3(): $! redirecting STDOUT"))
!ll
793
open STDOUT, '>&' . fileno($out_fh) or croak("run3(): $! redirecting STDOUT")
l!l&&r!l&&!r
9300
MC/DC0%0/3
defined $out_fh && (open(STDOUT, '>&' . fileno($out_fh)) || croak("run3(): $! redirecting STDOUT"))
defined $out_fh open STDOUT, '>&' . fileno($out_fh) croak("run3(): $! redirecting STDOUT")
Truth table0%0/4
A && B or C
A= defined $out_fhB= open STDOUT, '>&' . fileno($out_fh)C= croak("run3(): $! redirecting STDOUT")
ABCresult
0XX0
1000
11X1
1011
393 or croak "run3(): $! redirecting STDOUT"
394 if defined $out_fh;
395  
396 100 ▶ open STDERR, ">&" . fileno $err_fh
Branch: 1 branch
BranchTrueFalseTotal
if defined $err_fh 82 18 100
Condition60%3/5
defined $err_fh && (open(STDERR, '>&' . fileno($err_fh)) || croak("run3(): $! redirecting STDERR"))
!ll
1882
open STDERR, '>&' . fileno($err_fh) or croak("run3(): $! redirecting STDERR")
l!l&&r!l&&!r
8200
MC/DC0%0/3
defined $err_fh && (open(STDERR, '>&' . fileno($err_fh)) || croak("run3(): $! redirecting STDERR"))
defined $err_fh open STDERR, '>&' . fileno($err_fh) croak("run3(): $! redirecting STDERR")
Truth table0%0/4
A && B or C
A= defined $err_fhB= open STDERR, '>&' . fileno($err_fh)C= croak("run3(): $! redirecting STDERR")
ABCresult
0XX0
1000
11X1
1011
397 or croak "run3(): $! redirecting STDERR"
398 if defined $err_fh;
399  
400 100 $sys_call_time = gettimeofday() if profiling;
401  
402 my $r = ref $cmd
403 100 ▶ ? system { $cmd->[0] } is_win32 ? quote_native( @$cmd ) : @$cmd
Branch: 1 branch
BranchTrueFalseTotal
ref $cmd ? : 100 0 100
404 : system $cmd;
405  
406 100 $errno = $!; # save $!, because later failures will overwrite it
407 100 $sys_exit_time = gettimeofday() if profiling;
408 100 if ( debugging ) {
409 my $err_fh = defined $err_fh ? \*STDERR_SAVE : \*STDERR;
410 if ( defined $r && $r != -1 ) {
411 print $err_fh "run3(): \$? is $?\n";
412 } else {
413 print $err_fh "run3(): \$? is $?, \$! is $errno\n";
414 }
415 }
416  
417 100 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r and $r == -1 || !!0 and not $options->{"return_if_system_error"}) 0 100 100
Condition75%6/8
defined $r and $r == -1 || !!0 and not $options->{"return_if_system_error"}
!ll&&!rl&&r
9910
defined $r and $r == -1 || !!0
!ll&&!rl&&r
0991
$r == -1 or !!0
l!l
199
MC/DC0%0/3
defined $r and $r == -1 || !!0 and not $options->{"return_if_system_error"}
defined $r $r == -1 not $options->{"return_if_system_error"}
Truth table50%2/4
A and B and not(C)
A= defined $rB= $r == -1C= not $options->{"return_if_system_error"}
ABCresult
0XX0
10X0
1100
1111
418 defined $r
419 && ( $r == -1 || ( is_win32 && $r == 0xFF00 ) )
420 && !$options->{return_if_system_error}
421 ) {
422 0 croak( $errno );
423 }
424  
425 100 1;
426 };
427 100 my $x = $@;
428  
429 100 my @errs;
430  
431 100 ▶ if ( defined $saved_fd0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $saved_fd0) 94 6 100
432 94 dup2( $saved_fd0, 0 );
433 94 POSIX::close( $saved_fd0 );
434 }
435  
436 # open STDIN, "<&STDIN_SAVE"# or push @errs, "run3(): $! restoring STDIN"
437 # if defined $in_fh;
438 100 ▶ open STDOUT, ">&STDOUT_SAVE" or push @errs, "run3(): $! restoring STDOUT"
Branch: 1 branch
BranchTrueFalseTotal
if defined $out_fh 93 7 100
Condition60%3/5
defined $out_fh && (open(STDOUT, '>&STDOUT_SAVE') || push(@errs, "run3(): $! restoring STDOUT"))
!ll
793
open STDOUT, ">&STDOUT_SAVE" or push @errs, "run3(): $! restoring STDOUT"
l!l&&r!l&&!r
9300
MC/DC0%0/3
defined $out_fh && (open(STDOUT, '>&STDOUT_SAVE') || push(@errs, "run3(): $! restoring STDOUT"))
defined $out_fh open STDOUT, ">&STDOUT_SAVE" push @errs, "run3(): $! restoring STDOUT"
Truth table0%0/4
A && B or C
A= defined $out_fhB= open STDOUT, ">&STDOUT_SAVE"C= push @errs, "run3(): $! restoring STDOUT"
ABCresult
0XX0
1000
11X1
1011
439 if defined $out_fh;
440 100 ▶ open STDERR, ">&STDERR_SAVE" or push @errs, "run3(): $! restoring STDERR"
Branch: 1 branch
BranchTrueFalseTotal
if defined $err_fh 82 18 100
Condition60%3/5
defined $err_fh && (open(STDERR, '>&STDERR_SAVE') || push(@errs, "run3(): $! restoring STDERR"))
!ll
1882
open STDERR, ">&STDERR_SAVE" or push @errs, "run3(): $! restoring STDERR"
l!l&&r!l&&!r
8200
MC/DC0%0/3
defined $err_fh && (open(STDERR, '>&STDERR_SAVE') || push(@errs, "run3(): $! restoring STDERR"))
defined $err_fh open STDERR, ">&STDERR_SAVE" push @errs, "run3(): $! restoring STDERR"
Truth table0%0/4
A && B or C
A= defined $err_fhB= open STDERR, ">&STDERR_SAVE"C= push @errs, "run3(): $! restoring STDERR"
ABCresult
0XX0
1000
11X1
1011
441 if defined $err_fh;
442  
443 100 ▶ croak join ", ", @errs if @errs;
Branch: 1 branch
BranchTrueFalseTotal
if @errs 0 100 100
444  
445 100 ▶ die $x unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 100 100
446  
447 100 ▶ _read_child_output_fh "stdout", $out_type, $stdout, $out_fh, $options
Branch: 1 branch
BranchTrueFalseTotal
if defined $out_fh and $out_type and $out_type ne "FH" 90 10 100
Condition100%6/6
defined $out_fh and $out_type and $out_type ne "FH"
!ll&&!rl&&r
9190
defined $out_fh and $out_type
!ll&&!rl&&r
7291
MC/DC100%3/3
defined $out_fh and $out_type and $out_type ne "FH"
defined $out_fh $out_type $out_type ne "FH"
Truth table100%4/4
A and B and C
A= defined $out_fhB= $out_typeC= $out_type ne "FH"
ABCresult
0XX0
10X0
1100
1111
448 if defined $out_fh && $out_type && $out_type ne "FH";
449 100 ▶ _read_child_output_fh "stderr", $err_type, $stderr, $err_fh, $options
Branch: 1 branch
BranchTrueFalseTotal
if defined $err_fh and $err_type and $err_type ne "FH" and not $tie_err_to_out 80 20 100
Condition77%7/9
defined $err_fh and $err_type and $err_type ne "FH" and not $tie_err_to_out
!ll&&!rl&&r
18280
defined $err_fh and $err_type and $err_type ne "FH"
!ll&&!rl&&r
18082
defined $err_fh and $err_type
!ll&&!rl&&r
18082
MC/DC50%2/4
defined $err_fh and $err_type and $err_type ne "FH" and not $tie_err_to_out
defined $err_fh $err_type $err_type ne "FH" not $tie_err_to_out
Truth table60%3/5
A and B and C and not(D)
A= defined $err_fhB= $err_typeC= $err_type ne "FH"D= not $tie_err_to_out
ABCDresult
0XXX0
10XX0
110X0
11100
11111
450 if defined $err_fh && $err_type && $err_type ne "FH" && !$tie_err_to_out;
451 100 $profiler->run_exit(
452 $cmd,
453 $run_call_time,
454 $sys_call_time,
455 $sys_exit_time,
456 scalar gettimeofday()
457 ) if profiling;
458  
459 100 $! = $errno; # restore $! from system()
460  
461 100 return 1;
462 }
463  
464 12 1;
465  
466 __END__
↑ Summary Run3/ProfLogger.pm »