blib/lib/IPC/System/Options.pm

filter: Statementstmt 59.1% 172 / 291 Branchbran 48.4% 95 / 196 Conditioncond 47.8% 22 / 46 MC/DCmcdc 14.8% 4 / 27 Subroutinesub 90.4% 19 / 21 Podpod 80.0% 4 / 5 totaltotal 53.9% 316 / 586 SCAR 73.3
CC
121
Cov
54%
_system_or_readpipe_or_run_or_start
903.1
__ANON__
34.6
__ANON__
12.6
CRAP 1525.6
↑ Summary /tmp/dUo_5qes69 »
1 package IPC::System::Options;
2  
3 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 2 our $DATE = '2021-01-31'; # DATE
5 2 our $DIST = 'IPC-System-Options'; # DIST
6 2 our $VERSION = '0.340'; # VERSION
7  
8 2 ▶use strict 'subs', 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Proc::ChildError qw(explain_child_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 my $log;
14 2 our %Global_Opts;
15  
16 sub import {
17 1 ▶ my $self = shift;
Subroutine import: called CC=5 SCAR=18.4
18  
19 1 my $caller = caller();
20 1 my $i = 0;
21 1 ▶ while ($i < @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 4 1 5
22 # backtick is the older, deprecated name for readpipe
23 4 ▶ if ($_[$i] =~ /\A(system|readpipe|backtick|run|start|import)\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[$i] =~ /\A(system|readpipe|backtick|run|start|import)\z/) { } 4 0 4
elsif ($_[$i] =~ /\A-(.+)/) { } 0 0 0
24 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 *{"$caller\::$_[$i]"} = \&{"$self\::" . $_[$i]};
26 } elsif ($_[$i] =~ /\A-(.+)/) {
27 0 ▶ die "$_[$i] requires an argument" unless $i < @_-1;
Branch: 1 branch
BranchTrueFalseTotal
unless $i < @_ - 1 0 0 0
28 0 $Global_Opts{$1} = $_[$i+1];
29 0 $i++;
30 } else {
31 0 die "$_[$i] is not exported by ".__PACKAGE__;
32 }
33 4 $i++;
34 }
35 }
36  
37 sub _args2cmd {
38 2 ▶ if (@_ == 1) {
Subroutine _args2cmd: called CC=5 SCAR=20.3
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 2 2
39 0 return $_[0];
40 }
41 2 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 2 2
42 0 require Win32::ShellQuote;
43 return Win32::ShellQuote::quote_system_string(
44 0 ▶ map { ref($_) eq 'SCALAR' ? $$_ : $_ } @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'SCALAR' ? : 0 0 0
45 } else {
46 2 require String::ShellQuote;
47 return join(
48 " ",
49 2 ▶ map { ref($_) eq 'SCALAR' ? $$_ : String::ShellQuote::shell_quote($_) } @_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'SCALAR' ? : 1 7 8
50 );
51 }
52 }
53  
54 sub _system_or_readpipe_or_run_or_start {
55 17 ▶ my $which = shift;
Subroutine _system_or_readpipe_or_run_or_start: called CC=86 SCAR=68.1
56 17 ▶ my $opts = ref($_[0]) eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 15 2 17
57 17 for (keys %Global_Opts) {
58 0 ▶ $opts->{$_} = $Global_Opts{$_} if !defined($opts->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{$_} 0 0 0
59 }
60 17 my @args = @_;
61  
62 # check known options
63 17 for (keys %$opts) {
64 20 ▶ die "Unknown option '$_'"
Branch: 1 branch
BranchTrueFalseTotal
unless /\A( capture_stdout|capture_stderr|capture_merged| tee_stdout|tee_stderr|tee_merged| chdir|dies?|dry_run|env|lang|log|max_log_output|shell| exit_code_success_criteria| fail_log_level| stdin # XXX: only for run() )\z/x 1 19 20
65 unless /\A(
66 capture_stdout|capture_stderr|capture_merged|
67 tee_stdout|tee_stderr|tee_merged|
68 chdir|dies?|dry_run|env|lang|log|max_log_output|shell|
69 exit_code_success_criteria|
70 fail_log_level|
71 stdin # XXX: only for run()
72 )\z/x;
73 }
74  
75 # defaults
76 16 ▶ $opts->{fail_log_level} ||= 'error';
Condition50%1/2
$opts->{"fail_log_level"} ||= "error"
l!l
016
MC/DC0%0/1
$opts->{"fail_log_level"} ||= "error"
$opts->{"fail_log_level"}
Truth table50%1/2
A
A= $opts->{"fail_log_level"}
Aresult
11
00
77  
78 16 ▶ my $opt_die = $opts->{die} || $opts->{dies};
Condition66%2/3
$opts->{'die'} || $opts->{'dies'}
l!l&&r!l&&!r
3013
MC/DC50%1/2
$opts->{'die'} || $opts->{'dies'}
$opts->{'die'} $opts->{'dies'}
Truth table66%2/3
A || B
A= $opts->{'die'}B= $opts->{'dies'}
ABresult
1X1
011
000
79  
80 16 my $child_error;
81 16 my $os_error = "";
82 16 my $exit_code_is_success;
83 my $extra_error;
84  
85 my $code_exit_code_is_success = sub {
86 14 ▶ my $exit_code = shift;
Subroutine __ANON__: called CC=7 SCAR=35.4
87 14 ▶ if (defined $opts->{exit_code_success_criteria}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{'exit_code_success_criteria'}) { } 0 14 14
88 0 ▶ if (ref $opts->{exit_code_success_criteria} eq '' ) { return $exit_code == $opts->{exit_code_success_criteria} }
Branch: 4 branches
BranchTrueFalseTotal
if (ref $opts->{'exit_code_success_criteria'} eq '') { } 0 0 0
elsif (ref $opts->{'exit_code_success_criteria'} eq 'ARRAY') { } 0 0 0
elsif (ref $opts->{'exit_code_success_criteria'} eq 'Regexp') { } 0 0 0
elsif (ref $opts->{'exit_code_success_criteria'} eq 'CODE') { } 0 0 0
89 0 ▶ elsif (ref $opts->{exit_code_success_criteria} eq 'ARRAY' ) { return (grep { $exit_code==$_ } @{ $opts->{exit_code_success_criteria} }) ? 1:0 }
Branch: 1 branch
BranchTrueFalseTotal
grep({$exit_code == $_;} @{$$opts{'exit_code_success_criteria'};}) ? : 0 0 0
90 0 elsif (ref $opts->{exit_code_success_criteria} eq 'Regexp') { return $exit_code =~ $opts->{exit_code_success_criteria} }
91 0 elsif (ref $opts->{exit_code_success_criteria} eq 'CODE' ) { return $opts->{exit_code_success_criteria}->($exit_code) }
92 0 else { die "exit_code_success_criteria must be a number, array of numbers, Regexp, or coderef" }
93 } else {
94 14 return $exit_code == 0;
95 }
96 16 };
97  
98 16 ▶ if ($opts->{log}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"log"}) 0 16 16
99 0 require Log::ger::Format::MultilevelLog; # just so scan_prereqs can detect it
100 0 require Log::ger::Format;
101 0 Log::ger::Format->set_for_current_package(MultilevelLog => (sub_name => 'logger'));
102 0 require Log::ger;
103 0 Log::ger->import;
104 }
105  
106 16 my $cwd;
107 16 ▶ if ($opts->{chdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"chdir"}) 2 14 16
108 2 require Cwd;
109 2 $cwd = Cwd::getcwd();
110 2 ▶ if (!defined $cwd) { # checking $! is always true here, why?
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cwd) 0 2 2
111 0 ▶ $log->error("Can't getcwd: $!") if $log;
Branch: 1 branch
BranchTrueFalseTotal
if $log 0 0 0
112 0 $child_error = -1;
113 0 $exit_code_is_success = 0;
114 0 $os_error = $!;
115 0 $extra_error = "Can't getcwd";
116 0 goto CHECK_RESULT;
117 }
118 2 ▶ unless (chdir $opts->{chdir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $opts->{"chdir"}) 1 1 2
119 1 ▶ $log->error("Can't chdir to '$opts->{chdir}': $!") if $log;
Branch: 1 branch
BranchTrueFalseTotal
if $log 0 1 1
120 1 $child_error = -1;
121 1 $exit_code_is_success = 0;
122 1 $os_error = $!;
123 1 $extra_error = "Can't chdir";
124 1 goto CHECK_RESULT;
125 }
126 }
127  
128 # set ENV
129 15 my %save_env;
130 my %set_env;
131 15 ▶ if ($opts->{lang}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"lang"}) 0 15 15
132 0 $set_env{LC_ALL} = $opts->{lang};
133 0 $set_env{LANGUAGE} = $opts->{lang};
134 0 $set_env{LANG} = $opts->{lang};
135 }
136 15 ▶ if ($opts->{env}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"env"}) 1 14 15
137 1 $set_env{$_} = $opts->{env}{$_} for keys %{ $opts->{env} };
138 }
139 15 ▶ if (%set_env) {
Branch: 1 branch
BranchTrueFalseTotal
if (%set_env) 1 14 15
140 1 for (keys %set_env) {
141 1 $save_env{$_} = $ENV{$_};
142 1 $ENV{$_} = $set_env{$_};
143 }
144 }
145  
146 15 my $wa;
147 my $res;
148  
149 15 my $capture_stdout_was_false;
150 15 my $emulate_backtick;
151 15 my $tmp_capture_stdout;
152  
153 my $code_capture = sub {
154 13 ▶ my $doit = shift;
Subroutine __ANON__: called CC=11 SCAR=25.3
155  
156 13 ▶ if ($opts->{capture_stdout} && $opts->{capture_stderr}) {
Branch: 8 branches
BranchTrueFalseTotal
if ($opts->{'capture_stdout'} and $opts->{'capture_stderr'}) { } 0 13 13
elsif ($opts->{'capture_merged'}) { } 1 12 13
elsif ($opts->{'capture_stdout'}) { } 3 9 12
elsif ($opts->{'capture_stderr'}) { } 1 8 9
elsif ($opts->{'tee_stdout'} and $opts->{'tee_stderr'}) { } 0 8 8
elsif ($opts->{'tee_merged'}) { } 1 7 8
elsif ($opts->{'tee_stdout'}) { } 1 6 7
elsif ($opts->{'tee_stderr'}) { } 1 5 6
Condition66%2/3
$opts->{"capture_stdout"} and $opts->{"capture_stderr"}
!ll&&!rl&&r
1030
MC/DC0%0/2
$opts->{"capture_stdout"} and $opts->{"capture_stderr"}
$opts->{"capture_stdout"} $opts->{"capture_stderr"}
Truth table66%2/3
A and B
A= $opts->{"capture_stdout"}B= $opts->{"capture_stderr"}
ABresult
0X0
100
111
157 0 require Capture::Tiny;
158 0 (${ $opts->{capture_stdout} }, ${ $opts->{capture_stderr} }) =
159 &Capture::Tiny::capture($doit);
160 } elsif ($opts->{capture_merged}) {
161 1 require Capture::Tiny;
162 1 ${ $opts->{capture_merged} } =
163 &Capture::Tiny::capture_merged($doit);
164 } elsif ($opts->{capture_stdout}) {
165 3 require Capture::Tiny;
166 3 ${ $opts->{capture_stdout} } =
167 &Capture::Tiny::capture_stdout($doit);
168 } elsif ($opts->{capture_stderr}) {
169 1 require Capture::Tiny;
170 1 ${ $opts->{capture_stderr} } =
171 &Capture::Tiny::capture_stderr($doit);
172  
173 ▶ } elsif ($opts->{tee_stdout} && $opts->{tee_stderr}) {
Condition66%2/3
$opts->{"tee_stdout"} and $opts->{"tee_stderr"}
!ll&&!rl&&r
710
MC/DC0%0/2
$opts->{"tee_stdout"} and $opts->{"tee_stderr"}
$opts->{"tee_stdout"} $opts->{"tee_stderr"}
Truth table66%2/3
A and B
A= $opts->{"tee_stdout"}B= $opts->{"tee_stderr"}
ABresult
0X0
100
111
174 0 require Capture::Tiny;
175 0 (${ $opts->{tee_stdout} }, ${ $opts->{tee_stderr} }) =
176 &Capture::Tiny::tee($doit);
177 } elsif ($opts->{tee_merged}) {
178 1 require Capture::Tiny;
179 1 ${ $opts->{tee_merged} } =
180 &Capture::Tiny::tee_merged($doit);
181 } elsif ($opts->{tee_stdout}) {
182 1 require Capture::Tiny;
183 1 ${ $opts->{tee_stdout} } =
184 &Capture::Tiny::tee_stdout($doit);
185 } elsif ($opts->{tee_stderr}) {
186 1 require Capture::Tiny;
187 1 ${ $opts->{tee_stderr} } =
188 &Capture::Tiny::tee_stderr($doit);
189 } else {
190 5 $doit->();
191 }
192 15 };
193  
194 15 ▶ if ($which eq 'system') {
Branch: 3 branches
BranchTrueFalseTotal
if ($which eq 'system') { } 10 5 15
elsif ($which eq 'readpipe') { } 3 2 5
elsif ($which eq 'run' or $which eq 'start') { } 2 0 2
195  
196 10 ▶ if ($opts->{log} || $opts->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"log"} or $opts->{"dry_run"}) 0 10 10
Condition33%1/3
$opts->{"log"} or $opts->{"dry_run"}
l!l&&r!l&&!r
0010
MC/DC0%0/2
$opts->{"log"} or $opts->{"dry_run"}
$opts->{"log"} $opts->{"dry_run"}
Truth table33%1/3
A or B
A= $opts->{"log"}B= $opts->{"dry_run"}
ABresult
1X1
011
000
197 0 ▶ if ($opts->{log}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'log'}) { } 0 0 0
198 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
199 0 my $routine;
200 0 my $label = "";
201 0 ▶ if ($opts->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'dry_run'}) { } 0 0 0
202 0 $label = "[DRY RUN] ";
203 0 $routine = "log_info";
204 } else {
205 0 $routine = "log_trace";
206 }
207 0 $routine->("%ssystem(%s), env=%s", $label, \@args, \%set_env);
208 } else {
209 0 warn "[DRY RUN] system("._args2cmd(@args).")\n";
210 }
211 0 ▶ if ($opts->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"dry_run"}) 0 0 0
212 0 $child_error = 0;
213 0 $exit_code_is_success = 1;
214 0 $res = "";
215 0 goto CHECK_RESULT;
216 }
217 }
218  
219 my $doit = sub {
220 10 ▶ if ($opts->{shell}) {
Subroutine __ANON__: called CC=4 SCAR=15.5
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'shell'}) { } 0 10 10
elsif (defined $opts->{'shell'}) { } 0 10 10
221 # force the use of shell
222 0 $res = system _args2cmd(@args);
223 } elsif (defined $opts->{shell}) {
224 # forbid shell
225 0 $res = system {$args[0]} @args;
226 } else {
227 # might or might not use shell (if @args == 1)
228 10 $res = system @args;
229 }
230 10 $child_error = $?;
231 10 ▶ $exit_code_is_success = $code_exit_code_is_success->($? < 0 ? $? : $? >> 8);
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 2 8 10
232 10 $os_error = $!;
233 10 };
234 10 $code_capture->($doit);
235  
236 } elsif ($which eq 'readpipe') {
237  
238 3 $wa = wantarray;
239  
240 3 ▶ if ($opts->{log} || $opts->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"log"} or $opts->{"dry_run"}) 0 3 3
Condition33%1/3
$opts->{"log"} or $opts->{"dry_run"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$opts->{"log"} or $opts->{"dry_run"}
$opts->{"log"} $opts->{"dry_run"}
Truth table33%1/3
A or B
A= $opts->{"log"}B= $opts->{"dry_run"}
ABresult
1X1
011
000
241 0 ▶ if ($opts->{log}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'log'}) { } 0 0 0
242 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
243 0 my $routine;
244 0 my $label = "";
245 0 ▶ if ($opts->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'dry_run'}) { } 0 0 0
246 0 $label = "[DRY RUN] ";
247 0 $routine = "log_info";
248 } else {
249 0 $routine = "log_trace";
250 }
251 0 $routine->("%sreadpipe(%s), env=%s", $label, _args2cmd(@args), \%set_env);
252 } else {
253 0 warn "[DRY RUN] readpipe("._args2cmd(@args).")\n";
254 }
255 0 ▶ if ($opts->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"dry_run"}) 0 0 0
256 0 $child_error = 0;
257 0 $exit_code_is_success = 1;
258 0 $res = "";
259 0 goto CHECK_RESULT;
260 }
261 }
262  
263 # we want to avoid the shell, so we don't use the builtin backtick.
264 # instead, we emulate backtick by system + capturing the output
265 3 ▶ if (defined $opts->{shell} && !$opts->{shell}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"shell"} and not $opts->{"shell"}) 1 2 3
Condition100%3/3
defined $opts->{"shell"} and not $opts->{"shell"}
!ll&&!rl&&r
111
MC/DC100%2/2
defined $opts->{"shell"} and not $opts->{"shell"}
defined $opts->{"shell"} not $opts->{"shell"}
Truth table100%3/3
A and not(B)
A= defined $opts->{"shell"}B= not $opts->{"shell"}
ABresult
0X0
100
111
266 1 $emulate_backtick++;
267 die "Currently cannot backtick() with options shell=0 and capture_merged|tee_*"
268 1 ▶ if $opts->{capture_merged} || $opts->{tee_stdout} || $opts->{tee_stderr} || $opts->{tee_merged};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"capture_merged"} or $opts->{"tee_stdout"} or $opts->{"tee_stderr"} or $opts->{"tee_merged"} 0 1 1
Condition33%3/9
$opts->{"capture_merged"} or $opts->{"tee_stdout"} or $opts->{"tee_stderr"} or $opts->{"tee_merged"}
l!l&&r!l&&!r
001
$opts->{"capture_merged"} or $opts->{"tee_stdout"} or $opts->{"tee_stderr"}
l!l&&r!l&&!r
001
$opts->{"capture_merged"} or $opts->{"tee_stdout"}
l!l&&r!l&&!r
001
MC/DC0%0/4
$opts->{"capture_merged"} or $opts->{"tee_stdout"} or $opts->{"tee_stderr"} or $opts->{"tee_merged"}
$opts->{"capture_merged"} $opts->{"tee_stdout"} $opts->{"tee_stderr"} $opts->{"tee_merged"}
Truth table20%1/5
A or B or C or D
A= $opts->{"capture_merged"}B= $opts->{"tee_stdout"}C= $opts->{"tee_stderr"}D= $opts->{"tee_merged"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
269 1 ▶ if (!$opts->{capture_stdout}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"capture_stdout"}) 1 0 1
270 1 $capture_stdout_was_false++;
271 1 $opts->{capture_stdout} = \$tmp_capture_stdout;
272 }
273 }
274  
275 my $doit = sub {
276 3 ▶ if ($emulate_backtick) {
Subroutine __ANON__: called CC=5 SCAR=16.6
Branch: 1 branch
BranchTrueFalseTotal
if ($emulate_backtick) { } 1 2 3
277 # we don't want shell so we have to emulate backtick with system
278 # + capture the output ourselves
279 1 system {$args[0]} @args;
280 } else {
281 2 my $cmd = _args2cmd(@args);
282 #warn "cmd for backtick: " . $cmd;
283 # use backtick, which uses the shell
284 2 ▶ if ($wa) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wa) { } 0 2 2
285 0 $res = [`$cmd`];
286 } else {
287 2 $res = `$cmd`;
288 }
289 }
290 3 ▶ $child_error = $? < 0 ? $? : $? >> 8;
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 3 3
291 3 ▶ $exit_code_is_success = $code_exit_code_is_success->($? < 0 ? $? : $? >> 8);
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 3 3
292 3 $os_error = $!;
293 3 };
294 3 $code_capture->($doit);
295  
296 3 ▶ if ($emulate_backtick) {
Branch: 1 branch
BranchTrueFalseTotal
if ($emulate_backtick) 1 2 3
297 $res = $capture_stdout_was_false ? $tmp_capture_stdout :
298 1 ▶ ${ $opts->{capture_stdout} };
Branch: 1 branch
BranchTrueFalseTotal
$capture_stdout_was_false ? : 1 0 1
299 1 ▶ $res = [split /^/m, $res] if $wa;
Branch: 1 branch
BranchTrueFalseTotal
if $wa 0 1 1
300 1 ▶ $opts->{capture_stdout} = undef if $capture_stdout_was_false;
Branch: 1 branch
BranchTrueFalseTotal
if $capture_stdout_was_false 1 0 1
301 }
302  
303 # log output
304 3 ▶ if ($opts->{log}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"log"}) 0 3 3
305 0 my $res_show;
306 0 ▶ if (defined $opts->{max_log_output}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"max_log_output"}) 0 0 0
307 0 $res_show = '';
308 0 ▶ if ($wa) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wa) { } 0 0 0
309 0 for (@$res) {
310 0 ▶ if (length($res_show) + length($_) >=
Branch: 1 branch
BranchTrueFalseTotal
if (length($res_show) + length($_) >= $opts->{'max_log_output'}) { } 0 0 0
311 $opts->{max_log_output}) {
312 $res_show .= substr(
313 0 $_,0,$opts->{max_log_output}-length($res_show));
314 0 last;
315 } else {
316 0 $res_show .= $_;
317 }
318 }
319 } else {
320 0 ▶ if (length($res) > $opts->{max_log_output}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $res > $opts->{"max_log_output"}) 0 0 0
321 0 $res_show = substr($res, 0, $opts->{max_log_output});
322 }
323 }
324 }
325 log_trace("result of readpipe(): %s (%d bytes)",
326 defined($res_show) ? $res_show : $res,
327 defined($res_show) ?
328 0 ▶ $opts->{max_log_output} : length($res))
Branch: 3 branches
BranchTrueFalseTotal
if $exit_code_is_success 0 0 0
defined $res_show ? : 0 0 0
defined $res_show ? : 0 0 0
329 if $exit_code_is_success;
330 }
331  
332 ▶ } elsif ($which eq 'run' || $which eq 'start') {
Condition66%2/3
$which eq "run" or $which eq "start"
l!l&&r!l&&!r
110
MC/DC0%0/2
$which eq "run" or $which eq "start"
$which eq "run" $which eq "start"
Truth table66%2/3
A or B
A= $which eq "run"B= $which eq "start"
ABresult
1X1
011
000
333  
334 2 ▶ if ($opts->{log} || $opts->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"log"} or $opts->{"dry_run"}) 0 2 2
Condition33%1/3
$opts->{"log"} or $opts->{"dry_run"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$opts->{"log"} or $opts->{"dry_run"}
$opts->{"log"} $opts->{"dry_run"}
Truth table33%1/3
A or B
A= $opts->{"log"}B= $opts->{"dry_run"}
ABresult
1X1
011
000
335 0 ▶ if ($opts->{log}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'log'}) { } 0 0 0
336 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
337 0 my $routine;
338 0 my $label = "";
339 0 ▶ if ($opts->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'dry_run'}) { } 0 0 0
340 0 $label = "[DRY RUN] ";
341 0 $routine = "log_info";
342 } else {
343 0 $routine = "log_trace";
344 }
345 0 $routine->("%srun(%s), env=%s", $label,
346 join(", ", @args), \%set_env);
347 } else {
348 0 warn "[DRY RUN] $which(".join(", ", @args).")\n";
349 }
350 0 ▶ if ($opts->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"dry_run"}) 0 0 0
351 0 $child_error = 0;
352 0 $exit_code_is_success = 1;
353 0 $res = "";
354 0 goto CHECK_RESULT;
355 }
356 }
357  
358 2 require IPC::Run;
359 2 ▶ my $func = $which eq 'run' ? "IPC::Run::run" : "IPC::Run::start";
Branch: 1 branch
BranchTrueFalseTotal
$which eq 'run' ? : 1 1 2
360 2 $res = &{$func}(
361 \@args,
362 defined($opts->{stdin}) ? \$opts->{stdin} : \*STDIN,
363 sub {
364 2 ▶ if ($opts->{capture_stdout}) {
Subroutine __ANON__: called CC=3 SCAR=12.7
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'capture_stdout'}) { } 2 0 2
365 2 ▶ if (ref $opts->{capture_stdout} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts->{'capture_stdout'} eq 'CODE') { } 0 2 2
366 0 $opts->{capture_stdout}->($_[0]);
367 } else {
368 2 ${$opts->{capture_stdout}} .= $_[0];
369 }
370 } else {
371 0 print $_[0];
372 }
373 }, # out
374 sub {
375 0 ▶ if ($opts->{capture_stderr}) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'capture_stderr'}) { } 0 0 0
376 0 ▶ if (ref $opts->{capture_sderr} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts->{'capture_sderr'} eq 'CODE') { } 0 0 0
377 0 $opts->{capture_sderr}->($_[0]);
378 } else {
379 0 ${$opts->{capture_stderr}} .= $_[0];
380 }
381 } else {
382 0 print STDERR $_[0];
383 }
384 }, # err
385 2 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'stdin'} ? : 2 0 2
386 2 ▶ if ($which eq 'run') {
Branch: 1 branch
BranchTrueFalseTotal
if ($which eq 'run') { } 1 1 2
387 1 $child_error = $?;
388 1 ▶ $exit_code_is_success = $code_exit_code_is_success->($? < 0 ? $? : $? >> 8);
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 1 1
389 1 $os_error = $!;
390 } else {
391 1 $child_error = 0;
392 1 $exit_code_is_success = 1;
393 1 $os_error = "";
394 }
395  
396 } # which
397  
398 # restore ENV
399 15 ▶ if (%save_env) {
Branch: 1 branch
BranchTrueFalseTotal
if (%save_env) 1 14 15
400 1 for (keys %save_env) {
401 1 ▶ if (defined $save_env{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $save_env{$_}) { } 0 1 1
402 0 $ENV{$_} = $save_env{$_};
403 } else {
404 1 undef $ENV{$_};
405 }
406 }
407 }
408  
409 # restore previous working directory
410 15 ▶ if ($cwd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cwd) 1 14 15
411 1 ▶ unless (chdir $cwd) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $cwd) 0 1 1
412 0 ▶ $log->error("Can't chdir back to '$cwd': $!") if $log;
Branch: 1 branch
BranchTrueFalseTotal
if $log 0 0 0
413 0 ▶ $child_error ||= -1;
Condition0%0/2
$child_error ||= -1
l!l
00
MC/DC0%0/1
$child_error ||= -1
$child_error
Truth table0%0/2
A
A= $child_error
Aresult
11
00
414 0 $os_error = $!;
415 0 $extra_error = "Can't chdir back";
416 0 goto CHECK_RESULT;
417 }
418 }
419  
420 CHECK_RESULT:
421 16 ▶ unless ($exit_code_is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exit_code_is_success) 3 13 16
422 3 ▶ if ($opts->{log} || $opt_die) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"log"} or $opt_die) 2 1 3
Condition66%2/3
$opts->{"log"} or $opt_die
l!l&&r!l&&!r
021
MC/DC50%1/2
$opts->{"log"} or $opt_die
$opts->{"log"} $opt_die
Truth table66%2/3
A or B
A= $opts->{"log"}B= $opt_die
ABresult
1X1
011
000
423 my $msg = sprintf(
424 "%s(%s) failed: %s (%s)%s%s%s",
425 $which,
426 join(" ", @args),
427 defined $extra_error ? "" : $child_error,
428 defined $extra_error ? "$extra_error: $os_error" : explain_child_error($child_error, $os_error),
429 (ref($opts->{capture_stdout}) ?
430 ", captured stdout: <<" .
431 0 ▶ (defined ${$opts->{capture_stdout}} ? ${$opts->{capture_stdout}} : ''). ">>" : ""),
Branch: 2 branches
BranchTrueFalseTotal
ref $opts->{'capture_stderr'} ? : 0 2 2
defined ${$$opts{'capture_stderr'};} ? : 0 0 0
432 (ref($opts->{capture_stderr}) ?
433 ", captured stderr: <<" .
434 0 ▶ (defined ${$opts->{capture_stderr}} ? ${$opts->{capture_stderr}} : ''). ">>" : ""),
Branch: 2 branches
BranchTrueFalseTotal
ref $opts->{'capture_merged'} ? : 0 2 2
defined ${$$opts{'capture_merged'};} ? : 0 0 0
435 (ref($opts->{capture_merged}) ?
436 ", captured merged: <<" .
437 2 ▶ (defined ${$opts->{capture_merged}} ? ${$opts->{capture_merged}} : ''). ">>" : ""),
Branch: 4 branches
BranchTrueFalseTotal
defined $extra_error ? : 1 1 2
defined $extra_error ? : 1 1 2
ref $opts->{'capture_stdout'} ? : 0 2 2
defined ${$$opts{'capture_stdout'};} ? : 0 0 0
438 );
439 2 ▶ logger($opts->{fail_log_level}, $msg) if $opts->{log};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"log"} 0 2 2
440 2 ▶ die $msg if $opt_die;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_die 2 0 2
441 }
442 }
443  
444 14 ▶ if ($which ne 'start') {
Branch: 1 branch
BranchTrueFalseTotal
if ($which ne "start") 13 1 14
445 13 $? = $child_error;
446 }
447  
448 14 ▶ return $wa && $which ne 'run' && $which ne 'start' ? @$res : $res;
Branch: 1 branch
BranchTrueFalseTotal
$wa && $which ne 'run' && $which ne 'start' ? : 0 14 14
Condition33%2/6
$wa and $which ne "run" and $which ne "start"
!ll&&!rl&&r
1400
$wa and $which ne "run"
!ll&&!rl&&r
1400
MC/DC0%0/3
$wa and $which ne "run" and $which ne "start"
$wa $which ne "run" $which ne "start"
Truth table25%1/4
A and B and C
A= $waB= $which ne "run"C= $which ne "start"
ABCresult
0XX0
10X0
1100
1111
449 }
450  
451 sub system {
452 12 ▶ _system_or_readpipe_or_run_or_start('system', @_);
Subroutine system: called CC=1 SCAR=0.0
Pod documented
453 }
454  
455 # backtick is the older, deprecated name for readpipe
456 sub backtick {
457 0 ▶ _system_or_readpipe_or_run_or_start('readpipe', @_);
Subroutine backtick: not called CC=1 SCAR=6.9
Pod undocumented
458 }
459  
460 sub readpipe {
461 3 ▶ _system_or_readpipe_or_run_or_start('readpipe', @_);
Subroutine readpipe: called CC=1 SCAR=0.0
Pod documented
462 }
463  
464 sub run {
465 1 ▶ _system_or_readpipe_or_run_or_start('run', @_);
Subroutine run: called CC=1 SCAR=0.0
Pod documented
466 }
467  
468 sub start {
469 1 ▶ _system_or_readpipe_or_run_or_start('start', @_);
Subroutine start: called CC=1 SCAR=0.0
Pod documented
470 }
471  
472 2 1;
473 # ABSTRACT: Perl's system(), readpipe()/qx, IPC::Run's run(), start() (with more options)
474  
475 __END__
↑ Summary /tmp/dUo_5qes69 »