blib/lib/IO/CaptureOutput.pm

filter: Statementstmt 93.8% 121 / 129 Branchbran 86.6% 52 / 60 Conditioncond 92.3% 60 / 65 MC/DCmcdc 77.4% 24 / 31 Subroutinesub 95.6% 22 / 23 Podpod 100% 3 / 3 totaltotal 90.6% 282 / 311 CC 54 SCAR 22.9
CC
54
Cov
92%
_capture
18.0
_validate
13.0
_shell_quote_win32
12.0
CRAP 9.9
↑ Summary /dev/null »
1 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package IO::CaptureOutput;
5 # ABSTRACT: capture STDOUT and STDERR from Perl code, subprocesses or XS
6  
7 6 our $VERSION = '1.1104';
8  
9 6 ▶use vars qw/@ISA @EXPORT_OK %EXPORT_TAGS $CarpLevel/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 @ISA = 'Exporter';
13 6 @EXPORT_OK = qw/capture capture_exec qxx capture_exec_combined qxy/;
14 6 %EXPORT_TAGS = (all => \@EXPORT_OK);
15 6 $CarpLevel = 0; # help capture report errors at the right level
16  
17 sub _capture (&@) { ## no critic
18 62 ▶ my ($code, $output, $error, $output_file, $error_file) = @_;
Subroutine _capture: called CC=18 SCAR=28.9
19  
20 # check for valid combinations of input
21 {
22 62 local $Carp::CarpLevel = 1;
23 62 my $error = _validate($output, $error, $output_file, $error_file);
24 62 ▶ croak $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 2 60 62
25 }
26  
27 # if either $output or $error are defined, then we need a variable for
28 # results; otherwise we only capture to files and don't waste memory
29 60 ▶ if ( defined $output || defined $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output or defined $error) 50 10 60
Condition100%3/3
defined $output or defined $error
l!l&&r!l&&!r
46410
MC/DC100%2/2
defined $output or defined $error
defined $output defined $error
Truth table100%3/3
A or B
A= defined $outputB= defined $error
ABresult
1X1
011
000
30 50 for ($output, $error) {
31 100 ▶ $_ = \do { my $s; $s = ''} unless ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 10 90 100
32 100 ▶ $$_ = '' if $_ != \undef && !defined($$_);
Branch: 1 branch
BranchTrueFalseTotal
if $_ != \(undef) and not defined $$_ 17 83 100
Condition100%3/3
$_ != \(undef) and not defined $$_
!ll&&!rl&&r
47917
MC/DC100%2/2
$_ != \(undef) and not defined $$_
$_ != \(undef) not defined $$_
Truth table100%3/3
A and not(B)
A= $_ != \(undef)B= not defined $$_
ABresult
0X0
100
111
33 }
34 }
35  
36 # merge if same refs for $output and $error or if both are undef --
37 # i.e. capture \&foo, undef, undef, $merged_file
38 # this means capturing into separate files *requires* at least one
39 # capture variable
40 60 ▶ my $should_merge =
Condition85%12/14
defined $error && defined $output && $output == $error || !(defined $output || defined $error) || 0
l!l
2040
defined $error and defined $output and $output == $error or not defined $output || defined $error
l!l&&r!l&&!r
101040
defined $error and defined $output and $output == $error
!ll&&!rl&&r
104010
defined $error and defined $output
!ll&&!rl&&r
10050
defined $output || defined $error
l!l&&r!l&&!r
40010
MC/DC20%1/5
defined $error && defined $output && $output == $error || !(defined $output || defined $error) || 0
defined $error defined $output $output == $error defined $output defined $error
Truth table30%3/10
A and B and C or not(D || E)
A= defined $errorB= defined $outputC= $output == $errorD= defined $outputE= defined $error
ABCDEresult
111XX1
0XX001
10X001
110001
0XX1X0
0XX010
10X1X0
10X010
1101X0
110010
41 (defined $error && defined $output && $output == $error) ||
42 ( !defined $output && !defined $error ) ||
43 0;
44  
45 60 my ($capture_out, $capture_err);
46  
47 # undef means capture anonymously; anything other than \undef means
48 # capture to that ref; \undef means skip capture
49 60 ▶ if ( !defined $output || $output != \undef ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $output or $output != \(undef)) 58 2 60
Condition100%3/3
not defined $output or $output != \(undef)
l!l&&r!l&&!r
10482
MC/DC100%2/2
not defined $output or $output != \(undef)
not defined $output $output != \(undef)
Truth table100%3/3
not(A) or B
A= not defined $outputB= $output != \(undef)
ABresult
1X1
011
000
50 58 $capture_out = IO::CaptureOutput::_proxy->new(
51 'STDOUT', $output, undef, $output_file
52 );
53 }
54 59 ▶ if ( !defined $error || $error != \undef ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $error or $error != \(undef)) 57 2 59
Condition100%3/3
not defined $error or $error != \(undef)
l!l&&r!l&&!r
10472
MC/DC100%2/2
not defined $error or $error != \(undef)
not defined $error $error != \(undef)
Truth table100%3/3
not(A) or B
A= not defined $errorB= $error != \(undef)
ABresult
1X1
011
000
55 57 ▶ $capture_err = IO::CaptureOutput::_proxy->new(
Branch: 1 branch
BranchTrueFalseTotal
$should_merge ? : 20 37 57
56 'STDERR', $error, ($should_merge ? 'STDOUT' : undef), $error_file
57 );
58 }
59  
60 # now that output capture is setup, call the subroutine
61 # results get read when IO::CaptureOutput::_proxy objects go out of scope
62 58 &$code();
63 }
64  
65 # Extra indirection for symmetry with capture_exec, etc. Gets error reporting
66 # to the right level
67 sub capture (&@) { ## no critic
68 52 ▶ return &_capture;
Subroutine capture: called CC=1 SCAR=0.0
Pod documented
69 }
70  
71 sub capture_exec {
72 5 ▶ my @args = @_;
Subroutine capture_exec: called CC=3 SCAR=11.0
Pod documented
73 5 my ($output, $error);
74 5 ▶ my $exit = _capture sub { system _shell_quote(@args) }, \$output, \$error;
Subroutine __ANON__: called CC=1 SCAR=0.0
75 5 ▶ my $success = ($exit == 0 ) ? 1 : 0 ;
Branch: 1 branch
BranchTrueFalseTotal
$exit == 0 ? : 4 1 5
76 5 $? = $exit;
77 5 ▶ return wantarray ? ($output, $error, $success, $exit) : $output;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 1 5
78 }
79  
80 6 *qxx = \&capture_exec;
81  
82 sub capture_exec_combined {
83 5 ▶ my @args = @_;
Subroutine capture_exec_combined: called CC=3 SCAR=11.0
Pod documented
84 5 my $output;
85 5 ▶ my $exit = _capture sub { system _shell_quote(@args) }, \$output, \$output;
Subroutine __ANON__: called CC=1 SCAR=0.0
86 5 ▶ my $success = ($exit == 0 ) ? 1 : 0 ;
Branch: 1 branch
BranchTrueFalseTotal
$exit == 0 ? : 4 1 5
87 5 $? = $exit;
88 5 ▶ return wantarray ? ($output, $success, $exit) : $output;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 1 5
89 }
90  
91 6 *qxy = \&capture_exec_combined;
92  
93 # extra quoting required on Win32 systems
94 10 ▶*_shell_quote = ($^O =~ /MSWin32/) ? \&_shell_quote_win32 : sub {@_};
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /MSWin32/ ? : 0 6 6
95 sub _shell_quote_win32 {
96 0 ▶ my @args;
Subroutine _shell_quote_win32: not called CC=3 SCAR=24.8
97 0 for (@_) {
98 0 ▶ if (/[ \"]/) { # TODO: check if ^ requires escaping
Branch: 1 branch
BranchTrueFalseTotal
if (/[ \"]/) 0 0 0
99 0 (my $escaped = $_) =~ s/([\"])/\\$1/g;
100 0 push @args, '"' . $escaped . '"';
101 0 next;
102 }
103 0 push @args, $_
104 }
105 0 return @args;
106 }
107  
108 # detect errors and return an error message or empty string;
109 sub _validate {
110 62 ▶ my ($output, $error, $output_file, $error_file) = @_;
Subroutine _validate: called CC=13 SCAR=25.7
111  
112 # default to "ok"
113 62 my $msg = q{};
114  
115 # \$out, \$out, $outfile, $errfile
116 62 ▶ if ( defined $output && defined $error
Branch: 2 branches
BranchTrueFalseTotal
if (defined $output and defined $error and defined $output_file and defined $error_file and $output == $error and $output != \(undef) and $output_file ne $error_file) { } 1 61 62
elsif (not defined $output || defined $error and defined $output_file and defined $error_file and $output_file ne $error_file) { } 1 119 120
Condition94%17/18
defined $output and defined $error and defined $output_file and defined $error_file and $output == $error and $output != \(undef) and $output_file ne $error_file
!ll&&!rl&&r
6011
defined $output and defined $error and defined $output_file and defined $error_file and $output == $error and $output != \(undef)
!ll&&!rl&&r
6002
defined $output and defined $error and defined $output_file and defined $error_file and $output == $error
!ll&&!rl&&r
5732
defined $output and defined $error and defined $output_file and defined $error_file
!ll&&!rl&&r
5525
defined $output and defined $error and defined $output_file
!ll&&!rl&&r
21347
defined $output and defined $error
!ll&&!rl&&r
15641
MC/DC85%6/7
defined $output and defined $error and defined $output_file and defined $error_file and $output == $error and $output != \(undef) and $output_file ne $error_file
defined $output defined $error defined $output_file defined $error_file $output == $error $output != \(undef) $output_file ne $error_file
Truth table87%7/8
A and B and C and D and E and F and G
A= defined $outputB= defined $errorC= defined $output_fileD= defined $error_fileE= $output == $errorF= $output != \(undef)G= $output_file ne $error_file
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
11110XX0
111110X0
11111100
11111111
117 && defined $output_file && defined $error_file
118 && $output == $error
119 && $output != \undef
120 && $output_file ne $error_file
121 ) {
122 1 $msg = "Merged STDOUT and STDERR, but specified different output and error files";
123 }
124 # undef, undef, $outfile, $errfile
125 ▶ elsif ( !defined $output && !defined $error
Condition100%12/12
not defined $output || defined $error and defined $output_file and defined $error_file and $output_file ne $error_file
!ll&&!rl&&r
5911
not defined $output || defined $error and defined $output_file and defined $error_file
!ll&&!rl&&r
5812
not defined $output || defined $error and defined $output_file
!ll&&!rl&&r
5083
defined $output || defined $error
l!l&&r!l&&!r
46411
MC/DC100%5/5
not defined $output || defined $error and defined $output_file and defined $error_file and $output_file ne $error_file
defined $output defined $error defined $output_file defined $error_file $output_file ne $error_file
Truth table100%6/6
not(A || B) and C and D and E
A= defined $outputB= defined $errorC= defined $output_fileD= defined $error_fileE= $output_file ne $error_file
ABCDEresult
1XXXX0
01XXX0
000XX0
0010X0
001100
001111
126 && defined $output_file && defined $error_file
127 && $output_file ne $error_file
128 ) {
129 1 $msg = "Merged STDOUT and STDERR, but specified different output and error files";
130 }
131  
132 62 return $msg;
133 }
134  
135 # Captures everything printed to a filehandle for the lifetime of the object
136 # and then transfers it to a scalar reference
137 package IO::CaptureOutput::_proxy;
138 6 ▶use File::Temp 0.16 'tempfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
139 6 ▶use File::Basename qw/basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 6 ▶use Symbol qw/gensym qualify qualify_to_ref/;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
142  
143 113 ▶sub _is_wperl { $^O eq 'MSWin32' && basename($^X) eq 'wperl.exe' }
Subroutine _is_wperl: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$^O eq "MSWin32" and basename($^X) eq "wperl.exe" 0 113 113
144  
145 sub new {
146 115 ▶ my $class = shift;
Subroutine new: called CC=8 SCAR=20.8
147 115 my ($orig_fh, $capture_var, $merge_fh, $capture_file) = @_;
148 115 $orig_fh = qualify($orig_fh); # e.g. main::STDOUT
149 115 my $fhref = qualify_to_ref($orig_fh); # e.g. \*STDOUT
150  
151 # Duplicate the filehandle
152 115 my $saved_fh;
153 {
154 6 ▶ no strict 'refs'; ## no critic - needed for 5.005
Subroutine BEGIN: called CC=1 SCAR=0.0
155 115 ▶ if ( defined fileno($orig_fh) && ! _is_wperl() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined fileno $orig_fh and not _is_wperl()) 113 2 115
Condition66%2/3
defined fileno $orig_fh and not _is_wperl()
!ll&&!rl&&r
20113
MC/DC50%1/2
defined fileno $orig_fh and not _is_wperl()
defined fileno $orig_fh not _is_wperl()
Truth table66%2/3
A and not(B)
A= defined fileno $orig_fhB= not _is_wperl()
ABresult
0X0
100
111
156 113 $saved_fh = gensym;
157 113 ▶ open $saved_fh, ">&$orig_fh" or croak "Can't redirect <$orig_fh> - $!";
Branch: 1 branch
BranchTrueFalseTotal
open $saved_fh, ">&$orig_fh" or croak("Can't redirect <$orig_fh> - $!") 0 113 113
158 }
159 }
160  
161 # Create replacement filehandle if not merging
162 115 my ($newio_fh, $newio_file);
163 115 ▶ if ( ! $merge_fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $merge_fh) { } 95 20 115
164 95 $newio_fh = gensym;
165 95 ▶ if ($capture_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($capture_file) { } 21 74 95
166 21 $newio_file = $capture_file;
167 } else {
168 74 (undef, $newio_file) = tempfile;
169 }
170 95 ▶ open $newio_fh, "+>$newio_file" or croak "Can't write temp file for $orig_fh - $!";
Branch: 1 branch
BranchTrueFalseTotal
open $newio_fh, "+>$newio_file" or croak("Can't write temp file for $orig_fh - $!") 2 93 95
171 }
172 else {
173 20 $newio_fh = qualify($merge_fh);
174 }
175  
176 # Redirect (or merge)
177 {
178 6 ▶ no strict 'refs'; ## no critic -- needed for 5.005
Subroutine BEGIN: called CC=1 SCAR=0.0
179 113 ▶ open $fhref, ">&".fileno($newio_fh) or croak "Can't redirect $orig_fh - $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fhref, '>&' . fileno($newio_fh) or croak("Can't redirect $orig_fh - $!") 0 113 113
180 }
181  
182 113 bless [$$, $orig_fh, $saved_fh, $capture_var, $newio_fh, $newio_file, $capture_file], $class;
183 }
184  
185 sub DESTROY {
186 113 ▶ my $self = shift;
Subroutine DESTROY: called CC=11 SCAR=24.0
187  
188 113 my ($pid, $orig_fh, $saved_fh, $capture_var, $newio_fh,
189 $newio_file, $capture_file) = @$self;
190 113 ▶ return unless $pid eq $$; # only cleanup in the process that is capturing
Branch: 1 branch
BranchTrueFalseTotal
unless $pid eq $$ 2 111 113
191  
192 # restore the original filehandle
193 111 my $fh_ref = Symbol::qualify_to_ref($orig_fh);
194 111 select((select ($fh_ref), $|=1)[0]);
195 111 ▶ if (defined $saved_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $saved_fh) { } 109 2 111
196 109 ▶ open $fh_ref, ">&". fileno($saved_fh) or croak "Can't restore $orig_fh - $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh_ref, '>&' . fileno($saved_fh) or croak("Can't restore $orig_fh - $!") 0 109 109
197 }
198 else {
199 2 close $fh_ref;
200 }
201  
202 # transfer captured data to the scalar reference if we didn't merge
203 # $newio_file is undef if this file handle is merged to another
204 111 ▶ if (ref $capture_var && $newio_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $capture_var and $newio_file) 81 30 111
Condition100%3/3
ref $capture_var and $newio_file
!ll&&!rl&&r
201081
MC/DC100%2/2
ref $capture_var and $newio_file
ref $capture_var $newio_file
Truth table100%3/3
A and B
A= ref $capture_varB= $newio_file
ABresult
0X0
100
111
205 # some versions of perl complain about reading from fd 1 or 2
206 # which could happen if STDOUT and STDERR were closed when $newio
207 # was opened, so we just squelch warnings here and continue
208 81 local $^W;
209 81 seek $newio_fh, 0, 0;
210 81 $$capture_var = do {local $/; <$newio_fh>};
211 }
212 111 ▶ close $newio_fh if $newio_file;
Branch: 1 branch
BranchTrueFalseTotal
if $newio_file 91 20 111
213  
214 # Cleanup
215 111 ▶ return unless defined $newio_file && -e $newio_file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $newio_file and -e $newio_file 20 91 111
Condition66%2/3
defined $newio_file and -e $newio_file
!ll&&!rl&&r
20091
MC/DC50%1/2
defined $newio_file and -e $newio_file
defined $newio_file -e $newio_file
Truth table66%2/3
A and B
A= defined $newio_fileB= -e $newio_file
ABresult
0X0
100
111
216 91 ▶ return if $capture_file; # the "temp" file was explicitly named
Branch: 1 branch
BranchTrueFalseTotal
if $capture_file 19 72 91
217 72 ▶ unlink $newio_file or carp "Couldn't remove temp file '$newio_file' - $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $newio_file or carp("Couldn't remove temp file '${newio_file}' - $!") 0 72 72
218 }
219  
220 6 1;
221  
222 __END__
↑ Summary /dev/null »