blib/lib/IPC/Run3/Shell.pm

filter: Statementstmt 97.9% 195 / 199 Branchbran 91.9% 149 / 162 Conditioncond 95.5% 43 / 45 MC/DCmcdc 92.8% 26 / 28 Subroutinesub 96.3% 26 / 27 Podpod 40.0% 2 / 5 totaltotal 94.6% 441 / 466 CC 102 SCAR 36.1
CC
102
Cov
95%
__ANON__
65.0
import_into
18.0
_strify
10.2
CRAP 37.1
↑ Summary
1 #!perl
2 package IPC::Run3::Shell;
3 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ##### This is the Perl module IPC::Run3::Shell #####
7 #
8 # Documentation can be found in the file Shell.pod (or via the perldoc command).
9 #
10 # Copyright (c) 2014 Hauke Daempfling (haukex@zero-g.net).
11 #
12 # This library is free software; you can redistribute it and/or modify
13 # it under the same terms as Perl 5 itself.
14 #
15 # For more information see the "Perl Artistic License",
16 # which should have been distributed with your copy of Perl.
17 # Try the command "perldoc perlartistic" or see
18 # http://perldoc.perl.org/perlartistic.html .
19  
20 17 our $VERSION = '0.56';
21  
22 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 17 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 17 ▶use Scalar::Util qw/ blessed looks_like_number /;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 17 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 17 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # debugging stuff
29 # either set env var or set this var externally (may set to a fh / glob ref)
30 17 our $DEBUG;
31 17 ▶BEGIN { $DEBUG = ! ! $ENV{IPC_RUN3_SHELL_DEBUG} unless $DEBUG }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $IPC::Run3::Shell::DEBUG 17 0 17
32 sub debug { ## no critic (RequireArgUnpacking)
33 1 ▶ return unless $DEBUG;
Subroutine debug: called CC=3 SCAR=18.4
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $IPC::Run3::Shell::DEBUG 1 0 1
34 0 ▶ return print { ref $DEBUG eq 'GLOB' ? $DEBUG : \*STDERR } "# ", __PACKAGE__, " Debug: ", @_, "\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $IPC::Run3::Shell::DEBUG eq 'GLOB' ? : 0 0 0
35 }
36  
37 17 my $dumper = Data::Dumper->new([])->Terse(1)->Purity(1)
38 ->Useqq(1)->Quotekeys(0)->Sortkeys(1)->Indent(0)->Pair('=>');
39 6 ▶sub pp { return $dumper->Values(\@_)->Reset->Dump } ## no critic (RequireArgUnpacking)
Subroutine pp: called CC=1 SCAR=0.0
Pod undocumented
40  
41 17 ▶use IPC::Run3 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 17 my @RUN3_OPTS = qw/ binmode_stdin binmode_stdout binmode_stderr append_stdout append_stderr return_if_system_error /;
44 17 my %KNOWN_OPTS = map { $_=>1 } @RUN3_OPTS,
45 qw/ show_cmd allow_exit irs chomp stdin stdout stderr fail_on_stderr both /;
46  
47 17 our $OBJECT_PACKAGE;
48 {
49 package ## no critic (ProhibitMultiplePackages)
50 IPC::Run3::Shell::Autoload; # hide from PAUSE by splitting onto two lines
51 17 ▶ BEGIN { $IPC::Run3::Shell::OBJECT_PACKAGE = __PACKAGE__ }
Subroutine BEGIN: called CC=1 SCAR=0.0
52 17 our $AUTOLOAD;
53 sub AUTOLOAD { ## no critic (ProhibitAutoloading)
54 14 ▶ my $cmd = $AUTOLOAD;
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
55 14 ▶ IPC::Run3::Shell::debug "Autoloading '$cmd'" if $IPC::Run3::Shell::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Run3::Shell::DEBUG 0 14 14
56 14 $cmd =~ s/^.*:://;
57 17 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
58 14 *$AUTOLOAD = IPC::Run3::Shell::make_cmd($cmd);
59 14 goto &$AUTOLOAD;
60 }
61 ▶ sub DESTROY {} # so AUTOLOAD isn't called on destruction
Subroutine DESTROY: not called CC=1 SCAR=0.0
62 }
63  
64 17 sub new {
65 14 ▶ my ($class, %opt) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
66 14 return bless \%opt, $OBJECT_PACKAGE;
67 }
68  
69 17 my %EXPORTABLE = map {$_=>1} qw/ make_cmd /; # "run" gets special handling
70  
71 # this run() is for calling via IPC::Run3::Shell::run(), note that we don't export this below
72 17 *run = make_cmd();
73  
74 sub import {
75 27 ▶ my ($class, @export) = @_;
Subroutine import: called CC=1 SCAR=0.0
76 27 my ($callpack) = caller;
77 27 return import_into($class, $callpack, @export);
78 }
79  
80 sub import_into {
81 28 ▶ my ($class, $callpack, @export) = @_;
Subroutine import_into: called CC=18 SCAR=28.9
Pod documented
82 28 my %opt;
83 28 ▶ %opt = ( %opt, %{shift @export} ) while ref $export[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
ref $export[0] eq "HASH" and do { %opt = (%opt, %{shift @export;}) } 7 28 35
84 28 for my $exp (@export) {
85 31 ▶ if (!defined $exp) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $exp) { } 2 29 31
elsif (not ref $exp and $exp and my($sym) = $exp =~ /^:(\w+)$/) { } 10 19 29
86 2 warnings::warnif('uninitialized','Use of uninitialized value in import');
87 1 next;
88 }
89 ▶ elsif ( !ref($exp) && $exp && ( my ($sym) = $exp=~/^:(\w+)$/ ) ) {
Condition100%6/6
not ref $exp and $exp and my($sym) = $exp =~ /^:(\w+)$/
!ll&&!rl&&r
15410
not ref $exp and $exp
!ll&&!rl&&r
14114
MC/DC100%3/3
not ref $exp and $exp and my($sym) = $exp =~ /^:(\w+)$/
not ref $exp $exp my($sym) = $exp =~ /^:(\w+)$/
Truth table100%4/4
not(A) and B and C
A= not ref $expB= $expC= my($sym) = $exp =~ /^:(\w+)$/
ABCresult
0XX0
10X0
1100
1111
90 10 ▶ if ($sym eq 'run') {
Branch: 3 branches
BranchTrueFalseTotal
if ($sym eq 'run') { } 5 5 10
elsif ($sym eq 'AUTOLOAD') { } 1 4 5
elsif ($sym eq 'FATAL') { } 1 3 4
91 # instead of exporting 'run', we actually export a make_cmd closure (with default options but *no* arguments)
92 5 ▶ debug "Exporting '${callpack}::$sym' => make_cmd("._cmd2str(\%opt).")" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Run3::Shell::DEBUG 0 5 5
93 17 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
94 5 *{"${callpack}::$sym"} = make_cmd(\%opt);
95 }
96 elsif ($sym eq 'AUTOLOAD') {
97 1 ▶ debug "Exporting '${callpack}::$sym'" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Run3::Shell::DEBUG 0 1 1
98 17 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
99 1 *{"${callpack}::AUTOLOAD"} = \&{"${OBJECT_PACKAGE}::AUTOLOAD"};
100 }
101 elsif ($sym eq 'FATAL') {
102 1 debug "Enabling fatal warnings";
103 1 warnings->import(FATAL=>'IPC::Run3::Shell');
104 }
105 else {
106 3 ▶ croak "$class can't export \"$sym\"" unless $EXPORTABLE{$sym};
Branch: 1 branch
BranchTrueFalseTotal
unless $EXPORTABLE{$sym} 1 2 3
107 2 my $target = __PACKAGE__."::$sym";
108 2 ▶ debug "Exporting '${callpack}::$sym' => '$target'" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Run3::Shell::DEBUG 0 2 2
109 17 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
110 2 *{"${callpack}::$sym"} = \&{$target};
111 }
112 }
113 else {
114 19 ▶ my ($sym, @cmd) = ref $exp eq 'ARRAY' ? @$exp : ($exp, $exp);
Branch: 1 branch
BranchTrueFalseTotal
ref $exp eq 'ARRAY' ? : 13 6 19
115 19 ▶ croak "$class: no function name specified" unless $sym;
Branch: 1 branch
BranchTrueFalseTotal
unless $sym 3 16 19
116 16 $sym = _strify($sym); # warn on refs
117 15 ▶ croak "$class: empty command for function \"$sym\"" unless @cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless @cmd 1 14 15
118 14 ▶ debug "Exporting '${callpack}::$sym' => make_cmd("._cmd2str(\%opt, @cmd).")" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Run3::Shell::DEBUG 0 14 14
119 17 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
120 14 *{"${callpack}::$sym"} = make_cmd(\%opt, @cmd);
121 }
122 }
123 21 return;
124 }
125  
126 sub make_cmd { ## no critic (ProhibitExcessComplexity)
127 56 ▶ my @omcmd = @_;
Subroutine make_cmd: called CC=3 SCAR=11.0
Pod documented
128 56 ▶ warnings::warnif(__PACKAGE__."::make_cmd() may have been called as a method")
Branch: 1 branch
BranchTrueFalseTotal
if $omcmd[0] and $omcmd[0] eq __PACKAGE__ 1 55 56
Condition100%3/3
$omcmd[0] and $omcmd[0] eq __PACKAGE__
!ll&&!rl&&r
18371
MC/DC100%2/2
$omcmd[0] and $omcmd[0] eq __PACKAGE__
$omcmd[0] $omcmd[0] eq __PACKAGE__
Truth table100%3/3
A and B
A= $omcmd[0]B= $omcmd[0] eq __PACKAGE__
ABresult
0X0
100
111
129 if $omcmd[0] && $omcmd[0] eq __PACKAGE__ ;
130 return sub {
131 191 ▶ my @acmd = @_; # args to this function call
Subroutine __ANON__: called CC=65 SCAR=41.7
132 191 my @mcmd = @omcmd; # copy of args to make_cmd
133 # if we are a method, get default options from the object
134 191 ▶ my %opt = blessed($acmd[0]) && $acmd[0]->isa($OBJECT_PACKAGE) ? %{shift @acmd} : ();
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($acmd[0]) && $acmd[0]->isa($IPC::Run3::Shell::OBJECT_PACKAGE) ? : 117 74 191
Condition100%3/3
builtin::blessed($acmd[0]) and $acmd[0]->isa($IPC::Run3::Shell::OBJECT_PACKAGE)
!ll&&!rl&&r
731117
MC/DC100%2/2
builtin::blessed($acmd[0]) and $acmd[0]->isa($IPC::Run3::Shell::OBJECT_PACKAGE)
builtin::blessed($acmd[0]) $acmd[0]->isa($IPC::Run3::Shell::OBJECT_PACKAGE)
Truth table100%3/3
A and B
A= builtin::blessed($acmd[0])B= $acmd[0]->isa($IPC::Run3::Shell::OBJECT_PACKAGE)
ABresult
0X0
100
111
135 # hashrefs as the first argument of make_cmd and this method override current options
136 191 ▶ %opt = ( %opt, %{shift @mcmd} ) while ref $mcmd[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
ref $mcmd[0] eq "HASH" and do { %opt = (%opt, %{shift @mcmd;}) } 69 191 260
137 191 ▶ %opt = ( %opt, %{shift @acmd} ) while ref $acmd[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
ref $acmd[0] eq "HASH" and do { %opt = (%opt, %{shift @acmd;}) } 123 191 314
138 # now look at the back of @acmd
139 191 my @tmp_opts;
140 191 ▶ push @tmp_opts, pop @acmd while ref $acmd[-1] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
ref $acmd[-1] eq "HASH" and do { push @tmp_opts, pop @acmd } 25 191 216
141 191 ▶ %opt = ( %opt, %{pop @tmp_opts} ) while @tmp_opts;
Branch: 1 branch
BranchTrueFalseTotal
@tmp_opts and do { %opt = (%opt, %{pop @tmp_opts;}) } 25 191 216
142 # this is for the tests that test the option inheritance mechanism
143 191 ▶ if (exists $opt{__TEST_OPT_A} || exists $opt{__TEST_OPT_B}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{"__TEST_OPT_A"} or exists $opt{"__TEST_OPT_B"}) 45 146 191
Condition100%3/3
exists $opt{"__TEST_OPT_A"} or exists $opt{"__TEST_OPT_B"}
l!l&&r!l&&!r
3411146
MC/DC100%2/2
exists $opt{"__TEST_OPT_A"} or exists $opt{"__TEST_OPT_B"}
exists $opt{"__TEST_OPT_A"} exists $opt{"__TEST_OPT_B"}
Truth table100%3/3
A or B
A= exists $opt{"__TEST_OPT_A"}B= exists $opt{"__TEST_OPT_B"}
ABresult
1X1
011
000
144 return join ',', (
145 exists $opt{__TEST_OPT_A} ? 'A='.(defined $opt{__TEST_OPT_A} ? $opt{__TEST_OPT_A} : 'undef') : (),
146 45 ▶ exists $opt{__TEST_OPT_B} ? 'B='.(defined $opt{__TEST_OPT_B} ? $opt{__TEST_OPT_B} : 'undef') : () );
Branch: 4 branches
BranchTrueFalseTotal
exists $opt{'__TEST_OPT_A'} ? : 34 11 45
defined $opt{'__TEST_OPT_A'} ? : 33 1 34
exists $opt{'__TEST_OPT_B'} ? : 41 4 45
defined $opt{'__TEST_OPT_B'} ? : 40 1 41
147 }
148 # check options for validity
149 146 for (keys %opt) {
150 warnings::warnif(__PACKAGE__.": unknown option \"$_\"")
151 113 ▶ unless $KNOWN_OPTS{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $KNOWN_OPTS{$_} 3 110 113
152 }
153 145 ▶ my $allow_exit = defined $opt{allow_exit} ? $opt{allow_exit} : [0];
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'allow_exit'} ? : 20 125 145
154 145 ▶ if ($allow_exit ne 'ANY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_exit ne "ANY") 141 4 145
155 141 ▶ $allow_exit = [$allow_exit] unless ref $allow_exit eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $allow_exit eq "ARRAY" 4 137 141
156 141 ▶ warnings::warnif(__PACKAGE__.": allow_exit is empty") unless @$allow_exit;
Branch: 1 branch
BranchTrueFalseTotal
unless @$allow_exit 1 140 141
157 141 for (@$allow_exit) {
158 # We throw our own custom warning instead of Perl's regular warning because Perl's warning
159 # would be reported in this module instead of the calling code.
160 147 ▶ warnings::warnif('numeric','Argument "'.(defined($_)?$_:"(undef)").'" isn\'t numeric in allow_exit')
Branch: 2 branches
BranchTrueFalseTotal
unless defined $_ and &looks_like_number($_) 5 142 147
defined $_ ? : 4 1 5
Condition100%3/3
defined $_ and &looks_like_number($_)
!ll&&!rl&&r
14142
MC/DC100%2/2
defined $_ and &looks_like_number($_)
defined $_ &looks_like_number($_)
Truth table100%3/3
A and B
A= defined $_B= &looks_like_number($_)
ABresult
0X0
100
111
161 unless defined && looks_like_number($_);
162 17 ▶ no warnings 'numeric', 'uninitialized'; ## no critic (ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
163 145 $_ = 0+$_; # so later usage as a number isn't a warning
164 }
165 }
166 # Possible To-Do for Later: Define priorities for incompatible options so we can carp instead of croaking?
167 # Also maybe look at some other places where we croak at runtime to see if there is any way to carp there instead.
168 croak __PACKAGE__.": can't use options stderr and fail_on_stderr at the same time"
169 143 ▶ if exists $opt{stderr} && $opt{fail_on_stderr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{"stderr"} and $opt{"fail_on_stderr"} 1 142 143
Condition100%3/3
exists $opt{"stderr"} and $opt{"fail_on_stderr"}
!ll&&!rl&&r
13571
MC/DC100%2/2
exists $opt{"stderr"} and $opt{"fail_on_stderr"}
exists $opt{"stderr"} $opt{"fail_on_stderr"}
Truth table100%3/3
A and B
A= exists $opt{"stderr"}B= $opt{"fail_on_stderr"}
ABresult
0X0
100
111
170 croak __PACKAGE__.": can't use options both and stdout at the same time"
171 142 ▶ if $opt{both} && exists $opt{stdout};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"both"} and exists $opt{"stdout"} 1 141 142
Condition100%3/3
$opt{"both"} and exists $opt{"stdout"}
!ll&&!rl&&r
130111
MC/DC100%2/2
$opt{"both"} and exists $opt{"stdout"}
$opt{"both"} exists $opt{"stdout"}
Truth table100%3/3
A and B
A= $opt{"both"}B= exists $opt{"stdout"}
ABresult
0X0
100
111
172 croak __PACKAGE__.": can't use options both and stderr at the same time"
173 141 ▶ if $opt{both} && exists $opt{stderr};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"both"} and exists $opt{"stderr"} 1 140 141
Condition100%3/3
$opt{"both"} and exists $opt{"stderr"}
!ll&&!rl&&r
130101
MC/DC100%2/2
$opt{"both"} and exists $opt{"stderr"}
$opt{"both"} exists $opt{"stderr"}
Truth table100%3/3
A and B
A= $opt{"both"}B= exists $opt{"stderr"}
ABresult
0X0
100
111
174 croak __PACKAGE__.": can't use options both and fail_on_stderr at the same time"
175 140 ▶ if $opt{both} && $opt{fail_on_stderr};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"both"} and $opt{"fail_on_stderr"} 1 139 140
Condition100%3/3
$opt{"both"} and $opt{"fail_on_stderr"}
!ll&&!rl&&r
13091
MC/DC100%2/2
$opt{"both"} and $opt{"fail_on_stderr"}
$opt{"both"} $opt{"fail_on_stderr"}
Truth table100%3/3
A and B
A= $opt{"both"}B= $opt{"fail_on_stderr"}
ABresult
0X0
100
111
176 # assemble command (after having processed any option hashes etc.)
177 139 my @fcmd = (@mcmd, @acmd);
178 139 ▶ croak __PACKAGE__.": empty command" unless @fcmd;
Branch: 1 branch
BranchTrueFalseTotal
unless @fcmd 1 138 139
179 # stringify the stringifiable things, handle undef, and warn on refs
180 138 @fcmd = map {_strify($_)} @fcmd;
181
182 # prepare STDOUT redirection
183 130 my ($out, $stdout) = ('');
184 130 ▶ if (exists $opt{stdout}) ## no critic (ProhibitCascadingIfElse)
Branch: 4 branches
BranchTrueFalseTotal
if (exists $opt{'stdout'}) { } 18 112 130
elsif ($opt{'both'}) { } 9 103 112
elsif (wantarray) { } 10 93 103
elsif (defined wantarray) { } 49 44 93
185 18 { $stdout = $opt{stdout} }
186 elsif ($opt{both})
187 9 ▶ { $stdout = defined(wantarray) ? \$out : undef }
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 8 1 9
188 elsif (wantarray)
189 10 { $stdout = $out = [] }
190 elsif (defined(wantarray))
191 49 { $stdout = \$out }
192 else
193 44 { $stdout = undef }
194 # prepare STDERR redirection
195 130 my ($err, $stderr) = ('');
196 130 ▶ if (exists $opt{stderr})
Branch: 3 branches
BranchTrueFalseTotal
if (exists $opt{'stderr'}) { } 6 124 130
elsif ($opt{'fail_on_stderr'}) { } 9 115 124
elsif ($opt{'both'}) { } 9 106 115
197 6 { $stderr = $opt{stderr} }
198 elsif ($opt{fail_on_stderr})
199 9 { $stderr = \$err }
200 elsif ($opt{both})
201 9 ▶ { $stderr = wantarray ? \$err : ( defined(wantarray) ? \$out : undef ) }
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 4 5 9
defined wantarray ? : 4 1 5
202 else
203 106 { $stderr = undef }
204 # prepare options hash
205 130 my %r3o = ( return_if_system_error=>1 );
206 130 ▶ for (@RUN3_OPTS) { $r3o{$_} = $opt{$_} if exists $opt{$_} }
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{$_} 1 779 780
207 # execute and process
208 130 ▶ debug "run3("._cmd2str(@fcmd).") ".pp(\%opt) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Run3::Shell::DEBUG 0 130 130
209 130 ▶ print { ref $opt{show_cmd} eq 'GLOB' ? $opt{show_cmd} : \*STDERR } '$ '._cmd2str(@fcmd)."\n" if $opt{show_cmd};
Branch: 2 branches
BranchTrueFalseTotal
if $opt{"show_cmd"} 5 125 130
ref $opt{'show_cmd'} eq 'GLOB' ? : 1 4 5
210 130 ▶ local $/ = exists $opt{irs} ? $opt{irs} : $/;
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{'irs'} ? : 6 124 130
211 # NOTE that we've documented that the user can rely on $?, so don't mess with it
212 130 ▶ IPC::Run3::run3( \@fcmd, $opt{stdin}, $stdout, $stderr, \%r3o )
Branch: 1 branch
BranchTrueFalseTotal
IPC::Run3::run3(\@fcmd, $opt{"stdin"}, $stdout, $stderr, \%r3o) or croak('IPC::Run3::Shell' . qq[ (internal): run3 "$fcmd[0]" failed]) 1 129 130
213 or croak __PACKAGE__." (internal): run3 \"$fcmd[0]\" failed";
214 129 my $exitcode = $?>>8;
215 croak "Command \"$fcmd[0]\" failed: process wrote to STDERR: \"$err\""
216 129 ▶ if $opt{fail_on_stderr} && $err ne '' && $err ne $/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"fail_on_stderr"} and $err ne "" and $err ne $/ 5 124 129
Condition100%6/6
$opt{"fail_on_stderr"} and $err ne "" and $err ne $/
!ll&&!rl&&r
12225
$opt{"fail_on_stderr"} and $err ne ""
!ll&&!rl&&r
12027
MC/DC100%3/3
$opt{"fail_on_stderr"} and $err ne "" and $err ne $/
$opt{"fail_on_stderr"} $err ne "" $err ne $/
Truth table100%4/4
A and B and C
A= $opt{"fail_on_stderr"}B= $err ne ""C= $err ne $/
ABCresult
0XX0
10X0
1100
1111
217 124 ▶ if ($? == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($? == -1) { } 3 121 124
elsif ($? & 127) { } 4 117 121
218 3 warnings::warnif("Command \"$fcmd[0]\" failed: $!");
219 return
220 2 }
221 elsif ($?&127) {
222 4 ▶ warnings::warnif(sprintf("Command \"%s\" failed: signal %d, %s coredump",
Branch: 1 branch
BranchTrueFalseTotal
$? & 128 ? : 1 3 4
223 $fcmd[0], ($?&127), ($?&128)?'with':'without' ))
224 }
225 else {
226 # allow_exit is checked for validity above
227 warnings::warnif("Command \"$fcmd[0]\" failed: exit status $exitcode")
228 117 ▶ unless $allow_exit eq 'ANY' || grep {$_==$exitcode} @$allow_exit;
Branch: 1 branch
BranchTrueFalseTotal
unless $allow_exit eq "ANY" or grep {$_ == $exitcode;} @$allow_exit 15 102 117
Condition100%3/3
$allow_exit eq "ANY" or grep {$_ == $exitcode;} @$allow_exit
l!l&&r!l&&!r
49815
MC/DC100%2/2
$allow_exit eq "ANY" or grep {$_ == $exitcode;} @$allow_exit
$allow_exit eq "ANY" grep {$_ == $exitcode;} @$allow_exit
Truth table100%3/3
A or B
A= $allow_exit eq "ANY"B= grep {$_ == $exitcode;} @$allow_exit
ABresult
1X1
011
000
229 }
230 109 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 32 77 109
231 77 ▶ if (exists $opt{stdout})
Branch: 3 branches
BranchTrueFalseTotal
if (exists $opt{'stdout'}) { } 12 65 77
elsif ($opt{'both'}) { } 8 57 65
elsif (wantarray) { } 10 47 57
232 12 { return $exitcode }
233 elsif ($opt{both}) {
234 8 ▶ chomp($out,$err) if $opt{chomp};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"chomp"} 2 6 8
235 8 ▶ return wantarray ? ($out, $err, $exitcode) : $out
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 4 8
236 }
237 elsif (wantarray) {
238 10 ▶ chomp(@$out) if $opt{chomp};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"chomp"} 4 6 10
239 10 return @$out
240 }
241 else {
242 47 ▶ chomp($out) if $opt{chomp};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"chomp"} 5 42 47
243 47 return $out
244 }
245 }
246 55 }
247  
248 # This function attempts to behave like normal Perl stringification, but it adds two things:
249 # 1. Warnings on undef, in Perl's normal "uninitialized" category, the difference being that
250 # with "warnif", they will appear to originate in the calling code, and not in this function.
251 # 2. Warn if we are passed a reference that is not an object with overloaded stringification,
252 # since that is much more likely to be a mistake on the part of the user instead of intentional.
253 sub _strify {
254 469 ▶ my ($x) = @_;
Subroutine _strify: called CC=10 SCAR=23.2
255 469 ▶ if (!defined $x) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $x) { } 4 465 469
elsif (builtin::blessed($x) and overload::Overloaded($x)) { } 10 455 465
256 4 warnings::warnif('uninitialized','Use of uninitialized value in argument list');
257 1 return "" }
258 ▶ elsif (blessed($x) && overload::Overloaded($x)) { # an object with overloading
Condition100%3/3
builtin::blessed($x) and overload::Overloaded($x)
!ll&&!rl&&r
452310
MC/DC100%2/2
builtin::blessed($x) and overload::Overloaded($x)
builtin::blessed($x) overload::Overloaded($x)
Truth table100%3/3
A and B
A= builtin::blessed($x)B= overload::Overloaded($x)
ABresult
0X0
100
111
259 10 ▶ if (overload::Method($x,'""')) # stringification explicitly defined, it'll work
Branch: 3 branches
BranchTrueFalseTotal
if (overload::Method($x, '""')) { } 5 5 10
elsif (defined(my $rv = eval { do { "$x" } })) { } 2 3 5
elsif ($@ =~ /\bno method found\b/) { } 2 1 3
260 5 { return "$x" }
261 # Else, stringification is not explicitly defined - stringification *may* work through autogeneration, but it may also die.
262 # There doesn't seem to be a way to ask Perl if stringification will die or not other than trying it out with eval.
263 # See also: http://www.perlmonks.org/?node_id=1121710
264 # Reminder to self: "$x" will always be defined; even if overloaded stringify returns undef;
265 # undef interpolated into the string will cause warning, but the resulting empty string is still defined.
266 5 elsif (defined(my $rv = eval { "$x" }))
267 2 { return $rv }
268 elsif ($@=~/\bno method found\b/) { # overloading failed, throw custom error
269 # Note: as far as I can tell the message "no method found"
270 # hasn't changed since its intoduction in Perl 5.000
271 # (e.g. git log -p -S 'no method found' gv.c )
272 # Perl bug #31793, which relates to overload::StrVal, apparently also caused problems with Carp
273 2 ▶ if (!$overload::VERSION || $overload::VERSION<1.04)
Branch: 1 branch
BranchTrueFalseTotal
if (not $overload::VERSION or $overload::VERSION < 1.04) { } 0 2 2
Condition33%1/3
not $overload::VERSION or $overload::VERSION < "1.04"
l!l&&r!l&&!r
002
MC/DC0%0/2
not $overload::VERSION or $overload::VERSION < "1.04"
not $overload::VERSION $overload::VERSION < "1.04"
Truth table33%1/3
not(A) or B
A= not $overload::VERSIONB= $overload::VERSION < "1.04"
ABresult
1X1
011
000
274 0 { die "Package ".ref($x)." doesn't overload stringification: $@" } ## no critic (RequireCarping)
275 else
276 2 { croak "Package ".ref($x)." doesn't overload stringification: $@" }
277 }
278 # something other than overloading failed, just re-throw
279 1 else { die $@ } ## no critic (RequireCarping)
280 # Remember that Perl's normal behavior should stringification not be
281 # available is to die; we're just propagating that behavior outward.
282 }
283 else {
284 # Note that objects without any overloading will stringify using Perl's default mechanism
285 455 ▶ ref($x) and warnings::warnif(__PACKAGE__.": argument list contains references/objects");
Branch: 1 branch
BranchTrueFalseTotal
ref $x and warnings::warnif("IPC::Run3::Shell: argument list contains references/objects") 10 445 455
286 449 return "$x" }
287 }
288  
289 # function for sorta-pretty-printing commands
290 sub _cmd2str {
291 5 ▶ my @c = @_;
Subroutine _cmd2str: called CC=6 SCAR=18.2
292 5 my $o = '';
293 5 for my $c (@c) {
294 18 ▶ $o .= ' ' if $o;
Branch: 1 branch
BranchTrueFalseTotal
if $o 13 5 18
295 18 ▶ if (ref $c eq 'HASH') { # options
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq 'HASH') { } 0 18 18
296 # note we don't pay attention to where in the argument list we are
297 # (I don't expect hashrefs to appear as arguments, the user is even warned about them)
298 0 $o .= pp($c);
299 }
300 else {
301 18 ▶ my $s = defined $c ? "$c" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $c ? : 18 0 18
302 18 ▶ $s = pp($s) if $s=~/[^\w\-\=\/\.]/;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ m[[^\w\-\=/\.]] 6 12 18
303 18 $o .= $s;
304 }
305 }
306 5 return $o;
307 }
308  
309  
310 17 1;
↑ Summary