Run3.pm

filter: Statementstmt 97.8% 185 / 189 Branchbran 80.0% 104 / 130 Conditioncond 76.0% 70 / 92 MC/DCmcdc 31.9% 15 / 47 Subroutinesub 100% 21 / 21 Podpod 100% 1 / 1 totaltotal 82.5% 396 / 480 CC 96 SCAR 34.7
CC
96
Cov
87%
run3
61.5
_spool_data_to_child
18.2
_read_child_output_fh
12.1
CRAP 32.2
↑ Summary Run3/ProfLogger.pm »
1 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package IPC::Run3;
4  
5 =head1 NAME
6  
7 IPC::Run3 - run a subprocess with input/output redirection
8  
9 =head1 VERSION
10  
11 version 0.049
12  
13 =cut
14  
15 12 our $VERSION = '0.049';
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 12 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
46 12 if ( is_win32 ) {
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 12 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
68 12 if ( profiling ) {
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 12 ▶END {
Subroutine END: called CC=1 SCAR=0.0
92 13 $profiler->app_exit( scalar gettimeofday() ) if profiling;
93 }
94  
95 272 ▶sub _binmode {
Subroutine _binmode: called CC=5 SCAR=16.1
96 272 my ( $fh, $mode, $what ) = @_;
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 272 ▶ my $layer = !$mode
Branch: 2 branches
BranchTrueFalseTotal
!$mode ? : 268 5 273
$mode =~ /^:/ ? : 2 4 6
102 ? (is_win32 ? ":crlf" : ":raw")
103 : ($mode =~ /^:/ ? $mode : ":raw");
104 272 warn "binmode $what, $layer\n" if debugging >= 2;
105  
106 273 ▶ binmode $fh, ":raw" unless $layer eq ":raw"; # remove all layers first
Branch: 1 branch
BranchTrueFalseTotal
unless $layer eq ":raw" 2 271 273
107 273 ▶ binmode $fh, $layer or croak "binmode $layer failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, $layer or croak("binmode $layer failed: $!") 272 1 273
108 }
109  
110 96 ▶sub _spool_data_to_child {
Subroutine _spool_data_to_child: called CC=17 SCAR=29.0
111 97 my ( $type, $source, $binmode_it ) = @_;
112  
113 # If undef (not \undef) passed, they want the child to inherit
114 # the parent's STDIN.
115 96 ▶ return undef unless defined $source;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 0 96 96
116  
117 96 my $fh;
118 96 ▶ if ( ! $type ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $type) { } 2 94 96
elsif ($type eq 'FH') { } 1 93 94
119 3 ▶ open $fh, "<", $source or croak "$!: $source";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $source or croak("$!: $source") 2 0 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 93 ▶ $fh = $fh_cache{in} ||= tempfile;
Condition100%3/3
$fh_cache{"in"} ||= tempfile()
l!l&&r!l&&!r
77161
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 93 truncate $fh, 0;
130 93 seek $fh, 0, 0;
131 93 _binmode($fh, $binmode_it, "STDIN");
132 93 my $seekit;
133 93 ▶ if ( $type eq "SCALAR" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'SCALAR') { } 90 3 93
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 90 ▶ return $fh unless defined $$source; # \undef passed
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$source 82 8 90
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" 8 0 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" 2 0 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 $params = []; # TODO: get these from $options
164 1 while (1) {
165 3 my $data = $source->( @$params );
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" 2 0 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
Condition40%2/5
$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
110
MC/DC0%0/2
$seekit && (seek($fh, 0, 0) || croak("$! seeking on temp file for child's stdin"))
$seekit seek $fh, 0, 0
Truth table0%0/3
A && B
A= $seekitB= seek $fh, 0, 0
ABresult
0X0
100
111
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 177 ▶sub _fh_for_child_output {
Subroutine _fh_for_child_output: called CC=9 SCAR=22.0
183 177 my ( $what, $type, $dest, $options ) = @_;
184  
185 177 my $fh;
186 177 ▶ if ( $type eq "SCALAR" && $dest == \undef ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'SCALAR' and $dest == \(undef)) { } 68 109 177
elsif ($type eq 'FH') { } 1 108 109
elsif (not $type) { } 2 106 108
Condition100%3/3
$type eq "SCALAR" and $dest == \(undef)
!ll&&!rl&&r
610368
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") 2 0 2
$$options{"append_$what"} ? : 1 1 2
203 or croak "$!: $dest";
204 } else {
205 106 warn "run3(): capturing child $what\n"
206 if debugging >= 2;
207  
208 106 ▶ $fh = $fh_cache{$what} ||= tempfile;
Condition66%2/3
$fh_cache{$what} ||= tempfile()
l!l&&r!l&&!r
88180
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 106 seek $fh, 0, 0;
210 106 truncate $fh, 0;
211 }
212  
213 177 my $binmode_it = $options->{"binmode_$what"};
214 177 _binmode($fh, $binmode_it, uc $what);
215  
216 177 return $fh;
217 }
218  
219 174 ▶sub _read_child_output_fh {
Subroutine _read_child_output_fh: called CC=12 SCAR=24.9
220 174 my ( $what, $type, $dest, $fh, $options ) = @_;
221  
222 174 ▶ return if $type eq "SCALAR" && $dest == \undef;
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "SCALAR" and $dest == \(undef) 68 106 174
Condition100%3/3
$type eq "SCALAR" and $dest == \(undef)
!ll&&!rl&&r
310368
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 106 ▶ 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") 106 0 106
225  
226 106 ▶ if ( $type eq "SCALAR" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'SCALAR') { } 103 3 106
elsif ($type eq 'ARRAY') { } 2 1 3
elsif ($type eq 'CODE') { } 1 0 1
227 103 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 103 ▶ $options->{"append_$what"} ? length $$dest : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$options{"append_$what"} ? : 2 101 103
234 103 while (1) {
235 186 ▶ croak "$! reading child $what from temp file"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 0 186 186
236 unless defined $count;
237  
238 186 ▶ last unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 103 83 186
239  
240 83 warn "run3(): read $count bytes from child $what",
241 debugging >= 3 ? ( ": '", substr( $$dest, -$count ), "'" ) : (),
242 "\n"
243 if debugging >= 2;
244  
245 83 $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 306 ▶sub _type {
Subroutine _type: called CC=3 SCAR=11.0
286 306 my ( $redir ) = @_;
287  
288 306 ▶ return "FH" if eval {
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { local $SIG{"__DIE__"}; $redir->isa("IO::Handle") } } 0 306 306
289 306 local $SIG{'__DIE__'};
290 306 $redir->isa("IO::Handle")
291 };
292  
293 306 my $type = ref $redir;
294 306 ▶ return $type eq "GLOB" ? "FH" : $type;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'GLOB' ? : 2 304 306
295 }
296  
297 20 ▶sub _max_fd {
Subroutine _max_fd: called CC=1 SCAR=0.0
298 20 my $fd = dup(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 103 ▶sub run3 {
Subroutine run3: called CC=51 SCAR=41.2
Pod documented
308 103 $run_call_time = gettimeofday() if profiling;
309  
310 103 ▶ my $options = @_ && ref $_[-1] eq "HASH" ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] eq 'HASH' ? : 9 94 103
Condition100%3/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
1939
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 103 my ( $cmd, $stdin, $stdout, $stderr ) = @_;
313  
314 103 print STDERR "run3(): running ",
315 join( " ", map "'$_'", ref $cmd ? @$cmd : $cmd ),
316 "\n"
317 if debugging;
318  
319 103 ▶ if ( ref $cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd) { } 102 1 103
320 102 ▶ croak "run3(): empty command" unless @$cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless @$cmd 0 102 102
321 102 ▶ croak "run3(): undefined command" unless defined $cmd->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmd->[0] 0 102 102
322 102 ▶ croak "run3(): command name ('')" unless length $cmd->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless length $cmd->[0] 0 102 102
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 102 foreach (qw/binmode_stdin binmode_stdout binmode_stderr/) {
330 306 ▶ if (my $mode = $options->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mode = $options->{$_}) 5 301 306
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 102 my $in_type = _type $stdin;
337 102 my $out_type = _type $stdout;
338 102 my $err_type = _type $stderr;
339  
340 102 ▶ if ($fh_cache_pid != $$) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh_cache_pid != $$) 6 96 102
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 102 my ($in_fh, $out_fh, $err_fh);
352 $in_fh = _spool_data_to_child $in_type, $stdin,
353 102 ▶ $options->{binmode_stdin} if defined $stdin;
Branch: 1 branch
BranchTrueFalseTotal
if defined $stdin 96 6 102
354  
355 102 ▶ $out_fh = _fh_for_child_output "stdout", $out_type, $stdout,
Branch: 1 branch
BranchTrueFalseTotal
if defined $stdout 95 7 102
356 $options if defined $stdout;
357  
358 102 ▶ my $tie_err_to_out =
Condition100%6/6
defined $stderr && defined $stdout && $stderr eq $stdout
!ll&&!rl&&r
19812
defined $stderr and defined $stdout
!ll&&!rl&&r
18183
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
359 defined $stderr && defined $stdout && $stderr eq $stdout;
360  
361 102 ▶ $err_fh = $tie_err_to_out
Branch: 2 branches
BranchTrueFalseTotal
if defined $stderr 84 18 102
$tie_err_to_out ? : 2 82 84
362 ? $out_fh
363 : _fh_for_child_output "stderr", $err_type, $stderr,
364 $options if defined $stderr;
365  
366 # this should make perl close these on exceptions
367 # local *STDIN_SAVE;
368 102 local *STDOUT_SAVE;
369 102 local *STDERR_SAVE;
370  
371 102 ▶ my $saved_fd0 = defined $in_fh ? dup( 0 ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $in_fh ? : 96 6 102
372  
373 # open STDIN_SAVE, "<&STDIN"# or croak "run3(): $! saving STDIN"
374 # if defined $in_fh;
375 102 ▶ open STDOUT_SAVE, ">&STDOUT" or croak "run3(): $! saving STDOUT"
Branch: 1 branch
BranchTrueFalseTotal
if defined $out_fh 95 7 102
Condition75%3/4
defined $out_fh && (open(STDOUT_SAVE, '>&STDOUT') || croak("run3(): $! saving STDOUT"))
!ll
795
open STDOUT_SAVE, ">&STDOUT" or croak("run3(): $! saving STDOUT")
l!l
950
MC/DC0%0/2
defined $out_fh && (open(STDOUT_SAVE, '>&STDOUT') || croak("run3(): $! saving STDOUT"))
defined $out_fh open STDOUT_SAVE, ">&STDOUT"
Truth table33%1/3
A && B
A= defined $out_fhB= open STDOUT_SAVE, ">&STDOUT"
ABresult
0X0
100
111
376 if defined $out_fh;
377 102 ▶ open STDERR_SAVE, ">&STDERR" or croak "run3(): $! saving STDERR"
Branch: 1 branch
BranchTrueFalseTotal
if defined $err_fh 84 18 102
Condition75%3/4
defined $err_fh && (open(STDERR_SAVE, '>&STDERR') || croak("run3(): $! saving STDERR"))
!ll
1884
open STDERR_SAVE, ">&STDERR" or croak("run3(): $! saving STDERR")
l!l
840
MC/DC0%0/2
defined $err_fh && (open(STDERR_SAVE, '>&STDERR') || croak("run3(): $! saving STDERR"))
defined $err_fh open STDERR_SAVE, ">&STDERR"
Truth table33%1/3
A && B
A= defined $err_fhB= open STDERR_SAVE, ">&STDERR"
ABresult
0X0
100
111
378 if defined $err_fh;
379  
380 102 my $errno;
381 102 my $ok = eval {
382 # The open() call here seems to not force fd 0 in some cases;
383 # I ran in to trouble when using this in VCP, not sure why.
384 # the dup2() seems to work.
385 102 ▶ dup2( fileno $in_fh, 0 )
Branch: 1 branch
BranchTrueFalseTotal
if defined $in_fh 96 6 102
Condition75%3/4
defined $in_fh && (dup2(fileno $in_fh, 0) || croak("run3(): $! redirecting STDIN"))
!ll
696
dup2(fileno $in_fh, 0) or croak("run3(): $! redirecting STDIN")
l!l
960
MC/DC0%0/2
defined $in_fh && (dup2(fileno $in_fh, 0) || croak("run3(): $! redirecting STDIN"))
defined $in_fh dup2(fileno $in_fh, 0)
Truth table33%1/3
A && B
A= defined $in_fhB= dup2(fileno $in_fh, 0)
ABresult
0X0
100
111
386 # open STDIN, "<&=" . fileno $in_fh
387 or croak "run3(): $! redirecting STDIN"
388 if defined $in_fh;
389  
390 # close $in_fh or croak "$! closing STDIN temp file"
391 # if ref $stdin;
392  
393 102 ▶ open STDOUT, ">&" . fileno $out_fh
Branch: 1 branch
BranchTrueFalseTotal
if defined $out_fh 95 7 102
Condition75%3/4
defined $out_fh && (open(STDOUT, '>&' . fileno($out_fh)) || croak("run3(): $! redirecting STDOUT"))
!ll
795
open STDOUT, '>&' . fileno($out_fh) or croak("run3(): $! redirecting STDOUT")
l!l
950
MC/DC0%0/2
defined $out_fh && (open(STDOUT, '>&' . fileno($out_fh)) || croak("run3(): $! redirecting STDOUT"))
defined $out_fh open STDOUT, '>&' . fileno($out_fh)
Truth table33%1/3
A && B
A= defined $out_fhB= open STDOUT, '>&' . fileno($out_fh)
ABresult
0X0
100
111
394 or croak "run3(): $! redirecting STDOUT"
395 if defined $out_fh;
396  
397 102 ▶ open STDERR, ">&" . fileno $err_fh
Branch: 1 branch
BranchTrueFalseTotal
if defined $err_fh 84 18 102
Condition75%3/4
defined $err_fh && (open(STDERR, '>&' . fileno($err_fh)) || croak("run3(): $! redirecting STDERR"))
!ll
1884
open STDERR, '>&' . fileno($err_fh) or croak("run3(): $! redirecting STDERR")
l!l
840
MC/DC0%0/2
defined $err_fh && (open(STDERR, '>&' . fileno($err_fh)) || croak("run3(): $! redirecting STDERR"))
defined $err_fh open STDERR, '>&' . fileno($err_fh)
Truth table33%1/3
A && B
A= defined $err_fhB= open STDERR, '>&' . fileno($err_fh)
ABresult
0X0
100
111
398 or croak "run3(): $! redirecting STDERR"
399 if defined $err_fh;
400  
401 102 $sys_call_time = gettimeofday() if profiling;
402  
403 102 $! = 0; # make sure we don't test below against some previous error
404  
405 my $r = ref $cmd
406 102 ▶ ? system { $cmd->[0] } is_win32 ? quote_native( @$cmd ) : @$cmd
Branch: 1 branch
BranchTrueFalseTotal
ref $cmd ? : 102 0 102
407 : system $cmd;
408  
409 102 $errno = $!; # save $!, because later failures will overwrite it
410 102 $sys_exit_time = gettimeofday() if profiling;
411 102 if ( debugging ) {
412 my $err_fh = defined $err_fh ? \*STDERR_SAVE : \*STDERR;
413 if ( defined $r && $r != -1 ) {
414 print $err_fh "run3(): \$? is $?\n";
415 } else {
416 print $err_fh "run3(): \$? is $?, \$! is $errno\n";
417 }
418 }
419  
420 102 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r and $r == -1 || !!0 and not $options->{"return_if_system_error"}) 0 102 102
Condition75%6/8
defined $r and $r == -1 || !!0 and not $options->{"return_if_system_error"}
!ll&&!rl&&r
10110
defined $r and $r == -1 || !!0
!ll&&!rl&&r
01011
$r == -1 or !!0
l!l
1101
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
421 defined $r
422 && ( $r == -1 || ( is_win32 && $r == 0xFF00 && $errno != 0 ) )
423 && !$options->{return_if_system_error}
424 ) {
425 0 croak( $errno );
426 }
427  
428 102 1;
429 };
430 102 my $x = $@;
431  
432 102 my @errs;
433  
434 102 ▶ if ( defined $saved_fd0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $saved_fd0) 96 6 102
435 96 dup2( $saved_fd0, 0 );
436 96 POSIX::close( $saved_fd0 );
437 }
438  
439 # open STDIN, "<&STDIN_SAVE"# or push @errs, "run3(): $! restoring STDIN"
440 # if defined $in_fh;
441 102 ▶ open STDOUT, ">&STDOUT_SAVE" or push @errs, "run3(): $! restoring STDOUT"
Branch: 1 branch
BranchTrueFalseTotal
if defined $out_fh 95 7 102
Condition60%3/5
defined $out_fh && (open(STDOUT, '>&STDOUT_SAVE') || push(@errs, "run3(): $! restoring STDOUT"))
!ll
795
open STDOUT, ">&STDOUT_SAVE" or push @errs, "run3(): $! restoring STDOUT"
l!l&&r!l&&!r
9500
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
442 if defined $out_fh;
443 102 ▶ open STDERR, ">&STDERR_SAVE" or push @errs, "run3(): $! restoring STDERR"
Branch: 1 branch
BranchTrueFalseTotal
if defined $err_fh 84 18 102
Condition60%3/5
defined $err_fh && (open(STDERR, '>&STDERR_SAVE') || push(@errs, "run3(): $! restoring STDERR"))
!ll
1884
open STDERR, ">&STDERR_SAVE" or push @errs, "run3(): $! restoring STDERR"
l!l&&r!l&&!r
8400
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
444 if defined $err_fh;
445  
446 102 ▶ croak join ", ", @errs if @errs;
Branch: 1 branch
BranchTrueFalseTotal
if @errs 0 102 102
447  
448 102 ▶ die $x unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 102 102
449  
450 102 ▶ _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" 92 10 102
Condition100%6/6
defined $out_fh and $out_type and $out_type ne "FH"
!ll&&!rl&&r
9192
defined $out_fh and $out_type
!ll&&!rl&&r
7293
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
451 if defined $out_fh && $out_type && $out_type ne "FH";
452 102 ▶ _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 82 20 102
Condition77%7/9
defined $err_fh and $err_type and $err_type ne "FH" and not $tie_err_to_out
!ll&&!rl&&r
18282
defined $err_fh and $err_type and $err_type ne "FH"
!ll&&!rl&&r
18084
defined $err_fh and $err_type
!ll&&!rl&&r
18084
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
453 if defined $err_fh && $err_type && $err_type ne "FH" && !$tie_err_to_out;
454 102 $profiler->run_exit(
455 $cmd,
456 $run_call_time,
457 $sys_call_time,
458 $sys_exit_time,
459 scalar gettimeofday()
460 ) if profiling;
461  
462 102 $! = $errno; # restore $! from system()
463  
464 102 return 1;
465 }
466  
467 12 1;
468  
469 __END__
↑ Summary Run3/ProfLogger.pm »