blib/lib/Command/Run.pm

filter: Statementstmt 91.6% 262 / 286 Branchbran 71.1% 138 / 194 Conditioncond 75.0% 24 / 32 MC/DCmcdc 50.0% 9 / 18 Subroutinesub 96.6% 29 / 30 Podpod 64.2% 9 / 14 totaltotal 82.0% 471 / 574 CC 112 SCAR 31.6
CC
112
Cov
83%
execute
51.0
_execute_in_place
35.1
data
17.8
CRAP 23.6
↑ Summary blib/lib/Command/Run/Tmpfile.pm »
1 package Command::Run;
2  
3 40 our $VERSION = "1.06";
4  
5 40 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 40 ▶use parent 'Command::Run::Tmpfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 40 our $debug;
17 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
18 0 my $obj = shift;
19 0 ▶ @_ ? $debug = shift : $debug;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
20 }
21  
22 76 ▶sub code_name {
Subroutine code_name: called CC=2 SCAR=7.0
Pod undocumented
23 76 my $code = shift;
24 76 require B;
25 76 my $cv = B::svref_2object($code);
26 76 ▶ return if $cv->GV->isa('B::SPECIAL');
Branch: 1 branch
BranchTrueFalseTotal
if $cv->GV->isa("B::SPECIAL") 0 76 76
27 76 $cv->GV->NAME;
28 }
29  
30 40 my %default_option = (
31 stderr => undef, # undef: pass-through, 'redirect': merge to stdout, 'capture': separate capture
32 );
33  
34 242 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
35 242 my $class = shift;
36 242 my $obj = $class->SUPER::new;
37 242 $obj->{OPTION} = { %default_option };
38 242 $obj->{RESULT} = {};
39 242 ▶ $obj->configure(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 166 76 242
40 242 $obj;
41 }
42  
43 195 ▶sub configure {
Subroutine configure: called CC=8 SCAR=20.8
Pod undocumented
44 195 my $obj = shift;
45 195 my %args = @_;
46 195 for my $key (keys %args) {
47 389 my $val = $args{$key};
48 389 ▶ if ($key eq 'command') {
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq 'command') { } 166 223 389
elsif ($key eq 'stdin') { } 54 169 223
elsif ($key eq 'stdout') { } 20 149 169
elsif ($key eq 'stderr') { } 46 103 149
49 166 ▶ $obj->command(ref $val eq 'ARRAY' ? @$val : $val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 166 0 166
50 } elsif ($key eq 'stdin') {
51 54 $obj->_set_stdin($val);
52 } elsif ($key eq 'stdout') {
53 20 $obj->{STDOUT_REF} = $val;
54 } elsif ($key eq 'stderr') {
55 46 ▶ if (ref $val eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'SCALAR') { } 5 41 46
56 5 $obj->{STDERR_REF} = $val;
57 5 $obj->option(stderr => 'capture');
58 } else {
59 41 $obj->option(stderr => $val);
60 }
61 } else {
62 103 $obj->option($key => $val);
63 }
64 }
65 195 $obj;
66 }
67  
68 449 ▶sub command {
Subroutine command: called CC=3 SCAR=11.0
Pod documented
69 449 my $obj = shift;
70 449 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 222 227 449
71 222 $obj->{COMMAND} = [ @_ ];
72 222 $obj;
73 } else {
74 227 ▶ @{$obj->{COMMAND} // []};
Condition100%2/2
$obj->{"COMMAND"} // []
l!l
2261
MC/DC100%1/1
$obj->{"COMMAND"} // []
$obj->{"COMMAND"}
Truth table100%2/2
A
A= $obj->{"COMMAND"}
Aresult
11
00
75 }
76 }
77  
78 149 ▶sub option {
Subroutine option: called CC=3 SCAR=11.2
Pod undocumented
79 149 my $obj = shift;
80 149 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 149 149
81 0 return $obj->{OPTION}->{+shift};
82 } else {
83 149 ▶ while (my($k, $v) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 149 149 298
84 149 $obj->{OPTION}->{$k} = $v;
85 }
86 149 return $obj;
87 }
88 }
89  
90 224 ▶sub run {
Subroutine run: called CC=7 SCAR=19.5
Pod documented
91 224 my $obj = shift;
92 224 my %args = @_;
93 224 my $stdout_ref = delete $args{stdout};
94 224 ▶ my $stderr_ref = ref $args{stderr} eq 'SCALAR' ? delete $args{stderr} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'stderr'} eq 'SCALAR' ? : 3 221 224
95 224 ▶ $args{stderr} = 'capture' if $stderr_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $stderr_ref 3 221 224
96 224 $obj->update(%args);
97 196 ▶ if (my $ref = $stdout_ref // $obj->{STDOUT_REF}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $stdout_ref // $obj->{"STDOUT_REF"}) 19 177 196
Condition100%3/3
$stdout_ref // $obj->{"STDOUT_REF"}
l!l&&r!l&&!r
712177
MC/DC100%2/2
$stdout_ref // $obj->{"STDOUT_REF"}
$stdout_ref $obj->{"STDOUT_REF"}
Truth table100%3/3
A // B
A= $stdout_refB= $obj->{"STDOUT_REF"}
ABresult
1X1
011
000
98 19 $$ref = $obj->data;
99 }
100 196 ▶ if (my $ref = $stderr_ref // $obj->{STDERR_REF}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $stderr_ref // $obj->{"STDERR_REF"}) 6 190 196
Condition100%3/3
$stderr_ref // $obj->{"STDERR_REF"}
l!l&&r!l&&!r
24190
MC/DC100%2/2
$stderr_ref // $obj->{"STDERR_REF"}
$stderr_ref $obj->{"STDERR_REF"}
Truth table100%3/3
A // B
A= $stderr_refB= $obj->{"STDERR_REF"}
ABresult
1X1
011
000
101 6 $$ref = $obj->error;
102 }
103 196 return $obj->result;
104 }
105  
106 233 ▶sub update {
Subroutine update: called CC=8 SCAR=21.0
Pod documented
107 40 ▶ use Time::localtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 233 my $obj = shift;
109 233 my %args = @_;
110 233 my $command = delete $args{command};
111 233 ▶ my @command = defined $command ?
Branch: 2 branches
BranchTrueFalseTotal
defined $command ? : 26 207 233
ref $command eq 'ARRAY' ? : 18 8 26
112 (ref $command eq 'ARRAY' ? @$command : $command) : $obj->command;
113 233 ▶ if (@command) {
Branch: 1 branch
BranchTrueFalseTotal
if (@command) 232 1 233
114 232 $obj->{RESULT} = $obj->execute(\@command, %args);
115 # Store stdout in temp file for path access
116 203 my $fh = $obj->fh;
117 203 ▶ $fh->seek(0, 0) or die "seek: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->seek(0, 0) or die "seek: $!\n" 203 0 203
118 203 ▶ $fh->truncate(0) or die "truncate: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->truncate(0) or die "truncate: $!\n" 203 0 203
119 203 ▶ $fh->print($obj->{RESULT}->{data} // '');
Condition50%1/2
$obj->{"RESULT"}{"data"} // ""
l!l
2030
MC/DC0%0/1
$obj->{"RESULT"}{"data"} // ""
$obj->{"RESULT"}{"data"}
Truth table50%1/2
A
A= $obj->{"RESULT"}{"data"}
Aresult
11
00
120 203 $fh->flush;
121 203 ▶ $fh->seek(0, 0) or die "seek: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->seek(0, 0) or die "seek: $!\n" 203 0 203
122 }
123 204 $obj->date(ctime());
124 204 $obj;
125 }
126  
127 199 ▶sub result {
Subroutine result: called CC=1 SCAR=0.0
Pod documented
128 199 my $obj = shift;
129 199 $obj->{RESULT};
130 }
131  
132 232 ▶sub execute {
Subroutine execute: called CC=35 SCAR=39.3
Pod undocumented
133 232 my $obj = shift;
134 232 my $command = shift;
135 232 my %opt = (%{$obj->{OPTION}}, @_);
136 232 ▶ my @command = ref $command eq 'ARRAY' ? @$command : ($command);
Branch: 1 branch
BranchTrueFalseTotal
ref $command eq 'ARRAY' ? : 232 0 232
137  
138 # Use nofork path for code references when requested
139 232 ▶ if ($opt{nofork} and ref $command[0] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"nofork"} and ref $command[0] eq "CODE") 76 156 232
Condition100%3/3
$opt{"nofork"} and ref $command[0] eq "CODE"
!ll&&!rl&&r
154276
MC/DC100%2/2
$opt{"nofork"} and ref $command[0] eq "CODE"
$opt{"nofork"} ref $command[0] eq "CODE"
Truth table100%3/3
A and B
A= $opt{"nofork"}B= ref $command[0] eq "CODE"
ABresult
0X0
100
111
140 76 return $obj->_execute_in_place(\@command, %opt);
141 }
142  
143 156 ▶ my $stderr = $opt{stderr} // '';
Condition100%2/2
$opt{"stderr"} // ""
l!l
41115
MC/DC100%1/1
$opt{"stderr"} // ""
$opt{"stderr"}
Truth table100%2/2
A
A= $opt{"stderr"}
Aresult
11
00
144  
145 # Create pipes for stdout and stderr
146 156 ▶ pipe(my $stdout_r, my $stdout_w) or die "pipe: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $stdout_r, my $stdout_w or die "pipe: $!\n" 156 0 156
147 156 ▶ pipe(my $stderr_r, my $stderr_w) or die "pipe: $!\n" if $stderr eq 'capture';
Branch: 1 branch
BranchTrueFalseTotal
if $stderr eq "capture" 32 124 156
Condition60%3/5
$stderr eq 'capture' && (pipe(my $stderr_r, my $stderr_w) || die("pipe: $!\n"))
!ll&&!rl&&r
124320
pipe my $stderr_r, my $stderr_w or die "pipe: $!\n"
l!l
320
MC/DC0%0/2
$stderr eq 'capture' && (pipe(my $stderr_r, my $stderr_w) || die("pipe: $!\n"))
$stderr eq 'capture' pipe my $stderr_r, my $stderr_w
Truth table33%1/3
A && B
A= $stderr eq 'capture'B= pipe my $stderr_r, my $stderr_w
ABresult
0X0
100
111
148  
149 156 ▶ my $pid = fork // die "fork: $!\n";
Condition50%1/2
fork // die("fork: $!\n")
l!l
1560
MC/DC0%0/1
fork // die("fork: $!\n")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
150 156 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 29 127 156
151 # Child process. It must never return to the program of the
152 # caller: a die here could be caught by an eval of the caller,
153 # and the child would go on as a second copy of the program.
154 # So catch everything, and leave by _exit.
155 29 my $status = 255;
156 29 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { close $stdout_r; close $stderr_r if $stderr eq "capture"; if (exists $opt{"stdin"}) { my $tmp = "IO::File"->new_tmpfile or die "tmpfile: $!\n"; binmode $tmp, ":encoding(utf8)"; $tmp->print($opt{"stdin"}); $tmp->seek(0, 0) or die "seek: $!\n"; open STDIN, "<&", $tmp or die "dup: $!\n"; binmode STDIN, ":encoding(utf8)"; } elsif (my $input = $obj->{"INPUT"}) { open STDIN, "<&=", $input->fileno or die "open: $!\n"; binmode STDIN, ":encoding(utf8)"; } ; open STDOUT, ">&=", $stdout_w->fileno or die "open stdout: $!\n"; if ($stderr eq "redirect") { open STDERR, ">&STDOUT" or die "open stderr: $!\n"; } elsif ($stderr eq "capture") { open STDERR, ">&=", $stderr_w->fileno or die "open stderr: $!\n"; } ; if (ref $command[0] eq "CODE") { select STDOUT; my $code = shift @command; @ARGV = @command; if (my $name = code_name($code)) { $0 = $name; } ; &$code(@command); $status = 0; } else { $status = 127; exec @command; die "exec: $!\n"; } ; 1 } } or print STDERR $@ 0 0 0
157 29 close $stdout_r;
158 29 ▶ close $stderr_r if $stderr eq 'capture';
Branch: 1 branch
BranchTrueFalseTotal
if $stderr eq "capture" 8 21 29
159  
160 29 ▶ if (exists $opt{stdin}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opt{'stdin'}) { } 1 28 29
elsif (my $input = $obj->{'INPUT'}) { } 4 24 28
161 1 ▶ my $tmp = new_tmpfile IO::File or die "tmpfile: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $tmp = "IO::File"->new_tmpfile or die "tmpfile: $!\n" 1 0 1
162 1 binmode $tmp, ':encoding(utf8)';
163 1 $tmp->print($opt{stdin});
164 1 ▶ $tmp->seek(0, 0) or die "seek: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$tmp->seek(0, 0) or die "seek: $!\n" 1 0 1
165 1 ▶ open STDIN, '<&', $tmp or die "dup: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&", $tmp or die "dup: $!\n" 1 0 1
166 1 binmode STDIN, ':encoding(utf8)';
167 } elsif (my $input = $obj->{INPUT}) {
168 4 ▶ open STDIN, "<&=", $input->fileno or die "open: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&=", $input->fileno or die "open: $!\n" 4 0 4
169 4 binmode STDIN, ':encoding(utf8)';
170 }
171  
172 29 ▶ open STDOUT, ">&=", $stdout_w->fileno or die "open stdout: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&=", $stdout_w->fileno or die "open stdout: $!\n" 29 0 29
173 29 ▶ if ($stderr eq 'redirect') {
Branch: 2 branches
BranchTrueFalseTotal
if ($stderr eq 'redirect') { } 3 26 29
elsif ($stderr eq 'capture') { } 8 18 26
174 3 ▶ open STDERR, ">&STDOUT" or die "open stderr: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&STDOUT" or die "open stderr: $!\n" 3 0 3
175 } elsif ($stderr eq 'capture') {
176 8 ▶ open STDERR, ">&=", $stderr_w->fileno or die "open stderr: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&=", $stderr_w->fileno or die "open stderr: $!\n" 8 0 8
177 }
178 # else: stderr passes through to terminal
179  
180 29 ▶ if (ref $command[0] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $command[0] eq 'CODE') { } 0 29 29
181 # print goes to the selected handle: make that the new STDOUT
182 0 select(STDOUT);
183 0 my $code = shift @command;
184 0 @ARGV = @command;
185 0 ▶ if (my $name = code_name($code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = code_name($code)) 0 0 0
186 0 $0 = $name;
187 }
188 0 $code->(@command);
189 0 $status = 0;
190 } else {
191 29 $status = 127;
192 29 exec @command;
193 0 die "exec: $!\n";
194 }
195 0 1;
196 } or print STDERR $@;
197 # _exit does not run the END blocks and the destructors of the
198 # caller, and does not flush: do it here for our two handles.
199 0 STDOUT->flush;
200 0 STDERR->flush;
201 0 POSIX::_exit($status);
202 }
203  
204 # Parent process
205 127 close $stdout_w;
206 127 ▶ close $stderr_w if $stderr eq 'capture';
Branch: 1 branch
BranchTrueFalseTotal
if $stderr eq "capture" 24 103 127
207  
208 # Read both pipes as the data comes. Reading one to the end
209 # before the other blocks forever when the command fills the pipe
210 # which is not being read.
211 127 my %buf = ($stdout_r => '');
212 127 ▶ $buf{$stderr_r} = '' if $stderr eq 'capture';
Branch: 1 branch
BranchTrueFalseTotal
if $stderr eq "capture" 24 103 127
213 127 my $select = IO::Select->new($stdout_r);
214 127 ▶ $select->add($stderr_r) if $stderr eq 'capture';
Branch: 1 branch
BranchTrueFalseTotal
if $stderr eq "capture" 24 103 127
215 127 ▶ while ($select->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select->count) 334 127 461
216 334 for my $fh ($select->can_read) {
217 398 my $n = sysread $fh, $buf{$fh}, 65536, length $buf{$fh};
218 398 ▶ next if not defined $n and $!{EINTR};
Subroutine BEGIN: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n and $!{"EINTR"} 0 398 398
Condition33%1/3
not defined $n and $!{"EINTR"}
!ll&&!rl&&r
39800
MC/DC0%0/2
not defined $n and $!{"EINTR"}
not defined $n $!{"EINTR"}
Truth table33%1/3
not(A) and B
A= not defined $nB= $!{"EINTR"}
ABresult
0X0
100
111
219 398 ▶ $select->remove($fh) if not $n; # end of file, or error
Branch: 1 branch
BranchTrueFalseTotal
unless $n 151 247 398
220 }
221 }
222  
223 # Decode after everything is read: a character can be cut by a read
224 127 my $stdout = _decode($buf{$stdout_r});
225 127 ▶ my $stderr_out = $stderr eq 'capture' ? _decode($buf{$stderr_r}) : '';
Branch: 1 branch
BranchTrueFalseTotal
$stderr eq 'capture' ? : 24 103 127
226  
227 127 close $stdout_r;
228 127 ▶ close $stderr_r if $stderr eq 'capture';
Branch: 1 branch
BranchTrueFalseTotal
if $stderr eq "capture" 24 103 127
229  
230 127 waitpid $pid, 0;
231 127 my $result = $?;
232  
233 return {
234 127 result => $result,
235 data => $stdout,
236 error => $stderr_out,
237 pid => $pid,
238 };
239 }
240  
241 ##
242 ## Decode bytes read from a pipe in the same way as reading the pipe
243 ## through the :encoding(utf8) layer.
244 ##
245 151 ▶sub _decode {
Subroutine _decode: called CC=3 SCAR=11.2
246 151 my $bytes = shift;
247 151 ▶ open my $fh, '<:encoding(utf8)', \$bytes or die "open: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(utf8)", \$bytes or die "open: $!\n" 151 0 151
248 151 my $text = do { local $/; <$fh> };
249 151 ▶ $text // '';
Condition50%1/2
$text // ""
l!l
1510
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
250 }
251  
252 150 ▶sub _tmpfile {
Subroutine _tmpfile: called CC=7 SCAR=19.9
253 150 my ($obj, $key, %opt) = @_;
254 150 ▶ $key .= '_RAW' if $opt{raw};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"raw"} 24 126 150
255 150 ▶ my $fh = $obj->{$key} //= do {
Condition66%2/3
$obj->{$key} //= do { my $f = "IO::File"->new_tmpfile or die "tmpfile: $!\n"; binmode $f, $opt{"raw"} ? ":utf8" : ":encoding(utf8)"; $f }
l!l&&r!l&&!r
101400
MC/DC0%0/2
$obj->{$key} //= do { my $f = "IO::File"->new_tmpfile or die "tmpfile: $!\n"; binmode $f, $opt{"raw"} ? ":utf8" : ":encoding(utf8)"; $f }
$obj->{$key} do { my $f = "IO::File"->new_tmpfile or die "tmpfile: $!\n"; binmode $f, $opt{"raw"} ? ":utf8" : ":encoding(utf8)"; $f }
Truth table66%2/3
A //= B
A= $obj->{$key}B= do { my $f = "IO::File"->new_tmpfile or die "tmpfile: $!\n"; binmode $f, $opt{"raw"} ? ":utf8" : ":encoding(utf8)"; $f }
ABresult
1X1
011
000
256 140 ▶ my $f = new_tmpfile IO::File or die "tmpfile: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $f = "IO::File"->new_tmpfile or die "tmpfile: $!\n" 140 0 140
257 140 ▶ binmode $f, $opt{raw} ? ':utf8' : ':encoding(utf8)';
Branch: 1 branch
BranchTrueFalseTotal
$opt{'raw'} ? : 22 118 140
258 140 $f;
259 };
260 150 ▶ $fh->seek(0, 0) or die "seek: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->seek(0, 0) or die "seek: $!\n" 150 0 150
261 150 ▶ $fh->truncate(0) or die "truncate: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->truncate(0) or die "truncate: $!\n" 150 0 150
262 150 $fh;
263 }
264  
265 ##
266 ## Undo the layer change made for the execution, bringing the handle
267 ## back to the layer list captured before the redirect.
268 ##
269 ## A handle on fd 0, 1 or 2 keeps its existing layer stack when
270 ## re-opened over a dup (open FH, '>&', ...); ordinary handles adopt
271 ## the dup source's stack instead, which is why only the standard
272 ## handles need this. Without it the layer pushed for each execution
273 ## would accumulate indefinitely.
274 ## See https://github.com/kaz-utashiro/perl-perlio-leak-bench
275 ##
276 ## We compare against the saved list rather than assuming the push
277 ## added exactly one layer. binmode ':encoding' is currently not
278 ## idempotent (perl/perl5#10454), so pushing it always stacks; should
279 ## that ever change, an unconditional ':pop' would take away a layer
280 ## the caller set. Popping only down to the saved depth is correct
281 ## either way.
282 ##
283 118 ▶sub _restore_layers {
Subroutine _restore_layers: called CC=5 SCAR=16.1
284 118 my($fh, $raw, $before) = @_;
285 ## ':utf8' only sets a flag on the handle, there is no layer to pop
286 118 ▶ if ($raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw) 37 81 118
287 37 ▶ binmode $fh, ':bytes' unless grep { $_ eq 'utf8' } @$before;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "utf8";} @$before 30 7 37
288 37 return;
289 }
290 81 my $want = scalar @$before;
291 81 my $have = () = PerlIO::get_layers($fh);
292 81 ▶ while ($have > $want) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have > $want) 81 81 162
293 81 binmode $fh, ':pop';
294 81 my $now = () = PerlIO::get_layers($fh);
295 81 ▶ last if $now >= $have; # ':pop' made no progress; stop rather than spin
Branch: 1 branch
BranchTrueFalseTotal
if $now >= $have 0 81 81
296 81 $have = $now;
297 }
298 }
299  
300 76 ▶sub _execute_in_place {
Subroutine _execute_in_place: called CC=32 SCAR=35.6
301 76 my $obj = shift;
302 76 my $command = shift;
303 76 my %opt = @_;
304 76 my @command = @$command;
305 76 ▶ my $stderr_mode = $opt{stderr} // '';
Condition100%2/2
$opt{"stderr"} // ""
l!l
1066
MC/DC100%1/1
$opt{"stderr"} // ""
$opt{"stderr"}
Truth table100%2/2
A
A= $opt{"stderr"}
Aresult
11
00
306 76 my $raw = $opt{raw};
307  
308 76 my $code = shift @command;
309  
310 76 my $tmp_stdout = $obj->_tmpfile('NOFORK_STDOUT', raw => $raw);
311  
312 # Save and redirect STDOUT (always needed)
313 76 ▶ open my $save_stdout, '>&', \*STDOUT or die "dup STDOUT: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $save_stdout, ">&", \*STDOUT or die "dup STDOUT: $!\n" 76 0 76
314 76 my @stdout_layers = PerlIO::get_layers(\*STDOUT);
315 76 ▶ open STDOUT, '>&', $tmp_stdout or die "redirect STDOUT: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $tmp_stdout or die "redirect STDOUT: $!\n" 76 0 76
316 76 ▶ binmode STDOUT, $raw ? ':utf8' : ':encoding(utf8)';
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 19 57 76
317  
318 # Handle STDERR — only save/redirect when needed
319 76 my ($save_stderr, $tmp_stderr);
320 76 ▶ if ($stderr_mode eq 'redirect') {
Branch: 2 branches
BranchTrueFalseTotal
if ($stderr_mode eq 'redirect') { } 2 74 76
elsif ($stderr_mode eq 'capture') { } 8 66 74
321 2 ▶ open $save_stderr, '>&', \*STDERR or die "dup STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $save_stderr, ">&", \*STDERR or die "dup STDERR: $!\n" 2 0 2
322 2 ▶ open STDERR, '>&', \*STDOUT or die "redirect STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", \*STDOUT or die "redirect STDERR: $!\n" 2 0 2
323 } elsif ($stderr_mode eq 'capture') {
324 8 $tmp_stderr = $obj->_tmpfile('NOFORK_STDERR', raw => $raw);
325 8 ▶ open $save_stderr, '>&', \*STDERR or die "dup STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $save_stderr, ">&", \*STDERR or die "dup STDERR: $!\n" 8 0 8
326 8 ▶ open STDERR, '>&', $tmp_stderr or die "redirect STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $tmp_stderr or die "redirect STDERR: $!\n" 8 0 8
327 }
328  
329 # Handle STDIN — only save/redirect when needed
330 76 my $save_stdin;
331 my @stdin_layers;
332 76 ▶ if (exists $opt{stdin}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opt{'stdin'}) { } 12 64 76
elsif (my $input = $obj->{'INPUT'}) { } 30 34 64
333 12 my $tmp_stdin = $obj->_tmpfile('NOFORK_STDIN', raw => $raw);
334 12 $tmp_stdin->print($opt{stdin});
335 12 ▶ $tmp_stdin->seek(0, 0) or die "seek: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$tmp_stdin->seek(0, 0) or die "seek: $!\n" 12 0 12
336 12 ▶ open $save_stdin, '<&', \*STDIN or die "dup STDIN: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $save_stdin, "<&", \*STDIN or die "dup STDIN: $!\n" 12 0 12
337 12 @stdin_layers = PerlIO::get_layers(\*STDIN);
338 12 ▶ open STDIN, '<&', $tmp_stdin or die "redirect STDIN: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&", $tmp_stdin or die "redirect STDIN: $!\n" 12 0 12
339 12 ▶ binmode STDIN, $raw ? ':utf8' : ':encoding(utf8)';
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 4 8 12
340 } elsif (my $input = $obj->{INPUT}) {
341 30 ▶ $input->seek(0, 0) or die "seek: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$input->seek(0, 0) or die "seek: $!\n" 30 0 30
342 30 ▶ open $save_stdin, '<&', \*STDIN or die "dup STDIN: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $save_stdin, "<&", \*STDIN or die "dup STDIN: $!\n" 30 0 30
343 30 @stdin_layers = PerlIO::get_layers(\*STDIN);
344 30 ▶ open STDIN, '<&', $input->fileno or die "redirect STDIN: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&", $input->fileno or die "redirect STDIN: $!\n" 30 0 30
345 30 ▶ binmode STDIN, $raw ? ':utf8' : ':encoding(utf8)';
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 14 16 30
346 }
347  
348 # Set global state
349 76 local $_;
350 76 local @ARGV = @command;
351 76 my $orig_0;
352 76 ▶ if (my $name = code_name($code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = code_name($code)) 76 0 76
353 76 $orig_0 = $0;
354 76 $0 = $name;
355 }
356  
357 # Print of the code goes to the handle select()ed by the caller:
358 # make that STDOUT while it runs
359 76 my $prev_select = select(STDOUT);
360 # Execute
361 76 my $result = 0;
362 76 eval { $code->(@command) };
363 76 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 72 76
364 4 $result = -1;
365 }
366  
367 # Flush and restore — only what was redirected.
368 #
369 # Pop the layer pushed for raw mode first; the redirect reused the
370 # handle's PerlIO object, so it would accumulate on every call.
371 #
372 # Restore the destination with dup2(), not `open STDOUT, ">&"`:
373 # re-opening onto a character device (terminal, /dev/null) gives
374 # the handle a new output side with no layers, and the caller's
375 # later output would lose its :encoding. dup2() leaves the PerlIO
376 # object and its layers alone.
377 # See https://github.com/kaz-utashiro/perl-perlio-leak-bench
378 76 STDOUT->flush;
379 76 _restore_layers(\*STDOUT, $raw, \@stdout_layers);
380 76 ▶ defined POSIX::dup2(fileno($save_stdout), fileno(STDOUT))
Branch: 1 branch
BranchTrueFalseTotal
defined POSIX::dup2(fileno $save_stdout, fileno STDOUT) or die "restore STDOUT: $!\n" 76 0 76
381 or die "restore STDOUT: $!\n";
382 76 close $save_stdout;
383 76 select($prev_select);
384 76 ▶ if ($save_stderr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save_stderr) 10 66 76
385 10 STDERR->flush;
386 10 ▶ defined POSIX::dup2(fileno($save_stderr), fileno(STDERR))
Branch: 1 branch
BranchTrueFalseTotal
defined POSIX::dup2(fileno $save_stderr, fileno STDERR) or die "restore STDERR: $!\n" 10 0 10
387 or die "restore STDERR: $!\n";
388 10 close $save_stderr;
389 }
390 76 ▶ if ($save_stdin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save_stdin) 42 34 76
391 42 _restore_layers(\*STDIN, $raw, \@stdin_layers);
392 42 ▶ open STDIN, '<&', $save_stdin or die "restore STDIN: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&", $save_stdin or die "restore STDIN: $!\n" 42 0 42
393 }
394 76 ▶ if (defined $orig_0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $orig_0) 76 0 76
395 76 $0 = $orig_0;
396 }
397  
398 # Read captured output from tmpfiles
399 76 ▶ $tmp_stdout->seek(0, 0) or die "seek: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$tmp_stdout->seek(0, 0) or die "seek: $!\n" 76 0 76
400 76 my $stdout_data = do { local $/; <$tmp_stdout> };
401  
402 76 my $stderr_data = '';
403 76 ▶ if ($tmp_stderr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp_stderr) 8 68 76
404 8 ▶ $tmp_stderr->seek(0, 0) or die "seek: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$tmp_stderr->seek(0, 0) or die "seek: $!\n" 8 0 8
405 8 $stderr_data = do { local $/; <$tmp_stderr> };
406 }
407  
408 return {
409 76 result => $result,
410 data => $stdout_data,
411 error => $stderr_data,
412 };
413 }
414  
415 30 ▶sub data {
Subroutine data: called CC=5 SCAR=28.8
Pod documented
416 30 my $obj = shift;
417 30 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 30 30
418 0 my $data = shift;
419 0 $obj->{RESULT}->{data} = $data;
420 0 my $fh = $obj->fh;
421 0 ▶ $fh->seek(0, 0) or die "seek: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->seek(0, 0) or die "seek: $!\n" 0 0 0
422 0 ▶ $fh->truncate(0) or die "truncate: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->truncate(0) or die "truncate: $!\n" 0 0 0
423 0 $fh->print($data);
424 0 $fh->flush;
425 0 ▶ $fh->seek(0, 0) or die "seek: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->seek(0, 0) or die "seek: $!\n" 0 0 0
426 0 return $obj;
427 }
428 30 $obj->{RESULT}->{data};
429 }
430  
431 9 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod documented
432 9 my $obj = shift;
433 9 $obj->{RESULT}->{error};
434 }
435  
436 205 ▶sub date {
Subroutine date: called CC=2 SCAR=6.9
Pod documented
437 205 my $obj = shift;
438 205 ▶ @_ ? $obj->{DATE} = shift : $obj->{DATE};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 204 1 205
439 }
440  
441 54 ▶sub _set_stdin {
Subroutine _set_stdin: called CC=3 SCAR=11.2
442 54 my $obj = shift;
443 54 my $data = shift;
444 54 my $input = $obj->_tmpfile('INPUT');
445 54 ▶ $input->fcntl(F_SETFD, 0) or die "fcntl F_SETFD: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$input->fcntl(2, 0) or die "fcntl F_SETFD: $!\n" 54 0 54
446 54 $input->print($data);
447 54 ▶ $input->seek(0, 0) or die "seek: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$input->seek(0, 0) or die "seek: $!\n" 54 0 54
448 54 $obj;
449 }
450  
451 29 ▶sub with {
Subroutine with: called CC=1 SCAR=0.0
Pod documented
452 29 my $obj = shift;
453 29 $obj->configure(@_);
454 }
455  
456 40 1;
457  
458 __END__
↑ Summary blib/lib/Command/Run/Tmpfile.pm »