lib/IPC/Cmd.pm

filter: Statementstmt 75.1% 103 / 137 Branchbran 48.6% 36 / 74 Conditioncond 52.3% 11 / 21 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 93.7% 15 / 16 Podpod 100% 2 / 2 totaltotal 64.1% 168 / 262 CC 48 SCAR 42.2
CC
48
Cov
65%
run
132.2
_open3_run
12.3
__ANON__
12.0
CRAP 68.2
« blib/lib/IPC/Cmd.pm ↑ Summary blib/lib/IPC/Cmd/System.pm »
1 package IPC::Cmd;
2
3 1 ▶use Params::Check qw[check];
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Module::Load::Conditional qw[can_load];
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Locale::Maketext::Simple Style => 'gettext';
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 1 ▶use ExtUtils::MakeMaker();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 1 require Carp;
14 1 $Carp::CarpLevel = 1;
15
16 BEGIN {
17 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 use vars qw[ @ISA $VERSION @EXPORT_OK $VERBOSE
19 $USE_IPC_RUN $USE_IPC_OPEN3
20 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
21
22 1 ▶ $VERSION = '0.22';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 $VERBOSE = 0;
24 1 $USE_IPC_RUN = 1;
25 1 $USE_IPC_OPEN3 = 1;
26
27 1 @ISA = qw[Exporter];
28 1 @EXPORT_OK = qw[can_run run];
29 }
30
31 ### check if we can run some command ###
32 sub can_run {
33 2 ▶ my $command = shift;
Subroutine can_run: called CC=4 SCAR=14.2
Pod documented
34
35 2 ▶ if( File::Spec->file_name_is_absolute($command) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($command)) { } 0 2 2
36 0 return MM->maybe_command($command);
37
38 } else {
39 2 for my $dir (split /$Config{path_sep}/, $ENV{PATH}) {
40 12 my $abs = File::Spec->catfile($dir, $command);
41 12 ▶ return $abs if $abs = MM->maybe_command($abs);
Branch: 1 branch
BranchTrueFalseTotal
if $abs = "MM"->maybe_command($abs) 1 11 12
42 }
43 }
44 }
45
46
47 ### Execute a command: $cmd may be a scalar or an arrayref of cmd and args
48 ### $bufout is an scalar ref to store outputs, $verbose can override conf
49 sub run {
50 24 ▶ my %hash = @_;
Subroutine run: called CC=31 SCAR=48.8
Pod documented
51
52 24 my $x = '';
53 my $tmpl = {
54 verbose => { default => $VERBOSE },
55 command => { required => 1,
56 24 ▶ allow => sub {my $cmd = pop();
Subroutine __ANON__: called CC=2 SCAR=6.9
57 24 ▶ !(ref $cmd) or ref $cmd eq 'ARRAY' }
Branch: 1 branch
BranchTrueFalseTotal
not ref $cmd or ref $cmd eq "ARRAY" 12 12 24
58 },
59 24 buffer => { default => \$x },
60 };
61
62 24 ▶ my $args = check( $tmpl, \%hash, $VERBOSE )
Branch: 1 branch
BranchTrueFalseTotal
my $args = check($tmpl, \%hash, $IPC::Cmd::VERBOSE) or warn(loc("Could not validate input!")), return 0 24 24
63 or ( warn(loc(q[Could not validate input!])), return );
64
65 ### Kludge! This enables autoflushing for each perl process we launched.
66 24 local $ENV{PERL5OPT} .= ' -MIPC::Cmd::System=autoflush=1';
67
68 24 my $verbose = $args->{verbose};
69 24 ▶ my $is_win98 = ($^O eq 'MSWin32' and !Win32::IsWinNT());
Condition33%1/3
$^O eq 'MSWin32' && !Win32::IsWinNT()
!ll&&!rl&&r
2400
MC/DC0%0/2
$^O eq 'MSWin32' && !Win32::IsWinNT()
$^O eq 'MSWin32' !Win32::IsWinNT()
Truth table33%1/3
A && not(B)
A= $^O eq 'MSWin32'B= !Win32::IsWinNT()
ABresult
0X0
100
111
70
71 24 my $err; # error flag
72 my $have_buffer; # to indicate we executed via IPC::Run
73 # or IPC::Open3 only then it makes sence
74 # to return the buffers
75
76 24 my (@buffer,@buferr,@bufout);
77
78 ### STDOUT message handler
79 my $_out_handler = sub {
80 #sub _out_handler {
81 8 ▶ my $buf = shift;
Subroutine __ANON__: called CC=3 SCAR=11.3
82 8 ▶ return unless defined $buf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buf 0 8 8
83
84 8 ▶ print STDOUT $buf if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 8 8
85 8 push @buffer, $buf;
86 8 push @bufout, $buf;
87 24 };
88
89 ### STDERR message handler
90 my $_err_handler = sub {
91 #sub _err_handler {
92 0 ▶ my $buf = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
93 0 ▶ return unless defined $buf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buf 0 0 0
94
95 0 ▶ print STDERR $buf if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
96 0 push @buffer, $buf;
97 0 push @buferr, $buf;
98 24 };
99
100 24 my $cmd = $args->{command};
101 24 ▶ my @cmd = ref ($cmd) ? grep(length, @{$cmd}) : $cmd;
Branch: 1 branch
BranchTrueFalseTotal
ref $cmd ? : 12 12 24
102
103 24 ▶ print loc(qq|Running [%1]...\n|,"@cmd") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 24 24
104
105 ### First, we prefer Barrie Slaymaker's wonderful IPC::Run module.
106 24 ▶ if (!$is_win98 and $USE_IPC_RUN and
Branch: 3 branches
BranchTrueFalseTotal
if (not $is_win98 and $IPC::Cmd::USE_IPC_RUN and can_load('modules', {'IPC::Run', '0.55'}, 'verbose', $verbose && $^O eq 'MSWin32')) { } 0 24 24
elsif (not $^O =~ /^(?:MSWin32|cygwin)$/ and $IPC::Cmd::USE_IPC_OPEN3 and can_load('modules', {map({$_, '0.0';} 'IPC::Open3', 'IO::Select', 'Symbol')}, 'verbose', $verbose)) { } 16 8 24
elsif ($verbose) { } 0 8 8
Condition55%5/9
not $is_win98 and $IPC::Cmd::USE_IPC_RUN and can_load("modules", {"IPC::Run", "0.55"}, "verbose", $verbose && $^O eq "MSWin32")
!ll&&!rl&&r
1680
not $is_win98 and $IPC::Cmd::USE_IPC_RUN
!ll&&!rl&&r
0168
$verbose && $^O eq 'MSWin32'
!ll&&!rl&&r
800
MC/DC0%0/3
not $is_win98 and $IPC::Cmd::USE_IPC_RUN and can_load("modules", {"IPC::Run", "0.55"}, "verbose", $verbose && $^O eq "MSWin32")
not $is_win98 $IPC::Cmd::USE_IPC_RUN can_load("modules", {"IPC::Run", "0.55"}, "verbose", $verbose && $^O eq "MSWin32")
MC/DC0%0/2
$verbose && $^O eq 'MSWin32'
$verbose $^O eq 'MSWin32'
Truth table50%2/4
not(A) and B and C
A= not $is_win98B= $IPC::Cmd::USE_IPC_RUNC= can_load("modules", {"IPC::Run", "0.55"}, "verbose", $verbose && $^O eq "MSWin32")
ABCresult
0XX0
10X0
1100
1111
Truth table33%1/3
A && B
A= $verboseB= $^O eq 'MSWin32'
ABresult
0X0
100
111
107 can_load(
108 modules => { 'IPC::Run' => '0.55' },
109 verbose => $verbose && ($^O eq 'MSWin32') )
110 ) {
111 0 STDOUT->autoflush(1); STDERR->autoflush(1);
112
113 0 $have_buffer++;
114
115 ### a command like:
116 # [
117 # '/usr/bin/gzip',
118 # '-cdf',
119 # '/Users/kane/sources/p4/other/archive-extract/t/src/x.tgz',
120 # '|',
121 # '/usr/bin/tar',
122 # '-tf -'
123 # ]
124 ### needs to become:
125 # [
126 # ['/usr/bin/gzip', '-cdf',
127 # '/Users/kane/sources/p4/other/archive-extract/t/src/x.tgz']
128 # '|',
129 # ['/usr/bin/tar', '-tf -']
130 # ]
131
132 0 my @command; my $special_chars;
133 0 ▶ if( ref $cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd) { } 0 0 0
134 0 my $aref = [];
135 0 for my $item (@cmd) {
136 0 ▶ if( $item =~ /[<>|&]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /[<>|&]/) { } 0 0 0
137 0 push @command, $aref, $item;
138 0 $aref = [];
139 0 $special_chars++;
140 } else {
141 0 push @$aref, $item;
142 }
143 }
144 0 push @command, $aref;
145 } else {
146 0 ▶ @command = map { if( /[<>|&]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/[<>|&]/) { } 0 0 0
147 0 $special_chars++; $_;
148 } else {
149 0 [ split / +/ ]
150 }
151 } split( /\s*([<>|&])\s*/, $cmd );
152 }
153
154 ### due to the double '>' construct, stdout buffers are now ending
155 ### up in the stderr buffer. this is a bug in IPC::Run.
156 ### Mailed barries about this early june, no solution yet :(
157 ### update (23-6-04): so this thing with the double > makes
158 ### this command not even fill any buffer:
159 ### perl -lewarn$$
160 ### so it looks like when there are no 'special' chars in the
161 ### command, like '|' and friends, best not use the '>' construct.
162 0 ▶ if( $special_chars ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($special_chars) { } 0 0 0
163 0 ▶ IPC::Run::run(@command, \*STDIN, '>', $_out_handler,
Branch: 1 branch
BranchTrueFalseTotal
IPC::Run::run(@command, \*STDIN, ">", $_out_handler, ">", $_err_handler) or ++$err 0 0 0
164 '>', $_err_handler) or $err++;
165 } else {
166 0 ▶ IPC::Run::run(@command, \*STDIN, $_out_handler,
Branch: 1 branch
BranchTrueFalseTotal
IPC::Run::run(@command, \*STDIN, $_out_handler, $_err_handler) or ++$err 0 0 0
167 $_err_handler) or $err++;
168 }
169
170
171 ### Next, IPC::Open3 is know to fail on Win32, but works on Un*x.
172 ▶ } elsif ( $^O !~ /^(?:MSWin32|cygwin)$/
Condition66%4/6
not $^O =~ /^(?:MSWin32|cygwin)$/ and $IPC::Cmd::USE_IPC_OPEN3 and can_load("modules", {map({$_, "0.0";} "IPC::Open3", "IO::Select", "Symbol")}, "verbose", $verbose)
!ll&&!rl&&r
8016
not $^O =~ /^(?:MSWin32|cygwin)$/ and $IPC::Cmd::USE_IPC_OPEN3
!ll&&!rl&&r
0816
MC/DC33%1/3
not $^O =~ /^(?:MSWin32|cygwin)$/ and $IPC::Cmd::USE_IPC_OPEN3 and can_load("modules", {map({$_, "0.0";} "IPC::Open3", "IO::Select", "Symbol")}, "verbose", $verbose)
not $^O =~ /^(?:MSWin32|cygwin)$/ $IPC::Cmd::USE_IPC_OPEN3 can_load("modules", {map({$_, "0.0";} "IPC::Open3", "IO::Select", "Symbol")}, "verbose", $verbose)
Truth table50%2/4
not(A) and B and C
A= not $^O =~ /^(?:MSWin32|cygwin)$/B= $IPC::Cmd::USE_IPC_OPEN3C= can_load("modules", {map({$_, "0.0";} "IPC::Open3", "IO::Select", "Symbol")}, "verbose", $verbose)
ABCresult
0XX0
10X0
1100
1111
173 and $USE_IPC_OPEN3
174 and can_load(
175 48 modules => { map{$_ => '0.0'}
176 qw|IPC::Open3 IO::Select Symbol| },
177 verbose => $verbose
178 ) ) {
179 16 my $rv;
180 16 ($rv,$err) = _open3_run(\@cmd, $_out_handler, $_err_handler);
181 16 $have_buffer++;
182
183
184 ### Abandon all hope; falls back to simple system() on verbose calls.
185 } elsif ($verbose) {
186 0 system(@cmd);
187 0 ▶ $err = $? ? $? : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? ? : 0 0 0
188
189 ### Non-verbose system() needs to have STDOUT and STDERR muted.
190 } else {
191 8 local *SAVEOUT; local *SAVEERR;
192
193 8 ▶ open(SAVEOUT, ">&STDOUT")
Branch: 1 branch
BranchTrueFalseTotal
open SAVEOUT, ">&STDOUT" or warn(loc("couldn't dup STDOUT: %1", $!)), return 0 8 8
194 or warn(loc("couldn't dup STDOUT: %1",$!)), return;
195 8 ▶ open(STDOUT, ">".File::Spec->devnull)
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, '>' . "File::Spec"->devnull or warn "couldn't reopen STDOUT: $!", return 0 8 8
196 or warn "couldn't reopen STDOUT: $!", return;
197
198 8 ▶ open(SAVEERR, ">&STDERR")
Branch: 1 branch
BranchTrueFalseTotal
open SAVEERR, ">&STDERR" or warn(loc("couldn't dup STDERR: %1", $!)), return 0 8 8
199 or warn(loc("couldn't dup STDERR: %1",$!)), return;
200 8 ▶ open(STDERR, ">".File::Spec->devnull)
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, '>' . "File::Spec"->devnull or warn(loc("couldn't reopen STDERR: %1", $!)), return 0 8 8
201 or warn(loc("couldn't reopen STDERR: %1",$!)), return;
202
203 8 system(@cmd);
204
205 8 ▶ open(STDOUT, ">&SAVEOUT")
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&SAVEOUT" or warn(loc("couldn't restore STDOUT: %1", $!)), return 0 8 8
206 or warn(loc("couldn't restore STDOUT: %1",$!)), return;
207 8 ▶ open(STDERR, ">&SAVEERR")
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&SAVEERR" or warn(loc("couldn't restore STDERR: %1", $!)), return 0 8 8
208 or warn(loc("couldn't restore STDERR: %1",$!)), return;
209 }
210
211 ### unless $err has been set from _open3_run, set it to $? ###
212 24 ▶ $err ||= $?;
Condition33%1/3
$err ||= $?
l!l&&r!l&&!r
0024
MC/DC0%0/2
$err ||= $?
$err $?
Truth table33%1/3
A ||= B
A= $errB= $?
ABresult
1X1
011
000
213
214 24 ▶ if ( scalar @buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @buffer) 8 16 24
215 8 my $capture = $args->{buffer};
216 8 $$capture = join '', @buffer;
217 }
218
219 return wantarray
220 24 ▶ ? $have_buffer
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 12 12 24
$have_buffer ? : 8 4 12
221 ? (!$err, $?, \@buffer, \@bufout, \@buferr)
222 : (!$err, $? )
223 : !$err
224 }
225
226
227 ### IPC::Run::run emulator, using IPC::Open3.
228 sub _open3_run {
229 16 ▶ my ($cmdref, $_out_handler, $_err_handler, $verbose) = @_;
Subroutine _open3_run: called CC=10 SCAR=25.1
230
231 ### in case there are pipes in there;
232 ### IPC::Open3 will call exec and exec will do the right thing ###
233 16 my $cmd = join " ", @$cmdref;
234
235 ### Following code are adapted from Friar 'abstracts' in the
236 ### Perl Monastery (http://www.perlmonks.org/index.pl?node_id=151886).
237
238 16 my ($infh, $outfh, $errfh); # open3 handles
239
240 16 my $pid = eval {
241 16 IPC::Open3::open3(
242 $infh = Symbol::gensym(),
243 $outfh = Symbol::gensym(),
244 $errfh = Symbol::gensym(),
245 $cmd,
246 )
247 };
248
249
250 16 ▶ return (undef, $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 16 16
251
252 16 my $sel = IO::Select->new; # create a select object
253 16 $sel->add($outfh, $errfh); # and add the fhs
254
255 16 STDOUT->autoflush(1); STDERR->autoflush(1);
256 16 ▶ $outfh->autoflush(1) if UNIVERSAL::can($outfh, 'autoflush');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($outfh, "autoflush") 16 0 16
257 16 ▶ $errfh->autoflush(1) if UNIVERSAL::can($errfh, 'autoflush');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($errfh, "autoflush") 16 0 16
258
259 16 ▶ while (my @ready = $sel->can_read) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $sel->can_read) 25 16 41
260 25 foreach my $fh (@ready) { # loop through buffered handles
261 # read up to 4096 bytes from this fh.
262 40 my $len = sysread $fh, my($buf), 4096;
263
264 40 ▶ if (not defined $len){
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $len) { } 0 40 40
elsif ($len == 0) { } 32 8 40
elsif ($fh == $outfh) { } 8 0 8
elsif ($fh == $errfh) { } 0 0 0
265 # There was an error reading
266 0 warn loc("Error from child: %1",$!);
267 0 return(undef, $!);
268 }
269 elsif ($len == 0){
270 32 $sel->remove($fh); # finished reading
271 32 next;
272 }
273 elsif ($fh == $outfh) {
274 8 $_out_handler->($buf);
275 } elsif ($fh == $errfh) {
276 0 $_err_handler->($buf);
277 } else {
278 0 warn loc("%1 error", 'IO::Select');
279 0 return(undef, $!);
280 }
281 }
282 }
283
284 16 waitpid $pid, 0; # wait for it to die
285 16 return 1;
286 }
287
288 1 1;
289
290 __END__
« blib/lib/IPC/Cmd.pm ↑ Summary blib/lib/IPC/Cmd/System.pm »