| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 49 | 166 | ▶ | $obj->command(ref $val eq 'ARRAY' ? @$val : $val); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 71 | 222 | $obj->{COMMAND} = [ @_ ]; | |||||||||||||||||||||||||||||||
| 72 | 222 | $obj; | |||||||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||||||
| 74 | 227 | ▶ | @{$obj->{COMMAND} // []}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$obj->{"COMMAND"} // []
MC/DC100%1/1
$obj->{"COMMAND"} // []
$obj->{"COMMAND"}
Truth table100%2/2
A
A= $obj->{"COMMAND"}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 81 | 0 | return $obj->{OPTION}->{+shift}; | |||||||||||||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||||||||||||||
| 83 | 149 | ▶ | while (my($k, $v) = splice @_, 0, 2) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 95 | 224 | ▶ | $args{stderr} = 'capture' if $stderr_ref; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 224 | $obj->update(%args); | |||||||||||||||||||||||||||||||
| 97 | 196 | ▶ | if (my $ref = $stdout_ref // $obj->{STDOUT_REF}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$stdout_ref // $obj->{"STDOUT_REF"}
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"}
| |||||||||||||||||||||||||||||||||
| 98 | 19 | $$ref = $obj->data; | |||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | 196 | ▶ | if (my $ref = $stderr_ref // $obj->{STDERR_REF}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$stderr_ref // $obj->{"STDERR_REF"}
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"}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 112 | (ref $command eq 'ARRAY' ? @$command : $command) : $obj->command; | ||||||||||||||||||||||||||||||||
| 113 | 233 | ▶ | if (@command) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 118 | 203 | ▶ | $fh->truncate(0) or die "truncate: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 119 | 203 | ▶ | $fh->print($obj->{RESULT}->{data} // ''); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$obj->{"RESULT"}{"data"} // ""
MC/DC0%0/1
$obj->{"RESULT"}{"data"} // ""
$obj->{"RESULT"}{"data"}
Truth table50%1/2
A
A= $obj->{"RESULT"}{"data"}
| |||||||||||||||||||||||||||||||||
| 120 | 203 | $fh->flush; | |||||||||||||||||||||||||||||||
| 121 | 203 | ▶ | $fh->seek(0, 0) or die "seek: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | # Use nofork path for code references when requested | ||||||||||||||||||||||||||||||||
| 139 | 232 | ▶ | if ($opt{nofork} and ref $command[0] eq 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$opt{"nofork"} and ref $command[0] eq "CODE"
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"
| |||||||||||||||||||||||||||||||||
| 140 | 76 | return $obj->_execute_in_place(\@command, %opt); | |||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 156 | ▶ | my $stderr = $opt{stderr} // ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$opt{"stderr"} // ""
MC/DC100%1/1
$opt{"stderr"} // ""
$opt{"stderr"}
Truth table100%2/2
A
A= $opt{"stderr"}
| |||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | # Create pipes for stdout and stderr | ||||||||||||||||||||||||||||||||
| 146 | 156 | ▶ | pipe(my $stdout_r, my $stdout_w) or die "pipe: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 147 | 156 | ▶ | pipe(my $stderr_r, my $stderr_w) or die "pipe: $!\n" if $stderr eq 'capture'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
$stderr eq 'capture' && (pipe(my $stderr_r, my $stderr_w) || die("pipe: $!\n"))
pipe my $stderr_r, my $stderr_w or die "pipe: $!\n"
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
| |||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 156 | ▶ | my $pid = fork // die "fork: $!\n"; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
fork // die("fork: $!\n")
MC/DC0%0/1
fork // die("fork: $!\n")
fork
Truth table50%1/2
A
A= fork
| |||||||||||||||||||||||||||||||||
| 150 | 156 | ▶ | if ($pid == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 157 | 29 | close $stdout_r; | |||||||||||||||||||||||||||||||
| 158 | 29 | ▶ | close $stderr_r if $stderr eq 'capture'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | 29 | ▶ | if (exists $opt{stdin}) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 161 | 1 | ▶ | my $tmp = new_tmpfile IO::File or die "tmpfile: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 165 | 1 | ▶ | open STDIN, '<&', $tmp or die "dup: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 169 | 4 | binmode STDIN, ':encoding(utf8)'; | |||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | 29 | ▶ | open STDOUT, ">&=", $stdout_w->fileno or die "open stdout: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 173 | 29 | ▶ | if ($stderr eq 'redirect') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 174 | 3 | ▶ | open STDERR, ">&STDOUT" or die "open stderr: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 175 | } elsif ($stderr eq 'capture') { | ||||||||||||||||||||||||||||||||
| 176 | 8 | ▶ | open STDERR, ">&=", $stderr_w->fileno or die "open stderr: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||
| 178 | # else: stderr passes through to terminal | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | 29 | ▶ | if (ref $command[0] eq 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 213 | 127 | my $select = IO::Select->new($stdout_r); | |||||||||||||||||||||||||||||||
| 214 | 127 | ▶ | $select->add($stderr_r) if $stderr eq 'capture'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 215 | 127 | ▶ | while ($select->count) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
not defined $n and $!{"EINTR"}
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"}
| |||||||||||||||||||||||||||||||||
| 219 | 398 | ▶ | $select->remove($fh) if not $n; # end of file, or error | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | 127 | close $stdout_r; | |||||||||||||||||||||||||||||||
| 228 | 127 | ▶ | close $stderr_r if $stderr eq 'capture'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 248 | 151 | my $text = do { local $/; <$fh> }; | |||||||||||||||||||||||||||||||
| 249 | 151 | ▶ | $text // ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$text // ""
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
}
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
}
| |||||||||||||||||||||||||||||||||
| 256 | 140 | ▶ | my $f = new_tmpfile IO::File or die "tmpfile: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 257 | 140 | ▶ | binmode $f, $opt{raw} ? ':utf8' : ':encoding(utf8)'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 258 | 140 | $f; | |||||||||||||||||||||||||||||||
| 259 | }; | ||||||||||||||||||||||||||||||||
| 260 | 150 | ▶ | $fh->seek(0, 0) or die "seek: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 261 | 150 | ▶ | $fh->truncate(0) or die "truncate: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 287 | 37 | ▶ | binmode $fh, ':bytes' unless grep { $_ eq 'utf8' } @$before; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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"} // ""
MC/DC100%1/1
$opt{"stderr"} // ""
$opt{"stderr"}
Truth table100%2/2
A
A= $opt{"stderr"}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 314 | 76 | my @stdout_layers = PerlIO::get_layers(\*STDOUT); | |||||||||||||||||||||||||||||||
| 315 | 76 | ▶ | open STDOUT, '>&', $tmp_stdout or die "redirect STDOUT: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 316 | 76 | ▶ | binmode STDOUT, $raw ? ':utf8' : ':encoding(utf8)'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 321 | 2 | ▶ | open $save_stderr, '>&', \*STDERR or die "dup STDERR: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 322 | 2 | ▶ | open STDERR, '>&', \*STDOUT or die "redirect STDERR: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 326 | 8 | ▶ | open STDERR, '>&', $tmp_stderr or die "redirect STDERR: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 336 | 12 | ▶ | open $save_stdin, '<&', \*STDIN or die "dup STDIN: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 337 | 12 | @stdin_layers = PerlIO::get_layers(\*STDIN); | |||||||||||||||||||||||||||||||
| 338 | 12 | ▶ | open STDIN, '<&', $tmp_stdin or die "redirect STDIN: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 339 | 12 | ▶ | binmode STDIN, $raw ? ':utf8' : ':encoding(utf8)'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 340 | } elsif (my $input = $obj->{INPUT}) { | ||||||||||||||||||||||||||||||||
| 341 | 30 | ▶ | $input->seek(0, 0) or die "seek: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 342 | 30 | ▶ | open $save_stdin, '<&', \*STDIN or die "dup STDIN: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 343 | 30 | @stdin_layers = PerlIO::get_layers(\*STDIN); | |||||||||||||||||||||||||||||||
| 344 | 30 | ▶ | open STDIN, '<&', $input->fileno or die "redirect STDIN: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 345 | 30 | ▶ | binmode STDIN, $raw ? ':utf8' : ':encoding(utf8)'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 381 | or die "restore STDOUT: $!\n"; | ||||||||||||||||||||||||||||||||
| 382 | 76 | close $save_stdout; | |||||||||||||||||||||||||||||||
| 383 | 76 | select($prev_select); | |||||||||||||||||||||||||||||||
| 384 | 76 | ▶ | if ($save_stderr) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 385 | 10 | STDERR->flush; | |||||||||||||||||||||||||||||||
| 386 | 10 | ▶ | defined POSIX::dup2(fileno($save_stderr), fileno(STDERR)) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 387 | or die "restore STDERR: $!\n"; | ||||||||||||||||||||||||||||||||
| 388 | 10 | close $save_stderr; | |||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||
| 390 | 76 | ▶ | if ($save_stdin) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 391 | 42 | _restore_layers(\*STDIN, $raw, \@stdin_layers); | |||||||||||||||||||||||||||||||
| 392 | 42 | ▶ | open STDIN, '<&', $save_stdin or die "restore STDIN: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||
| 394 | 76 | ▶ | if (defined $orig_0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 400 | 76 | my $stdout_data = do { local $/; <$tmp_stdout> }; | |||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||
| 402 | 76 | my $stderr_data = ''; | |||||||||||||||||||||||||||||||
| 403 | 76 | ▶ | if ($tmp_stderr) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 404 | 8 | ▶ | $tmp_stderr->seek(0, 0) or die "seek: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 422 | 0 | ▶ | $fh->truncate(0) or die "truncate: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 423 | 0 | $fh->print($data); | |||||||||||||||||||||||||||||||
| 424 | 0 | $fh->flush; | |||||||||||||||||||||||||||||||
| 425 | 0 | ▶ | $fh->seek(0, 0) or die "seek: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 446 | 54 | $input->print($data); | |||||||||||||||||||||||||||||||
| 447 | 54 | ▶ | $input->seek(0, 0) or die "seek: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||