blib/lib/Capture/Tiny.pm

filter: Statementstmt 92.7% 242 / 261 Branchbran 80.6% 108 / 134 Conditioncond 70.5% 55 / 78 MC/DCmcdc 42.8% 18 / 42 Subroutinesub 94.4% 34 / 36 Podpod 100% 8 / 8 totaltotal 83.1% 465 / 559 CC 105 SCAR 34.6
CC
105
Cov
86%
_capture_tee
65.5
_kill_tees
17.0
_proxy_std
10.4
CRAP 32.0
↑ Summary
1 3306 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3306 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3306 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package Capture::Tiny;
5 # ABSTRACT: Capture STDOUT and STDERR from Perl, XS or external programs
6 3306 our $VERSION = '0.50';
7 3306 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3306 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3306 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3306 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3306 ▶use File::Temp qw/tempfile tmpnam/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3306 ▶use Scalar::Util qw/reftype blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 # Get PerlIO or fake it
14 3306 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.4
15 3306 local $@;
16 3306 eval { require PerlIO; PerlIO->can('get_layers') }
17 0 ▶ or *PerlIO::get_layers = sub { return () };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require PerlIO; "PerlIO"->can("get_layers") } } or *PerlIO::get_layers = sub { return (); } 3306 0 3306
18 }
19  
20 #--------------------------------------------------------------------------#
21 # create API subroutines and export them
22 # [do STDOUT flag, do STDERR flag, do merge flag, do tee flag]
23 #--------------------------------------------------------------------------#
24  
25 3306 my %api = (
26 capture => [1,1,0,0],
27 capture_stdout => [1,0,0,0],
28 capture_stderr => [0,1,0,0],
29 capture_merged => [1,1,1,0],
30 tee => [1,1,0,1],
31 tee_stdout => [1,0,0,1],
32 tee_stderr => [0,1,0,1],
33 tee_merged => [1,1,1,1],
34 );
35  
36 3306 for my $sub ( keys %api ) {
37 26448 my $args = join q{, }, @{$api{$sub}};
38 563047 ▶ eval "sub $sub(&;@) {unshift \@_, $args; goto \\&_capture_tee;}"; ## no critic
Subroutine capture: called CC=1 SCAR=0.0
Subroutine capture_merged: called CC=1 SCAR=0.0
Subroutine capture_stderr: called CC=1 SCAR=0.0
Subroutine capture_stdout: called CC=1 SCAR=0.0
Subroutine tee: called CC=1 SCAR=0.0
Subroutine tee_merged: called CC=1 SCAR=0.0
Subroutine tee_stderr: called CC=1 SCAR=0.0
Subroutine tee_stdout: called CC=1 SCAR=0.0
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
39 }
40  
41 3306 our @ISA = qw/Exporter/;
42 3306 our @EXPORT_OK = keys %api;
43 3306 our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
44  
45 #--------------------------------------------------------------------------#
46 # constants and fixtures
47 #--------------------------------------------------------------------------#
48  
49 3306 my $IS_WIN32 = $^O eq 'MSWin32';
50  
51 ##our $DEBUG = $ENV{PERL_CAPTURE_TINY_DEBUG};
52 ##
53 ##my $DEBUGFH;
54 ##open $DEBUGFH, "> DEBUG" if $DEBUG;
55 ##
56 ##*_debug = $DEBUG ? sub(@) { print {$DEBUGFH} @_ } : sub(){0};
57  
58 3306 our $TIMEOUT = 30;
59  
60 #--------------------------------------------------------------------------#
61 # command to tee output -- the argument is a filename that must
62 # be opened to signal that the process is ready to receive input.
63 # This is annoying, but seems to be the best that can be done
64 # as a simple, portable IPC technique
65 #--------------------------------------------------------------------------#
66 3306 my @cmd = ($^X, '-C0', '-e', <<'HERE');
67 use Fcntl;
68 $SIG{HUP}=sub{exit};
69 if ( my $fn=shift ) {
70 sysopen(my $fh, qq{$fn}, O_WRONLY|O_CREAT|O_EXCL) or die $!;
71 print {$fh} $$;
72 close $fh;
73 }
74 my $buf; while (sysread(STDIN, $buf, 2048)) {
75 syswrite(STDOUT, $buf); syswrite(STDERR, $buf);
76 }
77 HERE
78  
79 #--------------------------------------------------------------------------#
80 # filehandle manipulation
81 #--------------------------------------------------------------------------#
82  
83 5502797 ▶sub _relayer {
Subroutine _relayer: called CC=2 SCAR=6.9
84 5502797 my ($fh, $apply_layers) = @_;
85 # _debug("# requested layers (@{$layers}) for @{[fileno $fh]}\n");
86  
87 # eliminate pseudo-layers
88 5502797 binmode( $fh, ":raw" );
89 # strip off real layers until only :unix is left
90 5502797 ▶ while ( 1 < ( my $layers =()= PerlIO::get_layers( $fh, output => 1 ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 < (my $layers = () = &PerlIO::get_layers($fh, "output", 1))) 5502797 5502797 11005594
91 5502797 binmode( $fh, ":pop" );
92 }
93 # apply other layers
94 5502797 my @to_apply = @$apply_layers;
95 5502797 shift @to_apply; # eliminate initial :unix
96 # _debug("# applying layers (unix @to_apply) to @{[fileno $fh]}\n");
97 5502797 binmode($fh, ":" . join(":",@to_apply));
98 }
99  
100 0 ▶sub _name {
Subroutine _name: not called CC=1 SCAR=1.2
101 0 my $glob = shift;
102 3306 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
103 0 return *{$glob}{NAME};
104 }
105  
106 7485730 ▶sub _open {
Subroutine _open: called CC=2 SCAR=7.6
107 7485730 ▶ open $_[0], $_[1] or Carp::confess "Error from open(" . join(q{, }, @_) . "): $!";
Branch: 1 branch
BranchTrueFalseTotal
open $_[0], $_[1] or Carp::confess('Error from open(' . join(", ", @_) . "): $!") 7485730 0 7485730
108 # _debug( "# open " . join( ", " , map { defined $_ ? _name($_) : 'undef' } @_ ) . " as " . fileno( $_[0] ) . "\n" );
109 }
110  
111 3086260 ▶sub _close {
Subroutine _close: called CC=2 SCAR=7.6
112 # _debug( "# closing " . ( defined $_[0] ? _name($_[0]) : 'undef' ) . " on " . fileno( $_[0] ) . "\n" );
113 3086260 ▶ close $_[0] or Carp::confess "Error from close(" . join(q{, }, @_) . "): $!";
Branch: 1 branch
BranchTrueFalseTotal
close $_[0] or Carp::confess('Error from close(' . join(", ", @_) . "): $!") 3086260 0 3086260
114 }
115  
116 3306 my %dup; # cache this so STDIN stays fd0
117 my %proxy_count;
118 1044168 ▶sub _proxy_std {
Subroutine _proxy_std: called CC=10 SCAR=23.4
119 1044168 my %proxies;
120 1044168 ▶ if ( ! defined fileno STDIN ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined fileno STDIN) 138870 905298 1044168
121 138870 $proxy_count{stdin}++;
122 138870 ▶ if (defined $dup{stdin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dup{'stdin'}) { } 31830 107040 138870
123 31830 _open \*STDIN, "<&=" . fileno($dup{stdin});
124 # _debug( "# restored proxy STDIN as " . (defined fileno STDIN ? fileno STDIN : 'undef' ) . "\n" );
125 }
126 else {
127 107040 _open \*STDIN, "<" . File::Spec->devnull;
128 # _debug( "# proxied STDIN as " . (defined fileno STDIN ? fileno STDIN : 'undef' ) . "\n" );
129 107040 _open $dup{stdin} = IO::Handle->new, "<&=STDIN";
130 }
131 138870 $proxies{stdin} = \*STDIN;
132 138870 ▶ binmode(STDIN, ':utf8') if $] >= 5.008; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008" 138870 0 138870
133 }
134 1044168 ▶ if ( ! defined fileno STDOUT ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined fileno STDOUT) 107040 937128 1044168
135 107040 $proxy_count{stdout}++;
136 107040 ▶ if (defined $dup{stdout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dup{'stdout'}) { } 0 107040 107040
137 0 _open \*STDOUT, ">&=" . fileno($dup{stdout});
138 # _debug( "# restored proxy STDOUT as " . (defined fileno STDOUT ? fileno STDOUT : 'undef' ) . "\n" );
139 }
140 else {
141 107040 _open \*STDOUT, ">" . File::Spec->devnull;
142 # _debug( "# proxied STDOUT as " . (defined fileno STDOUT ? fileno STDOUT : 'undef' ) . "\n" );
143 107040 _open $dup{stdout} = IO::Handle->new, ">&=STDOUT";
144 }
145 107040 $proxies{stdout} = \*STDOUT;
146 107040 ▶ binmode(STDOUT, ':utf8') if $] >= 5.008; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008" 107040 0 107040
147 }
148 1044168 ▶ if ( ! defined fileno STDERR ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined fileno STDERR) 107040 937128 1044168
149 107040 $proxy_count{stderr}++;
150 107040 ▶ if (defined $dup{stderr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dup{'stderr'}) { } 0 107040 107040
151 0 _open \*STDERR, ">&=" . fileno($dup{stderr});
152 # _debug( "# restored proxy STDERR as " . (defined fileno STDERR ? fileno STDERR : 'undef' ) . "\n" );
153 }
154 else {
155 107040 _open \*STDERR, ">" . File::Spec->devnull;
156 # _debug( "# proxied STDERR as " . (defined fileno STDERR ? fileno STDERR : 'undef' ) . "\n" );
157 107040 _open $dup{stderr} = IO::Handle->new, ">&=STDERR";
158 }
159 107040 $proxies{stderr} = \*STDERR;
160 107040 ▶ binmode(STDERR, ':utf8') if $] >= 5.008; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008" 107040 0 107040
161 }
162 1044168 return %proxies;
163 }
164  
165 1037603 ▶sub _unproxy {
Subroutine _unproxy: called CC=4 SCAR=13.9
166 1037603 my (%proxies) = @_;
167 # _debug( "# unproxying: " . join(" ", keys %proxies) . "\n" );
168 1037603 for my $p ( keys %proxies ) {
169 351270 $proxy_count{$p}--;
170 # _debug( "# unproxied " . uc($p) . " ($proxy_count{$p} left)\n" );
171 351270 ▶ if ( ! $proxy_count{$p} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($proxy_count{$p}) 319680 31590 351270
172 319680 _close $proxies{$p};
173 319680 ▶ _close $dup{$p} unless $] < 5.008; # 5.6 will have already closed this as dup
Branch: 1 branch
BranchTrueFalseTotal
unless $] < "5.008" 319680 0 319680
174 319680 delete $dup{$p};
175 }
176 }
177 }
178  
179 1044168 ▶sub _copy_std {
Subroutine _copy_std: called CC=5 SCAR=16.3
180 1044168 my %handles;
181 1044168 for my $h ( qw/stdout stderr stdin/ ) {
182 3132504 ▶ next if $h eq 'stdin' && ! $IS_WIN32; # WIN32 hangs on tee without STDIN copied
Branch: 1 branch
BranchTrueFalseTotal
if $h eq "stdin" and not $IS_WIN32 1044168 2088336 3132504
Condition66%2/3
$h eq "stdin" and not $IS_WIN32
!ll&&!rl&&r
208833601044168
MC/DC50%1/2
$h eq "stdin" and not $IS_WIN32
$h eq "stdin" not $IS_WIN32
Truth table66%2/3
A and not(B)
A= $h eq "stdin"B= not $IS_WIN32
ABresult
0X0
100
111
183 2088336 ▶ my $redir = $h eq 'stdin' ? "<&" : ">&";
Branch: 1 branch
BranchTrueFalseTotal
$h eq 'stdin' ? : 0 2088336 2088336
184 2088336 _open $handles{$h} = IO::Handle->new(), $redir . uc($h); # ">&STDOUT" or "<&STDIN"
185 }
186 1044168 return \%handles;
187 }
188  
189 # In some cases we open all (prior to forking) and in others we only open
190 # the output handles (setting up redirection)
191 2081771 ▶sub _open_std {
Subroutine _open_std: called CC=4 SCAR=14.2
192 2081771 my ($handles) = @_;
193 2081771 ▶ _open \*STDIN, "<&" . fileno $handles->{stdin} if defined $handles->{stdin};
Branch: 1 branch
BranchTrueFalseTotal
if defined $handles->{"stdin"} 3282 2078489 2081771
194 2081771 ▶ _open \*STDOUT, ">&" . fileno $handles->{stdout} if defined $handles->{stdout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $handles->{"stdout"} 2081771 0 2081771
195 2081771 ▶ _open \*STDERR, ">&" . fileno $handles->{stderr} if defined $handles->{stderr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $handles->{"stderr"} 2081771 0 2081771
196 }
197  
198 #--------------------------------------------------------------------------#
199 # private subs
200 #--------------------------------------------------------------------------#
201  
202 372925 ▶sub _start_tee {
Subroutine _start_tee: called CC=3 SCAR=13.8
203 372925 my ($which, $stash) = @_; # $which is "stdout" or "stderr"
204 # setup pipes
205 372925 $stash->{$_}{$which} = IO::Handle->new for qw/tee reader/;
206 372925 pipe $stash->{reader}{$which}, $stash->{tee}{$which};
207 # _debug( "# pipe for $which\: " . _name($stash->{tee}{$which}) . " " . fileno( $stash->{tee}{$which} ) . " => " . _name($stash->{reader}{$which}) . " " . fileno( $stash->{reader}{$which}) . "\n" );
208 372925 select((select($stash->{tee}{$which}), $|=1)[0]); # autoflush
209 # setup desired redirection for parent and child
210 372925 $stash->{new}{$which} = $stash->{tee}{$which};
211 $stash->{child}{$which} = {
212 stdin => $stash->{reader}{$which},
213 stdout => $stash->{old}{$which},
214 372925 stderr => $stash->{capture}{$which},
215 };
216 # flag file is used to signal the child is ready
217 372925 $stash->{flag_files}{$which} = scalar( tmpnam() ) . $$;
218 # execute @cmd as a separate process
219 372925 ▶ if ( $IS_WIN32 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IS_WIN32) { } 0 372925 372925
220 0 my $old_eval_err=$@;
221 0 undef $@;
222  
223 0 eval "use Win32API::File qw/GetOsFHandle SetHandleInformation fileLastError HANDLE_FLAG_INHERIT INVALID_HANDLE_VALUE/ ";
224 # _debug( "# Win32API::File loaded\n") unless $@;
225 0 my $os_fhandle = GetOsFHandle( $stash->{tee}{$which} );
226 # _debug( "# Couldn't get OS handle: " . fileLastError() . "\n") if ! defined $os_fhandle || $os_fhandle == INVALID_HANDLE_VALUE();
227 0 my $result = SetHandleInformation( $os_fhandle, HANDLE_FLAG_INHERIT(), 0);
228 # _debug( $result ? "# set no-inherit flag on $which tee\n" : ("# can't disable tee handle flag inherit: " . fileLastError() . "\n"));
229 0 _open_std( $stash->{child}{$which} );
230 0 $stash->{pid}{$which} = system(1, @cmd, $stash->{flag_files}{$which});
231 # not restoring std here as it all gets redirected again shortly anyway
232 0 $@=$old_eval_err;
233 }
234 else { # use fork
235 372925 _fork_exec( $which, $stash );
236 }
237 }
238  
239 372925 ▶sub _fork_exec {
Subroutine _fork_exec: called CC=3 SCAR=11.1
240 372925 my ($which, $stash) = @_; # $which is "stdout" or "stderr"
241 372925 my $pid = fork;
242 372925 ▶ if ( not defined $pid ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 372925 372925
elsif ($pid == 0) { } 3282 369643 372925
243 0 Carp::confess "Couldn't fork(): $!";
244 }
245 elsif ($pid == 0) { # child
246 # _debug( "# in child process ...\n" );
247 3282 untie *STDIN; untie *STDOUT; untie *STDERR;
248 3282 _close $stash->{tee}{$which};
249 # _debug( "# redirecting handles in child ...\n" );
250 3282 _open_std( $stash->{child}{$which} );
251 # _debug( "# calling exec on command ...\n" );
252 3282 exec @cmd, $stash->{flag_files}{$which};
253 }
254 369643 $stash->{pid}{$which} = $pid
255 }
256  
257 3306 ▶my $have_usleep = eval "use Time::HiRes 'usleep'; 1";
Subroutine BEGIN: called CC=1 SCAR=0.0
258 149891869 ▶sub _files_exist {
Subroutine _files_exist: called CC=3 SCAR=11.0
259 149891869 ▶ return 1 if @_ == grep { -f } @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == grep({-f $_;} @_) 437822 149454047 149891869
260 149454047 ▶ Time::HiRes::usleep(1000) if $have_usleep;
Branch: 1 branch
BranchTrueFalseTotal
if $have_usleep 149454047 0 149454047
261 149454047 return 0;
262 }
263  
264 218911 ▶sub _wait_for_tees {
Subroutine _wait_for_tees: called CC=7 SCAR=20.5
265 218911 my ($stash) = @_;
266 218911 my $start = time;
267 218911 my @files = values %{$stash->{flag_files}};
268 my $timeout = defined $ENV{PERL_CAPTURE_TINY_TIMEOUT}
269 218911 ▶ ? $ENV{PERL_CAPTURE_TINY_TIMEOUT} : $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'PERL_CAPTURE_TINY_TIMEOUT'} ? : 218911 0 218911
270 218911 ▶ 1 until _files_exist(@files) || ($timeout && (time - $start > $timeout));
Branch: 1 branch
BranchTrueFalseTotal
(_files_exist(@files) or $timeout and time - $start > $timeout) or do { '???' } 218911 149454047 149672958
Condition50%3/6
_files_exist(@files) or $timeout and time - $start > $timeout
l!l&&r!l&&!r
2189110149454047
$timeout and time - $start > $timeout
!ll&&!rl&&r
14945404700
MC/DC33%1/3
_files_exist(@files) or $timeout and time - $start > $timeout
_files_exist(@files) $timeout time - $start > $timeout
Truth table50%2/4
A or B and C
A= _files_exist(@files)B= $timeoutC= time - $start > $timeout
ABCresult
1XX1
0111
00X0
0100
271 218911 ▶ Carp::confess "Timed out waiting for subprocesses to start" if ! _files_exist(@files);
Branch: 1 branch
BranchTrueFalseTotal
unless _files_exist(@files) 0 218911 218911
272 218911 unlink $_ for @files;
273 }
274  
275 218911 ▶sub _kill_tees {
Subroutine _kill_tees: called CC=7 SCAR=28.3
276 218911 my ($stash) = @_;
277 218911 ▶ if ( $IS_WIN32 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IS_WIN32) { } 0 218911 218911
278 # _debug( "# closing handles\n");
279 0 close($_) for values %{ $stash->{tee} };
280 # _debug( "# waiting for subprocesses to finish\n");
281 0 my $start = time;
282 0 ▶ 1 until wait == -1 || (time - $start > 30);
Branch: 1 branch
BranchTrueFalseTotal
(wait == -1 or time - $start > 30) or do { 1 } 0 0 0
Condition0%0/3
wait == -1 or time - $start > 30
l!l&&r!l&&!r
000
MC/DC0%0/2
wait == -1 or time - $start > 30
wait == -1 time - $start > 30
Truth table0%0/3
A or B
A= wait == -1B= time - $start > 30
ABresult
1X1
011
000
283 }
284 else {
285 218911 _close $_ for values %{ $stash->{tee} };
286 218911 waitpid $_, 0 for values %{ $stash->{pid} };
287 }
288 }
289  
290 1832073 ▶sub _slurp {
Subroutine _slurp: called CC=3 SCAR=11.0
291 1832073 my ($name, $stash) = @_;
292 1832073 my ($fh, $pos) = map { $stash->{$_}{$name} } qw/capture pos/;
293 # _debug( "# slurping captured $name from " . fileno($fh) . " at pos $pos with layers: @{[PerlIO::get_layers($fh)]}\n");
294 1832073 ▶ seek( $fh, $pos, 0 ) or die "Couldn't seek on capture handle for $name\n";
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $pos, 0 or die "Couldn't seek on capture handle for $name\n" 1832073 0 1832073
295 1832073 my $text = do { local $/; scalar readline $fh };
296 1832073 ▶ return defined($text) ? $text : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 1832071 2 1832073
297 }
298  
299 #--------------------------------------------------------------------------#
300 # _capture_tee() -- generic main sub for capturing or teeing
301 #--------------------------------------------------------------------------#
302  
303 1044168 ▶sub _capture_tee {
Subroutine _capture_tee: called CC=62 SCAR=41.8
304 # _debug( "# starting _capture_tee with (@_)...\n" );
305 1044168 my ($do_stdout, $do_stderr, $do_merge, $do_tee, $code, @opts) = @_;
306 1044168 ▶ my %do = ($do_stdout ? (stdout => 1) : (), $do_stderr ? (stderr => 1) : ());
Branch: 2 branches
BranchTrueFalseTotal
$do_stdout ? : 926297 117871 1044168
$do_stderr ? : 918092 126076 1044168
307 1044168 ▶ Carp::confess("Custom capture options must be given as key/value pairs\n")
Branch: 1 branch
BranchTrueFalseTotal
unless @opts % 2 == 0 0 1044168 1044168
308 unless @opts % 2 == 0;
309 1044168 my $stash = { capture => { @opts } };
310 1044168 for ( keys %{$stash->{capture}} ) {
311 10 my $fh = $stash->{capture}{$_};
312 10 ▶ Carp::confess "Custom handle for $_ must be seekable\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fh eq "GLOB" or builtin::blessed($fh) and $fh->isa("IO::Seekable") 0 10 10
Condition33%2/6
ref $fh eq "GLOB" or builtin::blessed($fh) and $fh->isa("IO::Seekable")
l!l&&r!l&&!r
0100
builtin::blessed($fh) and $fh->isa("IO::Seekable")
!ll&&!rl&&r
0010
MC/DC0%0/3
ref $fh eq "GLOB" or builtin::blessed($fh) and $fh->isa("IO::Seekable")
ref $fh eq "GLOB" builtin::blessed($fh) $fh->isa("IO::Seekable")
Truth table25%1/4
A or B and C
A= ref $fh eq "GLOB"B= builtin::blessed($fh)C= $fh->isa("IO::Seekable")
ABCresult
1XX1
0111
00X0
0100
313 unless ref($fh) eq 'GLOB' || (blessed($fh) && $fh->isa("IO::Seekable"));
314 }
315 # save existing filehandles and setup captures
316 1044168 local *CT_ORIG_STDIN = *STDIN ;
317 1044168 local *CT_ORIG_STDOUT = *STDOUT;
318 1044168 local *CT_ORIG_STDERR = *STDERR;
319 # find initial layers
320 1044168 my %layers = (
321 stdin => [PerlIO::get_layers(\*STDIN) ],
322 stdout => [PerlIO::get_layers(\*STDOUT, output => 1)],
323 stderr => [PerlIO::get_layers(\*STDERR, output => 1)],
324 );
325 # _debug( "# existing layers for $_\: @{$layers{$_}}\n" ) for qw/stdin stdout stderr/;
326 # get layers from underlying glob of tied filehandles if we can
327 # (this only works for things that work like Tie::StdHandle)
328 1044168 ▶ $layers{stdout} = [PerlIO::get_layers(tied *STDOUT)]
Branch: 1 branch
BranchTrueFalseTotal
if tied *STDOUT and builtin::reftype(tied *STDOUT) eq "GLOB" 107040 937128 1044168
Condition100%3/3
tied *STDOUT and builtin::reftype(tied *STDOUT) eq "GLOB"
!ll&&!rl&&r
88936847760107040
MC/DC100%2/2
tied *STDOUT and builtin::reftype(tied *STDOUT) eq "GLOB"
tied *STDOUT builtin::reftype(tied *STDOUT) eq "GLOB"
Truth table100%3/3
A and B
A= tied *STDOUTB= builtin::reftype(tied *STDOUT) eq "GLOB"
ABresult
0X0
100
111
329 if tied(*STDOUT) && (reftype tied *STDOUT eq 'GLOB');
330 1044168 ▶ $layers{stderr} = [PerlIO::get_layers(tied *STDERR)]
Branch: 1 branch
BranchTrueFalseTotal
if tied *STDERR and builtin::reftype(tied *STDERR) eq "GLOB" 107040 937128 1044168
Condition100%3/3
tied *STDERR and builtin::reftype(tied *STDERR) eq "GLOB"
!ll&&!rl&&r
88936847760107040
MC/DC100%2/2
tied *STDERR and builtin::reftype(tied *STDERR) eq "GLOB"
tied *STDERR builtin::reftype(tied *STDERR) eq "GLOB"
Truth table100%3/3
A and B
A= tied *STDERRB= builtin::reftype(tied *STDERR) eq "GLOB"
ABresult
0X0
100
111
331 if tied(*STDERR) && (reftype tied *STDERR eq 'GLOB');
332 # _debug( "# tied object corrected layers for $_\: @{$layers{$_}}\n" ) for qw/stdin stdout stderr/;
333 # bypass scalar filehandles and tied handles
334 # localize scalar STDIN to get a proxy to pick up FD0, then restore later to CT_ORIG_STDIN
335 1044168 my %localize;
336 $localize{stdin}++, local(*STDIN)
337 1044168 ▶ if grep { $_ eq 'scalar' } @{$layers{stdin}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "scalar";} @{$layers{"stdin"};} 85350 958818 1044168
338 $localize{stdout}++, local(*STDOUT)
339 1044168 ▶ if $do_stdout && grep { $_ eq 'scalar' } @{$layers{stdout}};
Branch: 1 branch
BranchTrueFalseTotal
if $do_stdout and grep {$_ eq "scalar";} @{$layers{"stdout"};} 53520 990648 1044168
Condition100%3/3
$do_stdout and grep {$_ eq "scalar";} @{$layers{"stdout"};}
!ll&&!rl&&r
11787187277753520
MC/DC100%2/2
$do_stdout and grep {$_ eq "scalar";} @{$layers{"stdout"};}
$do_stdout grep {$_ eq "scalar";} @{$layers{"stdout"};}
Truth table100%3/3
A and B
A= $do_stdoutB= grep {$_ eq "scalar";} @{$layers{"stdout"};}
ABresult
0X0
100
111
340 $localize{stderr}++, local(*STDERR)
341 1044168 ▶ if ($do_stderr || $do_merge) && grep { $_ eq 'scalar' } @{$layers{stderr}};
Branch: 1 branch
BranchTrueFalseTotal
if $do_stderr || $do_merge and grep {$_ eq "scalar";} @{$layers{"stderr"};} 53520 990648 1044168
Condition83%5/6
$do_stderr || $do_merge and grep {$_ eq "scalar";} @{$layers{"stderr"};}
!ll&&!rl&&r
12607686457253520
$do_stderr or $do_merge
l!l&&r!l&&!r
9180920126076
MC/DC66%2/3
$do_stderr || $do_merge and grep {$_ eq "scalar";} @{$layers{"stderr"};}
$do_stderr $do_merge grep {$_ eq "scalar";} @{$layers{"stderr"};}
Truth table60%3/5
A or B and C
A= $do_stderrB= $do_mergeC= grep {$_ eq "scalar";} @{$layers{"stderr"};}
ABCresult
00X0
1X00
0100
1X11
0111
342 1044168 ▶ $localize{stdin}++, local(*STDIN), _open( \*STDIN, "<&=0")
Branch: 1 branch
BranchTrueFalseTotal
if tied *STDIN and $] >= "5.008" 246900 797268 1044168
Condition66%2/3
tied *STDIN and $] >= "5.008"
!ll&&!rl&&r
7972680246900
MC/DC50%1/2
tied *STDIN and $] >= "5.008"
tied *STDIN $] >= "5.008"
Truth table66%2/3
A and B
A= tied *STDINB= $] >= "5.008"
ABresult
0X0
100
111
343 if tied *STDIN && $] >= 5.008;
344 1044168 ▶ $localize{stdout}++, local(*STDOUT), _open( \*STDOUT, ">&=1")
Branch: 1 branch
BranchTrueFalseTotal
if $do_stdout and tied *STDOUT and $] >= "5.008" 154800 889368 1044168
Condition83%5/6
$do_stdout and tied *STDOUT and $] >= "5.008"
!ll&&!rl&&r
8893680154800
$do_stdout and tied *STDOUT
!ll&&!rl&&r
117871771497154800
MC/DC66%2/3
$do_stdout and tied *STDOUT and $] >= "5.008"
$do_stdout tied *STDOUT $] >= "5.008"
Truth table75%3/4
A and B and C
A= $do_stdoutB= tied *STDOUTC= $] >= "5.008"
ABCresult
0XX0
10X0
1100
1111
345 if $do_stdout && tied *STDOUT && $] >= 5.008;
346 1044168 ▶ $localize{stderr}++, local(*STDERR), _open( \*STDERR, ">&=2")
Branch: 1 branch
BranchTrueFalseTotal
if $do_stderr || $do_merge and tied *STDERR and $] >= "5.008" 154800 889368 1044168
Condition77%7/9
$do_stderr || $do_merge and tied *STDERR and $] >= "5.008"
!ll&&!rl&&r
8893680154800
$do_stderr || $do_merge and tied *STDERR
!ll&&!rl&&r
126076763292154800
$do_stderr or $do_merge
l!l&&r!l&&!r
9180920126076
MC/DC50%2/4
$do_stderr || $do_merge and tied *STDERR and $] >= "5.008"
$do_stderr $do_merge tied *STDERR $] >= "5.008"
Truth table42%3/7
A or B and C and D
A= $do_stderrB= $do_mergeC= tied *STDERRD= $] >= "5.008"
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
347 if ($do_stderr || $do_merge) && tied *STDERR && $] >= 5.008;
348 # _debug( "# localized $_\n" ) for keys %localize;
349 # proxy any closed/localized handles so we don't use fds 0, 1 or 2
350 1044168 my %proxy_std = _proxy_std();
351 # _debug( "# proxy std: @{ [%proxy_std] }\n" );
352 # update layers after any proxying
353 1044168 ▶ $layers{stdout} = [PerlIO::get_layers(\*STDOUT, output => 1)] if $proxy_std{stdout};
Branch: 1 branch
BranchTrueFalseTotal
if $proxy_std{"stdout"} 107040 937128 1044168
354 1044168 ▶ $layers{stderr} = [PerlIO::get_layers(\*STDERR, output => 1)] if $proxy_std{stderr};
Branch: 1 branch
BranchTrueFalseTotal
if $proxy_std{"stderr"} 107040 937128 1044168
355 # _debug( "# post-proxy layers for $_\: @{$layers{$_}}\n" ) for qw/stdin stdout stderr/;
356 # store old handles and setup handles for capture
357 1044168 $stash->{old} = _copy_std();
358 1044168 $stash->{new} = { %{$stash->{old}} }; # default to originals
359 1044168 for ( keys %do ) {
360 1843158 ▶ $stash->{new}{$_} = ($stash->{capture}{$_} ||= File::Temp->new);
Condition66%2/3
$stash->{"capture"}{$_} ||= "File::Temp"->new
l!l&&r!l&&!r
1018431480
MC/DC0%0/2
$stash->{"capture"}{$_} ||= "File::Temp"->new
$stash->{"capture"}{$_} "File::Temp"->new
Truth table66%2/3
A ||= B
A= $stash->{"capture"}{$_}B= "File::Temp"->new
ABresult
1X1
011
000
361 1843158 ▶ seek( $stash->{capture}{$_}, 0, 2 ) or die "Could not seek on capture handle for $_\n";
Branch: 1 branch
BranchTrueFalseTotal
seek $stash->{"capture"}{$_}, 0, 2 or die "Could not seek on capture handle for $_\n" 1843158 0 1843158
362 1843158 $stash->{pos}{$_} = tell $stash->{capture}{$_};
363 # _debug("# will capture $_ on " . fileno($stash->{capture}{$_})."\n" );
364 1843158 ▶ _start_tee( $_ => $stash ) if $do_tee; # tees may change $stash->{new}
Branch: 1 branch
BranchTrueFalseTotal
if $do_tee 372925 1470233 1843158
365 }
366 1040886 ▶ _wait_for_tees( $stash ) if $do_tee;
Branch: 1 branch
BranchTrueFalseTotal
if $do_tee 218911 821975 1040886
367 # finalize redirection
368 1040886 ▶ $stash->{new}{stderr} = $stash->{new}{stdout} if $do_merge;
Branch: 1 branch
BranchTrueFalseTotal
if $do_merge 96711 944175 1040886
369 # _debug( "# redirecting in parent ...\n" );
370 1040886 _open_std( $stash->{new} );
371 # execute user provided code
372 1040886 my ($exit_code, $inner_error, $outer_error, $orig_pid, @result);
373 {
374 1040886 $orig_pid = $$;
375 1040886 ▶ local *STDIN = *CT_ORIG_STDIN if $localize{stdin}; # get original, not proxy STDIN
Branch: 1 branch
BranchTrueFalseTotal
if $localize{"stdin"} 331210 709676 1040886
376 # _debug( "# finalizing layers ...\n" );
377 1040886 ▶ _relayer(\*STDOUT, $layers{stdout}) if $do_stdout;
Branch: 1 branch
BranchTrueFalseTotal
if $do_stdout 923425 117461 1040886
378 1040886 ▶ _relayer(\*STDERR, $layers{stderr}) if $do_stderr;
Branch: 1 branch
BranchTrueFalseTotal
if $do_stderr 915220 125666 1040886
379 # _debug( "# running code $code ...\n" );
380 1040886 my $old_eval_err=$@;
381 1040886 undef $@;
382 1040886 eval { @result = $code->(); $inner_error = $@ };
383 1037603 $exit_code = $?; # save this for later
384 1037603 $outer_error = $@; # save this for later
385 1037603 ▶ STDOUT->flush if $do_stdout;
Branch: 1 branch
BranchTrueFalseTotal
if $do_stdout 920142 117461 1037603
386 1037603 ▶ STDERR->flush if $do_stderr;
Branch: 1 branch
BranchTrueFalseTotal
if $do_stderr 911937 125666 1037603
387 1037603 $@ = $old_eval_err;
388 }
389 # restore prior filehandles and shut down tees
390 # _debug( "# restoring filehandles ...\n" );
391 1037603 _open_std( $stash->{old} );
392 1037603 _close( $_ ) for values %{$stash->{old}}; # don't leak fds
393 # shouldn't need relayering originals, but see rt.perl.org #114404
394 1037603 ▶ _relayer(\*STDOUT, $layers{stdout}) if $do_stdout;
Branch: 1 branch
BranchTrueFalseTotal
if $do_stdout 920142 117461 1037603
395 1037603 ▶ _relayer(\*STDERR, $layers{stderr}) if $do_stderr;
Branch: 1 branch
BranchTrueFalseTotal
if $do_stderr 911937 125666 1037603
396 1037603 _unproxy( %proxy_std );
397 # _debug( "# killing tee subprocesses ...\n" ) if $do_tee;
398 1037603 ▶ _kill_tees( $stash ) if $do_tee;
Branch: 1 branch
BranchTrueFalseTotal
if $do_tee 218911 818692 1037603
399 # return captured output, but shortcut in void context
400 # unless we have to echo output to tied/scalar handles;
401 1037603 my %got;
402 1037603 ▶ if ( $orig_pid == $$ and ( defined wantarray or ($do_tee && keys %localize) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_pid == $$ and defined wantarray || $do_tee && keys %localize) 1037600 3 1037603
Condition55%5/9
$orig_pid == $$ and defined wantarray || $do_tee && keys %localize
!ll&&!rl&&r
031037600
defined wantarray or $do_tee and keys %localize
l!l&&r!l&&!r
103760003
$do_tee and keys %localize
!ll&&!rl&&r
300
MC/DC25%1/4
$orig_pid == $$ and defined wantarray || $do_tee && keys %localize
$orig_pid == $$ defined wantarray $do_tee keys %localize
Truth table40%2/5
A and B or C and D
A= $orig_pid == $$B= defined wantarrayC= $do_teeD= keys %localize
ABCDresult
0XXX0
100X0
10100
11XX1
10111
403 1037600 for ( keys %do ) {
404 1832073 _relayer($stash->{capture}{$_}, $layers{$_});
405 1832073 $got{$_} = _slurp($_, $stash);
406 # _debug("# slurped " . length($got{$_}) . " bytes from $_\n");
407 }
408 print CT_ORIG_STDOUT $got{stdout}
409 1037600 ▶ if $do_stdout && $do_tee && $localize{stdout};
Branch: 1 branch
BranchTrueFalseTotal
if $do_stdout and $do_tee and $localize{"stdout"} 0 1037600 1037600
Condition83%5/6
$do_stdout and $do_tee and $localize{"stdout"}
!ll&&!rl&&r
8476441899560
$do_stdout and $do_tee
!ll&&!rl&&r
117461730183189956
MC/DC0%0/3
$do_stdout and $do_tee and $localize{"stdout"}
$do_stdout $do_tee $localize{"stdout"}
Truth table75%3/4
A and B and C
A= $do_stdoutB= $do_teeC= $localize{"stdout"}
ABCresult
0XX0
10X0
1100
1111
410 print CT_ORIG_STDERR $got{stderr}
411 1037600 ▶ if $do_stderr && $do_tee && $localize{stderr};
Branch: 1 branch
BranchTrueFalseTotal
if $do_stderr and $do_tee and $localize{"stderr"} 0 1037600 1037600
Condition83%5/6
$do_stderr and $do_tee and $localize{"stderr"}
!ll&&!rl&&r
8591441784560
$do_stderr and $do_tee
!ll&&!rl&&r
125666733478178456
MC/DC0%0/3
$do_stderr and $do_tee and $localize{"stderr"}
$do_stderr $do_tee $localize{"stderr"}
Truth table75%3/4
A and B and C
A= $do_stderrB= $do_teeC= $localize{"stderr"}
ABCresult
0XX0
10X0
1100
1111
412 }
413 1037603 $? = $exit_code;
414 1037603 ▶ $@ = $inner_error if $inner_error;
Branch: 1 branch
BranchTrueFalseTotal
if $inner_error 1 1037602 1037603
415 1037603 ▶ die $outer_error if $outer_error;
Branch: 1 branch
BranchTrueFalseTotal
if $outer_error 1 1037602 1037603
416 # _debug( "# ending _capture_tee with (@_)...\n" );
417 1037602 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 3 1037599 1037602
418 1037599 my @return;
419 1037599 ▶ push @return, $got{stdout} if $do_stdout;
Branch: 1 branch
BranchTrueFalseTotal
if $do_stdout 920138 117461 1037599
420 1037599 ▶ push @return, $got{stderr} if $do_stderr && ! $do_merge;
Branch: 1 branch
BranchTrueFalseTotal
if $do_stderr and not $do_merge 815222 222377 1037599
Condition100%3/3
$do_stderr and not $do_merge
!ll&&!rl&&r
12566696711815222
MC/DC100%2/2
$do_stderr and not $do_merge
$do_stderr not $do_merge
Truth table100%3/3
A and not(B)
A= $do_stderrB= not $do_merge
ABresult
0X0
100
111
421 1037599 push @return, @result;
422 1037599 ▶ return wantarray ? @return : $return[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 555053 482546 1037599
423 }
424  
425 3306 1;
426  
427 __END__
↑ Summary