lib/System/Wrapper.pm

filter: Statementstmt 10.1% 26 / 257 Branchbran 0.0% 0 / 146 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 16.3% 8 / 49 Podpod 6.2% 1 / 16 totaltotal 6.4% 35 / 545 CC 102 SCAR 41.2
CC
102
Cov
6%
_flatten
182.0
_program_in_path
156.0
_rename_from_tmp
132.0
CRAP 61.6
↑ Summary lib/System/Wrapper/Parallel.pm »
1 package System::Wrapper;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use overload q{""} => \&stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use constant MAX_RAND => 2 ** 32;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.0.2';
12  
13 ## CLASS METHODS
14 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
15 0 my ( $class, %args ) = @_;
16 0 my $self = bless( { map { $_ => undef }
17 qw/
18 interpreter executable arguments input output
19 description path order
20 progress spec capture
21 _interpreter _executable _arguments
22 _input _output _destroy
23 _fifo _tmp _input_type _output_type
24 debug verbose
25 /
26 }, $class);
27  
28 0 return $self->_init(%args);
29 }
30  
31 ## OBJECT METHODS
32 0 ▶sub _interpreter : lvalue { shift->{_interpreter} }
Subroutine _interpreter: not called CC=1 SCAR=6.9
33 0 ▶sub _executable : lvalue { shift->{_executable} }
Subroutine _executable: not called CC=1 SCAR=6.9
34 0 ▶sub _input : lvalue { shift->{_input} }
Subroutine _input: not called CC=1 SCAR=6.9
35 0 ▶sub _output : lvalue { shift->{_output} }
Subroutine _output: not called CC=1 SCAR=6.9
36 0 ▶sub _arguments : lvalue { shift->{_arguments} }
Subroutine _arguments: not called CC=1 SCAR=6.9
37 0 ▶sub _fifo : lvalue { shift->{_fifo} }
Subroutine _fifo: not called CC=1 SCAR=6.9
38 0 ▶sub _tmp : lvalue { shift->{_tmp} }
Subroutine _tmp: not called CC=1 SCAR=6.9
39 0 ▶sub _destroy : lvalue { shift->{_destroy} }
Subroutine _destroy: not called CC=1 SCAR=6.9
40 0 ▶sub _input_type : lvalue { shift->{_input_type} }
Subroutine _input_type: not called CC=1 SCAR=6.9
41 0 ▶sub _output_type : lvalue { shift->{_output_type} }
Subroutine _output_type: not called CC=1 SCAR=6.9
42 0 ▶sub verbose : lvalue { shift->{verbose} }
Subroutine verbose: not called CC=1 SCAR=6.9
Pod undocumented
43 0 ▶sub debug : lvalue { shift->{debug} }
Subroutine debug: not called CC=1 SCAR=6.9
Pod undocumented
44  
45 0 ▶sub interpreter {
Subroutine interpreter: not called CC=3 SCAR=24.8
Pod undocumented
46 0 my ( $self, $interpreter ) = @_;
47  
48 0 ▶ if ( defined $interpreter ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $interpreter) 0 0 0
49 0 $self->{interpreter} = $interpreter;
50 }
51  
52 0 $self->_interpreter = $self->_program_in_path( $self->{interpreter}, 1 );
53  
54 0 ▶ return $self->_interpreter || $self->{interpreter};
Condition0%0/3
$self->_interpreter || $self->{'interpreter'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_interpreter || $self->{'interpreter'}
$self->_interpreter $self->{'interpreter'}
Truth table0%0/3
A || B
A= $self->_interpreterB= $self->{'interpreter'}
ABresult
1X1
011
000
55 }
56  
57 0 ▶sub executable {
Subroutine executable: not called CC=3 SCAR=24.8
Pod undocumented
58 0 my ( $self, $executable ) = @_;
59  
60 0 ▶ if ( defined $executable ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $executable) 0 0 0
61 0 $self->{executable} = $executable;
62 }
63  
64 0 $self->_executable = $self->_program_in_path( $self->{executable} );
65  
66 0 ▶ return $self->_executable || $self->{executable};
Condition0%0/3
$self->_executable || $self->{'executable'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_executable || $self->{'executable'}
$self->_executable $self->{'executable'}
Truth table0%0/3
A || B
A= $self->_executableB= $self->{'executable'}
ABresult
1X1
011
000
67 }
68  
69 0 ▶sub arguments {
Subroutine arguments: not called CC=4 SCAR=30.0
Pod undocumented
70 0 my ( $self, $args ) = @_;
71  
72 0 ▶ if ( defined $args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args) 0 0 0
73 0 $self->{arguments} = $args;
74 $self->_arguments = $self->_flatten( $self->{arguments} )
75 0 }
76  
77 0 ▶ return wantarray ? @{ $self->{arguments} ||= [] } : $self->_arguments;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
Condition0%0/2
$self->{"arguments"} ||= []
l!l
00
MC/DC0%0/1
$self->{"arguments"} ||= []
$self->{"arguments"}
Truth table0%0/2
A
A= $self->{"arguments"}
Aresult
11
00
78 }
79  
80 0 ▶sub input {
Subroutine input: not called CC=1 SCAR=6.9
Pod undocumented
81 0 my ( $self, $input ) = @_;
82 0 return $self->_io_spec( $input, 'input' );
83 }
84  
85 0 ▶sub output {
Subroutine output: not called CC=1 SCAR=6.9
Pod undocumented
86 0 my ( $self, $output ) = @_;
87 0 return $self->_io_spec( $output, 'output' );
88 }
89  
90 0 ▶sub _io_spec {
Subroutine _io_spec: not called CC=4 SCAR=30.0
91 0 my ($self, $spec, $io) = @_;
92  
93 0 my $private_method = "_$io";
94  
95 0 ▶ if ( defined $spec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec) 0 0 0
96  
97 0 my $io_type = $private_method . '_type';
98 0 ▶ $self->$io_type = ref $spec || 'SCALAR';
Condition0%0/2
ref $spec || 'SCALAR'
l!l
00
MC/DC0%0/1
ref $spec || 'SCALAR'
ref $spec
Truth table0%0/2
A
A= ref $spec
Aresult
11
00
99 0 $self->{$io} = $spec;
100 0 $self->$private_method = $self->_flatten( $self->{$io} );
101 }
102  
103 0 ▶ return wantarray ? $self->_deref( $self->{$io} ) : $self->$private_method;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
104 }
105  
106 0 ▶sub path {
Subroutine path: not called CC=7 SCAR=40.3
Pod undocumented
107 0 my ( $self, $path ) = @_;
108  
109 0 ▶ if ( defined $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) 0 0 0
110 0 ▶ $path = [$path] if 'SCALAR' eq ref \$path;
Branch: 1 branch
BranchTrueFalseTotal
if "SCALAR" eq ref \$path 0 0 0
111  
112 0 for (@$path) {
113 0 ▶ _err(
Branch: 1 branch
BranchTrueFalseTotal
unless -d $_ 0 0 0
114 "type of arg 1 to 'path' must be directory or reference to array of directories (not non-directory '%s')",
115 $_
116 ) unless -d $_;
117 }
118  
119 0 $self->{path} = $path;
120 }
121 0 ▶ return wantarray ? @{ $self->{path} ||= [] } : $self->{path};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
Condition0%0/2
$self->{"path"} ||= []
l!l
00
MC/DC0%0/1
$self->{"path"} ||= []
$self->{"path"}
Truth table0%0/2
A
A= $self->{"path"}
Aresult
11
00
122 }
123  
124 0 ▶sub capture {
Subroutine capture: not called CC=2 SCAR=17.9
Pod undocumented
125 0 my ( $self, $capture ) = @_;
126  
127 0 ▶ if ($capture) {
Branch: 1 branch
BranchTrueFalseTotal
if ($capture) 0 0 0
128 0 $self->{capture} = $capture;
129 }
130 0 return $self->{capture};
131 }
132  
133 0 ▶sub description {
Subroutine description: not called CC=2 SCAR=17.9
Pod undocumented
134 0 my ( $self, $description ) = @_;
135  
136 0 ▶ if ($description) {
Branch: 1 branch
BranchTrueFalseTotal
if ($description) 0 0 0
137 0 $self->{description} = $description;
138 }
139 0 return $self->{description};
140 }
141  
142 0 ▶sub progress {
Subroutine progress: not called CC=2 SCAR=17.9
Pod undocumented
143 0 my ( $self, $progress ) = @_;
144  
145 0 ▶ if ($progress) {
Branch: 1 branch
BranchTrueFalseTotal
if ($progress) 0 0 0
146 0 $self->{progress} = $progress;
147 }
148 0 return $self->{progress};
149 }
150  
151 0 ▶sub order {
Subroutine order: not called CC=9 SCAR=45.0
Pod undocumented
152 0 my ( $self, $order ) = @_;
153  
154 0 ▶ if ( defined $order ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $order) 0 0 0
155 0 ▶ if ( 'ARRAY' eq ref $order ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $order) { } 0 0 0
156 0 ▶ if (@$order) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$order) { } 0 0 0
157 _err(
158 "parameter 1 to 'order' must have at most %s elements, not %s",
159 0 scalar @{ $self->{order} },
160 scalar @$order
161 0 ▶ ) if scalar @{ $self->{order} } < scalar @$order;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"order"};} < scalar @$order 0 0 0
162  
163 0 my %spec = map { $_ => 1 } @{ $self->{order} };
164  
165 0 for ( 0 .. @{ $self->{order} } - 1 ) {
166 _err(
167 "parameter 1 values to 'order' must be any of [%s], not '%s'",
168 join( q{, }, sort keys %spec ),
169 $order->[$_]
170 0 ▶ ) unless exists $spec{ $order->[$_] };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $spec{$order->[$_]} 0 0 0
171 }
172 0 $self->{order} = $order;
173 }
174 else {
175 }
176 }
177 else {
178 0 ▶ _err( "type of parameter 1 to 'order' must be ARRAY, not %s",
Condition0%0/3
ref $order || ref \$order
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $order || ref \$order
ref $order ref \$order
Truth table0%0/3
A || B
A= ref $orderB= ref \$order
ABresult
1X1
011
000
179 ref $order || ref \$order );
180 }
181 }
182 0 ▶ return wantarray ? @{ $self->{order} } : $self->{order};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
183 }
184  
185 0 ▶sub command {
Subroutine command: not called CC=2 SCAR=17.9
Pod undocumented
186 0 my ($self) = @_;
187  
188 0 my @command = grep {$_}
189 0 map { scalar $self->$_ } $self->order;
190  
191 0 ▶ return wantarray ? @command : "@command";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
192 }
193  
194 0 ▶sub stringify {
Subroutine stringify: not called CC=2 SCAR=17.9
Pod undocumented
195 0 my ($self) = @_;
196  
197 0 ▶ my $out = $self->description ? q{# } . $self->description . qq{\n}
Branch: 1 branch
BranchTrueFalseTotal
$self->description ? : 0 0 0
198 : q{}
199 . $self->command . qq{\n};
200  
201 0 return $out;
202 }
203  
204 0 ▶sub run {
Subroutine run: not called CC=5 SCAR=34.0
Pod undocumented
205 0 my ($self) = @_;
206  
207 0 ▶ if ($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
208 0 $self->_rename_to_tmp;
209 0 print STDERR $self->stringify;
210 0 $self->_rename_from_tmp;
211 0 return;
212 }
213  
214 0 ▶ if ($self->verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 0 0
215 0 print STDERR $self->stringify;
216 }
217  
218 0 $self->_can_run;
219  
220 0 $self->_rename_to_tmp;
221  
222 0 my @command = $self->command;
223  
224 0 my $stdout;
225 0 ▶ eval { $stdout = $self->capture ? qx/"@command"/ : system "@command" };
Branch: 1 branch
BranchTrueFalseTotal
$self->capture ? : 0 0 0
226  
227 0 ▶ $self->_did_run( $@, $? ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_did_run($@, $?) or return 0 0 0
228  
229 0 $self->_rename_from_tmp;
230  
231 0 $self->_destroy = 1;
232  
233 0 return $stdout;
234 }
235  
236 ## INTERNAL METHODS
237 0 ▶sub _init {
Subroutine _init: not called CC=4 SCAR=30.0
238 0 my ( $self, %args ) = @_;
239  
240 0 $self->{order} = [qw/interpreter executable arguments input output/];
241 0 $self->{path} = [ grep {$_} File::Spec->path, q{.} ];
242 0 $self->{_tmp} = int rand MAX_RAND;
243  
244 0 ▶ while ( my ( $key, $value ) = each %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 0 0 0
245  
246 _err( "Can't access '%s' field in class '%s'",
247 $key, ref $self )
248 0 ▶ if !exists $self->{$key}
Branch: 1 branch
BranchTrueFalseTotal
if not exists $self->{$key} or $key =~ /^_/ 0 0 0
Condition0%0/3
not exists $self->{$key} or $key =~ /^_/
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $self->{$key} or $key =~ /^_/
not exists $self->{$key} $key =~ /^_/
Truth table0%0/3
not(A) or B
A= not exists $self->{$key}B= $key =~ /^_/
ABresult
1X1
011
000
249 or $key =~ m/^_/;
250  
251 0 $self->$key($value);
252 }
253  
254 0 return $self;
255 }
256  
257 0 ▶sub _progress {
Subroutine _progress: not called CC=4 SCAR=30.0
258 0 my ($self) = @_;
259  
260 0 ▶ return unless $self->progress;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->progress 0 0 0
261  
262 0 ▶ _err("need input to be set to track progress")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->input 0 0 0
263 unless $self->input;
264  
265 0 my $input_size = 0;
266  
267 0 $input_size += -s $_ for $self->input;
268 }
269  
270 0 ▶sub _tmp_name {
Subroutine _tmp_name: not called CC=1 SCAR=6.9
271 0 my ($self, $name) = @_;
272 0 return $name . '.part' . $self->_tmp;
273 }
274  
275 0 ▶sub _rename_to_tmp {
Subroutine _rename_to_tmp: not called CC=10 SCAR=44.7
276 0 my ($self) = @_;
277  
278 0 ▶ return unless $self->output;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->output 0 0 0
279  
280 0 ▶ if ('HASH' eq $self->_output_type) {
Branch: 3 branches
BranchTrueFalseTotal
if ('HASH' eq $self->_output_type) { } 0 0 0
elsif ('ARRAY' eq $self->_output_type) { } 0 0 0
elsif ('SCALAR' eq $self->_output_type) { } 0 0 0
281 0 my %output = $self->output;
282 NAME:
283 0 for my $spec ( keys %output ) {
284  
285 2 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
286 0 ▶ print STDERR Dumper \%output if not defined $output{$spec};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output{$spec} 0 0 0
287  
288 0 ▶ next NAME if -p $output{$spec};
Branch: 1 branch
BranchTrueFalseTotal
if -p $output{$spec} 0 0 0
289 0 my $tmp = $self->_tmp_name($output{$spec});
290  
291 0 $output{$spec} = $tmp;
292 }
293 0 $self->output(\%output);
294 }
295 elsif ('ARRAY' eq $self->_output_type) {
296 0 my @output = $self->output;
297 NAME:
298 0 for my $name (@output) {
299 0 ▶ next NAME if -p $name;
Branch: 1 branch
BranchTrueFalseTotal
if -p $name 0 0 0
300 0 my $tmp = $self->_tmp_name($name);
301  
302 0 $name = $tmp;
303 }
304 0 $self->output(\@output);
305 }
306 elsif ('SCALAR' eq $self->_output_type) {
307 0 my ($name) = $self->output;
308 0 my $tmp = $self->_tmp_name($name);
309 0 $self->output($tmp);
310 }
311 else {
312 0 _err(
313 "%s is not an accepted output type. It should be impossible to get this error",
314 $self->_output_type
315 );
316 }
317 }
318  
319 0 ▶sub _rename_from_tmp {
Subroutine _rename_from_tmp: not called CC=11 SCAR=48.8
320 0 my ($self) = @_;
321  
322 0 ▶ return unless $self->output;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->output 0 0 0
323  
324 0 my (@name, @tmp);
325  
326 0 ▶ if ('HASH' eq $self->_output_type) {
Branch: 3 branches
BranchTrueFalseTotal
if ('HASH' eq $self->_output_type) { } 0 0 0
elsif ('ARRAY' eq $self->_output_type) { } 0 0 0
elsif ('SCALAR' eq $self->_output_type) { } 0 0 0
327 0 my %output = $self->output;
328 NAME:
329 0 for my $spec ( keys %output ) {
330 0 push @tmp, $output{$spec};
331 0 push @name, $output{$spec};
332 0 $name[-1] =~ s/\.part.+//;
333 }
334 }
335 elsif ('ARRAY' eq $self->_output_type) {
336 0 my @output = $self->output;
337 NAME:
338 0 for my $tmp (@output) {
339 0 push @tmp, $tmp;
340 0 push @name, $tmp;
341 0 $name[-1] =~ s/\.part.+//;
342 }
343 }
344 elsif ('SCALAR' eq $self->_output_type) {
345 0 push @tmp, $self->output;
346 0 push @name, $tmp[-1];
347 0 $name[-1] =~ s/\.part.+//;
348 }
349 else {
350 0 _err(
351 "%s is not an accepted output type. It should be impossible to get this error",
352 $self->_output_type
353 );
354 }
355  
356 NAME:
357 0 for (0 .. @name - 1) {
358 0 my ($name, $tmp) = ($name[$_], $tmp[$_]);
359 0 ▶ next NAME if -p $name;
Branch: 1 branch
BranchTrueFalseTotal
if -p $name 0 0 0
360  
361 0 my $mv = System::Wrapper->new(
362 interpreter => 'perl',
363 arguments => qq{-e 'rename "$tmp", "$name" or die "\$!"'},
364 );
365 0 ▶ $mv->verbose = 1 if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
366 0 ▶ $mv->debug = 1 if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
367 0 $mv->run;
368 }
369 }
370  
371 0 ▶sub _can_run {
Subroutine _can_run: not called CC=7 SCAR=40.3
372 0 my ($self) = @_;
373  
374 0 ▶ if ( my $nonexistent_file = $self->_inputs_not_available ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $nonexistent_file = $self->_inputs_not_available) 0 0 0
375 0 ▶ _err( "%s %s", $nonexistent_file,
Branch: 1 branch
BranchTrueFalseTotal
-e $nonexistent_file ? : 0 0 0
376 -e $nonexistent_file
377 ? 'is not readable'
378 : 'does not exist' );
379 }
380  
381 0 ▶ _err("need interpreter or executable to be set to run")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interpreter or $self->executable 0 0 0
Condition0%0/3
$self->interpreter or $self->executable
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->interpreter or $self->executable
$self->interpreter $self->executable
Truth table0%0/3
A or B
A= $self->interpreterB= $self->executable
ABresult
1X1
011
000
382 unless $self->interpreter
383 or $self->executable;
384  
385 0 ▶ _err( "need interpreter '%s' or executable '%s' to be in path to run",
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_interpreter or $self->_executable 0 0 0
Condition0%0/3
$self->_interpreter or $self->_executable
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_interpreter or $self->_executable
$self->_interpreter $self->_executable
Truth table0%0/3
A or B
A= $self->_interpreterB= $self->_executable
ABresult
1X1
011
000
386 $self->interpreter, $self->executable )
387 unless $self->_interpreter
388 or $self->_executable;
389  
390 0 return 1;
391 }
392  
393 0 ▶sub _did_run {
Subroutine _did_run: not called CC=4 SCAR=30.0
394 0 my ( $self, $eval_error, $child_error ) = @_;
395  
396 0 ▶ _err( "failed to run command:\n%s\n%s",
Branch: 1 branch
BranchTrueFalseTotal
if $eval_error 0 0 0
397 scalar $self->command, $eval_error )
398 if $eval_error;
399  
400 0 ▶ _err(
Branch: 2 branches
BranchTrueFalseTotal
if $child_error 0 0 0
$child_error & 255 ? : 0 0 0
401 "failed to run command (%s):\n%s",
402 ( $child_error & 255 )
403 ? 'signal ' . ( $child_error & 255 )
404 : 'exit ' . ( $child_error >> 8 ),
405 scalar $self->command
406 ) if $child_error;
407  
408 0 return 1;
409 }
410  
411 0 ▶sub _inputs_not_available {
Subroutine _inputs_not_available: not called CC=3 SCAR=24.8
412 0 my ($self) = @_;
413  
414 0 for ( $self->input ) {
415 0 ▶ return $_ unless -r $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $_ 0 0 0
416 }
417  
418 0 return;
419 }
420  
421 0 ▶sub _program_in_path {
Subroutine _program_in_path: not called CC=12 SCAR=50.5
422 0 my ( $self, $program, $is_executable ) = @_;
423  
424 0 ▶ return unless $program;
Branch: 1 branch
BranchTrueFalseTotal
unless $program 0 0 0
425  
426 0 my ( $vol, $dir, $file ) = File::Spec->splitpath($program);
427  
428 0 ▶ return $program
Branch: 1 branch
BranchTrueFalseTotal
if $dir and -f $program || -l $program and !defined($is_executable) || -x $program 0 0 0
Condition0%0/12
$dir and -f $program || -l $program and !defined($is_executable) || -x $program
!ll&&!rl&&r
000
$dir and -f $program || -l $program
!ll&&!rl&&r
000
-f $program or -l $program
l!l&&r!l&&!r
000
not defined $is_executable or -x $program
l!l&&r!l&&!r
000
MC/DC0%0/5
$dir and -f $program || -l $program and !defined($is_executable) || -x $program
$dir -f $program -l $program not defined $is_executable -x $program
Truth table0%0/8
A and B or C and not(D) or E
A= $dirB= -f $programC= -l $programD= not defined $is_executableE= -x $program
ABCDEresult
0XXXX0
100XX0
11X000
101000
11X1X1
11X011
1011X1
101011
429 if $dir
430 and ( -f $program or -l $program )
431 and ( not defined $is_executable or -x $program );
432  
433 0 for ( @{ $self->path } ) {
434 0 my $path = File::Spec->catfile( $_, $program );
435  
436 0 ▶ return $path
Branch: 1 branch
BranchTrueFalseTotal
if -f $path || -l $path and !defined($is_executable) || -x $path 0 0 0
Condition0%0/9
-f $path || -l $path and !defined($is_executable) || -x $path
!ll&&!rl&&r
000
-f $path or -l $path
l!l&&r!l&&!r
000
not defined $is_executable or -x $path
l!l&&r!l&&!r
000
MC/DC0%0/4
-f $path || -l $path and !defined($is_executable) || -x $path
-f $path -l $path not defined $is_executable -x $path
Truth table0%0/7
A or B and not(C) or D
A= -f $pathB= -l $pathC= not defined $is_executableD= -x $path
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
437 if ( -f $path or -l $path )
438 and ( not defined $is_executable or -x $path );
439 }
440  
441 0 return q{};
442 }
443  
444 0 ▶sub _deref {
Subroutine _deref: not called CC=6 SCAR=37.4
445 0 my ($self, $struct) = @_;
446  
447 0 ▶ return unless $struct;
Branch: 1 branch
BranchTrueFalseTotal
unless $struct 0 0 0
448 0 ▶ return $struct unless ref $struct;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $struct 0 0 0
449 0 ▶ return $$struct if 'SCALAR' eq ref $struct;
Branch: 1 branch
BranchTrueFalseTotal
if "SCALAR" eq ref $struct 0 0 0
450 0 ▶ return @$struct if 'ARRAY' eq ref $struct;
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" eq ref $struct 0 0 0
451 0 ▶ return %$struct if 'HASH' eq ref $struct;
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq ref $struct 0 0 0
452 }
453  
454 0 ▶sub _flatten {
Subroutine _flatten: not called CC=13 SCAR=52.0
455 0 my ( $self, $struct ) = @_;
456  
457 0 ▶ return $struct unless ref $struct;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $struct 0 0 0
458  
459 0 eval {
460 0 require Storable;
461 0 Storable->import();
462 };
463 0 ▶ _err("Storable module required: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
464  
465 0 $struct = Storable::dclone($struct);
466  
467 0 my @expanded;
468 0 ▶ while ( ref $struct ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $struct) 0 0 0
469  
470 0 ▶ if ( 'ARRAY' eq ref $struct ) {
Branch: 3 branches
BranchTrueFalseTotal
if ('ARRAY' eq ref $struct) { } 0 0 0
elsif ('HASH' eq ref $struct) { } 0 0 0
elsif ('SCALAR' eq ref $struct) { } 0 0 0
471 0 ▶ last unless @$struct;
Branch: 1 branch
BranchTrueFalseTotal
unless @$struct 0 0 0
472 0 push @expanded, $self->_flatten( shift @$struct );
473 }
474  
475 elsif ( 'HASH' eq ref $struct ) {
476 0 ▶ last unless %$struct;
Branch: 1 branch
BranchTrueFalseTotal
unless %$struct 0 0 0
477 0 my ( $key, $value ) = each %$struct;
478 0 ▶ return unless defined $key and defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and defined $value 0 0 0
Condition0%0/3
defined $key and defined $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $key and defined $value
defined $key defined $value
Truth table0%0/3
A and B
A= defined $keyB= defined $value
ABresult
0X0
100
111
479 0 push @expanded, $key, $self->_flatten($value);
480 0 delete $struct->{$key};
481 }
482  
483 elsif ( 'SCALAR' eq ref $struct ) {
484 0 ▶ last unless $$struct;
Branch: 1 branch
BranchTrueFalseTotal
unless $$struct 0 0 0
485 0 push @expanded, $self->_flatten($$struct);
486 0 $struct = undef;
487 }
488 else {
489 0 _err(
490 "type of arg 1 to '_flatten' must be a scalar, hash or array reference (not '%s')",
491 ref $struct
492 );
493 }
494 }
495 0 @expanded = grep $_, @expanded;
496 0 ▶ return wantarray ? @expanded : "@expanded";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
497 }
498  
499 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=4 SCAR=25.6
500 0 my ($self) = @_;
501  
502 0 ▶ return unless $self->_destroy;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_destroy 0 0 0
503  
504 0 ▶ if ( $self->_tmp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_tmp) 0 0 0
505 0 my ( undef, $out_dir, undef )
506 = File::Spec->splitpath( $self->output );
507 0 my $out_files = File::Spec->catfile( $out_dir, q{*} . $self->_tmp );
508  
509 0 ▶ unlink glob $out_files;
Subroutine BEGIN: called CC=1 SCAR=0.2
510 }
511  
512 0 ▶ if ( $self->_fifo ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_fifo) 0 0 0
513 0 unlink glob $self->_fifo;
514 }
515 }
516  
517 ## INTERNAL SUBROUTINES
518 0 ▶sub _this_sub_name {
Subroutine _this_sub_name: not called CC=2 SCAR=17.9
519 0 ▶ return ( caller( shift || 1 ) )[3];
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
520 }
521  
522 0 ▶sub _err {
Subroutine _err: not called CC=1 SCAR=6.9
523 0 my ( $spec, @args ) = @_;
524 0 croak sprintf "%s error: $spec", _this_sub_name(2), @args;
525 }
526  
527  
528 2 1; # Magic true value required at end of module
529  
530  
531 =head1 NAME
532  
533 System::Wrapper - Class-wrapped system calls and qx operator
534  
535 =head1 VERSION
536  
537 This document describes System::Wrapper version 0.0.2
538  
539  
540 =head1 SYNOPSIS
541  
542 use System::Wrapper;
543  
544 my $command = System::Wrapper->new();
545  
546 $command->interpreter( 'perl');
547 $command->executable( 'program.pl');
548 $command->arguments( [ 'first', {second_a => 2, second_b => 2}, {third => [1,2,3]} ] );
549 $command->input( \@ARGV );
550 $command->output( { '--output' => 'file'}, q{>} => 'file2' );
551 $command->path( [$command->path, q{.}] );
552 $command->capture = 1;
553 $command->verbose = 1;
554 print $command->command;
555 $command->run;
556  
557  
558 =head1 DESCRIPTION
559  
560 This module wraps perl's C<system> call and c<qx> operator in an object-oriented
561 interface. It provides utility methods for accomplishing things that are not very
562 simple in C<system> and C<qx>. This includes in-situ I/O and call success via
563 temporary filenames, C<system> call progress estimation, finding whether the
564 executable and-or interpreter are on the path, validating filenames, cross-platform
565 output operators and argument type specification.
566  
567 This module can be used as a generic wrapper around C<system> and C<qx>, or as
568 a base class for building interfaces to utilities not available to C<perl> itself.
569  
570 =head1 INTERFACE
571  
572 =head2 CLASS METHODS
573  
574 =over
575  
576 =item new(%args)
577  
578 my %args = (
579 interpreter => undef, # optional: string
580 executable => undef, # required: string
581 arguments => undef, # optional: any nested structure of hashes,
582 # arrays or scalar references
583 input => undef, # optional: scalar or array reference
584 output => undef, # optional: hash reference of form { spec => file }
585 # eg: { '>' => 'out' } or { '--output' => 'out' }
586 capture => undef, # optional: return stdout, instead of exit code,
587 # via $self->run
588 path => [ grep $_, File::Spec->path, q{.} ]
589 # required: path of directories on which to look for
590 # interpreter and executable programs
591 );
592  
593 my $command = System::Wrapper->new(%args);
594  
595 =back
596  
597  
598 =head2 SELECTOR METHODS
599  
600 new
601 interpreter
602 executable
603 arguments
604 input
605 output
606 path
607 capture
608 command
609 run
610  
611 =head1 INSTALLATION
612  
613 To install this module type the following:
614  
615 perl Build.PL
616 Build
617 Build test
618 Build install
619  
620 or
621  
622 perl Makefile.PL
623 make
624 make test
625 make install
626  
627  
628 =head1 CONFIGURATION AND ENVIRONMENT
629  
630 System::Wrapper requires no configuration files or environment variables.
631  
632  
633 =head1 INCOMPATIBILITIES
634  
635 None reported.
636  
637  
638 =head1 BUGS AND LIMITATIONS
639  
640 No bugs have been reported.
641  
642 Please report any bugs or feature requests to
643 C<bug-system-wrapper@rt.cpan.org>, or through the web interface at
644 L<http://rt.cpan.org>.
645  
646 =head1 AUTHOR
647  
648 Pedro Silva C<< <psilva+pause@pedrosilva.pt> >>
649  
650  
651 =head1 LICENCE AND COPYRIGHT
652  
653 Copyright (c) 2010, Pedro Silva C<< <psilva+pause@pedrosilva.pt> >>. All rights reserved.
654  
655 This program is free software: you can redistribute it and/or modify
656 it under the terms of the GNU General Public License as published by
657 the Free Software Foundation, either version 3 of the License, or
658 (at your option) any later version.
659  
660 This program is distributed in the hope that it will be useful,
661 but WITHOUT ANY WARRANTY; without even the implied warranty of
662 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
663 GNU General Public License for more details.
664  
665 You should have received a copy of the GNU General Public License
666 along with this program. If not, see L<http://www.gnu.org/licenses/>.
↑ Summary lib/System/Wrapper/Parallel.pm »