blib/lib/IPC/PrettyPipe/Cmd.pm

filter: Statementstmt 94.6% 142 / 150 Branchbran 62.9% 39 / 62 Conditioncond 57.1% 12 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 100% 33 / 33 Podpod 75.0% 6 / 8 totaltotal 80.8% 232 / 287 CC 42 SCAR 19.0
CC
42
Cov
83%
add
14.4
valsubst
12.3
BUILDARGS
9.4
CRAP 6.7
↑ Summary blib/lib/IPC/PrettyPipe.pm »
1 package IPC::PrettyPipe::Cmd;
2  
3 # ABSTRACT: A command in an B<IPC::PrettyPipe> pipeline
4  
5 20 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 20 ▶use List::Util qw[ sum pairs ];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use Scalar::Util qw[ blessed ];
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 20 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 20 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 20 ▶use IPC::PrettyPipe::Arg;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 ▶use IPC::PrettyPipe::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 20 ▶use Types::Standard -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 20 ▶use Type::Params qw[ validate ];
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 20 ▶use IPC::PrettyPipe::Types -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 20 ▶use IPC::PrettyPipe::Queue;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 20 ▶use IPC::PrettyPipe::Arg::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 20 ▶use String::ShellQuote 'shell_quote';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 20 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 20 our $VERSION = '0.13';
29  
30 20 with 'IPC::PrettyPipe::Queue::Element';
31  
32 20 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 20 ▶use overload 'fallback' => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57 20 ▶use overload '|' => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=3 SCAR=11.1
58 3 my $swap = pop;
59 3 my $pipe = IPC::PrettyPipe->new;
60 3 ▶ $pipe->add( $_ ) for ( $swap ? reverse( @_ ) : @_ );
Branch: 1 branch
BranchTrueFalseTotal
$swap ? : 0 3 3
61 3 $pipe;
62 20 };
63  
64 # need access to has method which will get removed at the end of the
65 # compilation of this module
66 20 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
67 20 ▶ IPC::PrettyPipe::Arg::Format->shadow_attrs( fmt => sub { 'arg' . shift } );
Subroutine __ANON__: called CC=1 SCAR=0.0
68 }
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99 20 has cmd => (
100 is => 'ro',
101 isa => Str,
102 required => 1,
103 );
104  
105 # delay building args until all attributes have been specified
106 20 has _init_args => (
107 is => 'ro',
108 init_arg => 'args',
109 coerce => AutoArrayRef->coercion,
110 isa => ArrayRef,
111 predicate => 1,
112 clearer => 1,
113 );
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152 has args => (
153 is => 'ro',
154 140 ▶ default => sub { IPC::PrettyPipe::Queue->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
155 20 init_arg => undef,
156 );
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167 has streams => (
168 is => 'ro',
169 140 ▶ default => sub { IPC::PrettyPipe::Queue->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
170 20 init_arg => undef,
171 );
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210 has argfmt => (
211 is => 'ro',
212 lazy => 1,
213 handles => IPC::PrettyPipe::Arg::Format->shadowed_attrs,
214 15 ▶ default => sub { IPC::PrettyPipe::Arg::Format->new_from_attrs( shift ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
215 20 );
216  
217  
218  
219  
220  
221 141 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=5 SCAR=22.5
Pod undocumented
222  
223 141 my $class = shift;
224  
225 my $args
226 = @_ == 1
227 ? (
228 'HASH' eq ref( $_[0] )
229 ? $_[0]
230 : 'ARRAY' eq ref( $_[0] )
231 0 ▶ && @{ $_[0] } == 2 ? { cmd => $_[0][0], args => $_[0][1] }
Branch: 3 branches
BranchTrueFalseTotal
@_ == 1 ? : 0 141 141
'HASH' eq ref $_[0] ? : 0 0 0
'ARRAY' eq ref $_[0] && @{$_[0];} == 2 ? : 0 0 0
Condition0%0/3
"ARRAY" eq ref $_[0] and @{$_[0];} == 2
!ll&&!rl&&r
000
MC/DC0%0/2
"ARRAY" eq ref $_[0] and @{$_[0];} == 2
"ARRAY" eq ref $_[0] @{$_[0];} == 2
Truth table0%0/3
A and B
A= "ARRAY" eq ref $_[0]B= @{$_[0];} == 2
ABresult
0X0
100
111
232 : { cmd => $_[0] } )
233 : {@_};
234  
235 ## no critic (ProhibitAccessOfPrivateData)
236 141 delete @{$args}{ grep { !defined $args->{$_} } keys %$args };
237  
238 141 return $args;
239 }
240  
241  
242 140 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
243  
244  
245 140 my $self = shift;
246  
247 140 ▶ if ( $self->_has_init_args ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_init_args) 13 127 140
248  
249 13 $self->ffadd( @{ $self->_init_args } );
250 13 $self->_clear_init_args;
251 }
252  
253 140 return;
254 }
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265 30 ▶sub quoted_cmd { shell_quote( $_[0]->cmd ) }
Subroutine quoted_cmd: called CC=1 SCAR=0.0
Pod documented
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311  
312  
313  
314  
315  
316  
317 206 ▶sub add {
Subroutine add: called CC=12 SCAR=26.7
Pod documented
318  
319 206 my $self = shift;
320  
321 206 ▶ unshift @_, 'arg' if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 206 206
322  
323 ## no critic (ProhibitAccessOfPrivateData)
324  
325 206 my $argfmt = $self->argfmt->clone;
326  
327 206 my $argfmt_attrs = IPC::PrettyPipe::Arg::Format->shadowed_attrs;
328  
329 my ( $attr ) = validate(
330 \@_,
331 slurpy Dict [
332 arg => Str | Arg | ArrayRef | HashRef,
333 value => Optional [Str],
334 argfmt => Optional [ InstanceOf ['IPC::PrettyPipe::Arg::Format'] ],
335 206 ( map { $_ => Optional [Str] } keys %{$argfmt_attrs} ),
336 ] );
337  
338 205 my $arg = $attr->{arg};
339 205 my $ref = ref $arg;
340  
341 croak( "cannot specify a value if arg is a hash, array, or Arg object\n" )
342 205 ▶ if $ref && exists $attr->{value};
Branch: 1 branch
BranchTrueFalseTotal
if $ref and exists $attr->{"value"} 0 205 205
Condition66%2/3
$ref and exists $attr->{"value"}
!ll&&!rl&&r
132730
MC/DC0%0/2
$ref and exists $attr->{"value"}
$ref exists $attr->{"value"}
Truth table66%2/3
A and B
A= $refB= exists $attr->{"value"}
ABresult
0X0
100
111
343  
344 205 ▶ $argfmt->copy_from( $attr->{argfmt} ) if defined $attr->{argfmt};
Branch: 1 branch
BranchTrueFalseTotal
if defined $attr->{"argfmt"} 204 1 205
345 205 $argfmt->copy_from( IPC::PrettyPipe::Arg::Format->new_from_hash( $attr ) );
346  
347 205 ▶ if ( 'HASH' eq $ref ) {
Branch: 3 branches
BranchTrueFalseTotal
if ('HASH' eq $ref) { } 0 205 205
elsif ('ARRAY' eq $ref) { } 73 132 205
elsif ($arg->$IPC::PrettyPipe::Cmd::_isa('IPC::PrettyPipe::Arg')) { } 0 132 132
348  
349 0 ▶ while ( my ( $name, $value ) = each %$arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$arg) 0 0 0
350  
351 0 $self->args->push(
352 IPC::PrettyPipe::Arg->new(
353 name => $name,
354 value => $value,
355 fmt => $argfmt->clone
356 ) );
357  
358 }
359 }
360  
361 elsif ( 'ARRAY' eq $ref ) {
362  
363 73 ▶ croak( "missing value for argument ", $arg->[-1] )
Branch: 1 branch
BranchTrueFalseTotal
if @$arg % 2 1 72 73
364 if @$arg % 2;
365  
366 72 foreach ( pairs @$arg ) {
367  
368 72 my ( $name, $value ) = @$_;
369  
370 72 $self->args->push(
371 IPC::PrettyPipe::Arg->new(
372 name => $name,
373 value => $value,
374 fmt => $argfmt->clone
375 ) );
376  
377 }
378 }
379  
380 elsif ( $arg->$_isa( 'IPC::PrettyPipe::Arg' ) ) {
381  
382 0 $self->args->push( $arg );
383  
384 }
385  
386 # everything else
387 else {
388  
389 $self->args->push(
390 IPC::PrettyPipe::Arg->new(
391 name => $attr->{arg},
392 132 ▶ exists $attr->{value} ? ( value => $attr->value ) : (),
Branch: 1 branch
BranchTrueFalseTotal
exists $attr->{'value'} ? : 0 132 132
393 fmt => $argfmt->clone
394 ) );
395 }
396  
397 204 return;
398 }
399  
400  
401  
402  
403  
404  
405  
406  
407  
408  
409  
410  
411  
412  
413  
414  
415  
416  
417  
418  
419  
420  
421  
422  
423  
424  
425  
426  
427  
428  
429  
430  
431  
432  
433  
434  
435  
436  
437  
438  
439  
440  
441  
442  
443  
444  
445  
446  
447  
448  
449 137 ▶sub ffadd {
Subroutine ffadd: called CC=6 SCAR=18.2
Pod documented
450  
451 137 my $self = shift;
452 137 my @args = @_;
453  
454 137 my $argfmt = $self->argfmt->clone;
455  
456 137 ▶ for ( my $idx = 0 ; $idx < @args ; $idx++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < @args) 271 137 408
457  
458 271 my $t = $args[$idx];
459  
460 271 ▶ if ( $t->$_isa( 'IPC::PrettyPipe::Arg::Format' ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($t->$IPC::PrettyPipe::Cmd::_isa('IPC::PrettyPipe::Arg::Format')) { } 13 258 271
elsif ($t->$IPC::PrettyPipe::Cmd::_isa('IPC::PrettyPipe::Arg')) { } 0 258 258
elsif (ref($t) =~ /^(ARRAY|HASH)$/) { } 72 186 258
elsif ($t->$IPC::PrettyPipe::Cmd::_isa('IPC::PrettyPipe::Stream')) { } 0 186 186
461  
462 13 $t->copy_into( $argfmt );
463  
464 }
465  
466 elsif ( $t->$_isa( 'IPC::PrettyPipe::Arg' ) ) {
467  
468 0 $self->add( arg => $t );
469  
470  
471 }
472  
473 elsif ( ref( $t ) =~ /^(ARRAY|HASH)$/ ) {
474  
475 72 $self->add( arg => $t, argfmt => $argfmt->clone );
476  
477 }
478  
479 elsif ( $t->$_isa( 'IPC::PrettyPipe::Stream' ) ) {
480  
481 0 $self->stream( $t );
482  
483 }
484  
485 else {
486  
487 186 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=11.0
488  
489 186 my $stream = IPC::PrettyPipe::Stream->new(
490 spec => $t,
491 strict => 0,
492 );
493  
494 54 ▶ if ( $stream->requires_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream->requires_file) 52 2 54
495  
496 52 ▶ croak( "arg[$idx]: stream operator $t requires a file\n" )
Branch: 1 branch
BranchTrueFalseTotal
if ++$idx == @args 0 52 52
497 if ++$idx == @args;
498  
499 52 $stream->file( $args[$idx] );
500 }
501  
502 54 $self->stream( $stream );
503 }
504 132 ▶ catch {
Subroutine __ANON__: called CC=2 SCAR=7.2
505  
506 132 ▶ die $_ if /requires a file/;
Branch: 1 branch
BranchTrueFalseTotal
if /requires a file/ 0 132 132
507  
508 132 $self->add( arg => $t, argfmt => $argfmt->clone );
509 186 };
510  
511 }
512 }
513  
514 137 return;
515 }
516  
517  
518  
519  
520  
521  
522  
523  
524  
525  
526  
527  
528  
529  
530  
531 54 ▶sub stream {
Subroutine stream: called CC=5 SCAR=20.9
Pod documented
532  
533 54 my $self = shift;
534  
535 54 my $spec = shift;
536  
537 54 ▶ if ( $spec->$_isa( 'IPC::PrettyPipe::Stream' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec->$IPC::PrettyPipe::Cmd::_isa('IPC::PrettyPipe::Stream')) { } 54 0 54
elsif (not ref $spec) { } 0 0 0
538  
539 54 ▶ croak( "too many arguments\n" )
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 54 54
540 if @_;
541  
542 54 $self->streams->push( $spec );
543  
544 }
545  
546 elsif ( !ref $spec ) {
547  
548 0 ▶ $self->streams->push(
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
549 IPC::PrettyPipe::Stream->new(
550 spec => $spec,
551 +@_ ? ( file => @_ ) : () ) );
552 }
553  
554 else {
555  
556 0 croak( "illegal stream specification\n" );
557  
558 }
559  
560  
561 54 return;
562 }
563  
564  
565  
566  
567  
568  
569  
570  
571  
572  
573  
574 61 ▶sub valmatch {
Subroutine valmatch: called CC=1 SCAR=0.0
Pod documented
575 61 my $self = shift;
576 61 my $pattern = shift;
577  
578 # find number of matches;
579 61 return sum 0, map { $_->valmatch( $pattern ) } @{ $self->args->elements };
580 }
581  
582  
583  
584  
585  
586  
587  
588  
589  
590  
591  
592  
593  
594  
595  
596  
597  
598  
599  
600  
601  
602  
603  
604  
605  
606  
607  
608 31 ▶sub valsubst {
Subroutine valsubst: called CC=12 SCAR=25.1
Pod documented
609 31 my $self = shift;
610  
611 31 ▶ my @args = ( shift, shift, @_ > 1 ? {@_} : @_ );
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 6 25 31
612  
613  
614 ## no critic (ProhibitAccessOfPrivateData)
615  
616 31 my ( $pattern, $value, $args ) = validate(
617 \@args,
618 RegexpRef,
619 Str,
620 Optional [
621 Dict [
622 lastvalue => Optional [Str],
623 firstvalue => Optional [Str] ]
624 ],
625 );
626  
627 31 my $nmatch = $self->valmatch( $pattern );
628  
629 31 ▶ if ( $nmatch == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmatch == 1) { } 18 13 31
630  
631 18 ▶ $args->{lastvalue} //= $args->{firstvalue} // $value;
Condition66%4/6
$args->{"lastvalue"} //= $args->{"firstvalue"} // $value
l!l&&r!l&&!r
2160
$args->{"firstvalue"} // $value
l!l&&r!l&&!r
1150
MC/DC0%0/3
$args->{"lastvalue"} //= $args->{"firstvalue"} // $value
$args->{"lastvalue"} $args->{"firstvalue"} $value
Truth table0%0/4
A //= B // C
A= $args->{"lastvalue"}B= $args->{"firstvalue"}C= $value
ABCresult
1XX1
01X1
0011
0000
632 18 ▶ $args->{firstvalue} //= $args->{lastvalue};
Condition66%2/3
$args->{"firstvalue"} //= $args->{"lastvalue"}
l!l&&r!l&&!r
1170
MC/DC0%0/2
$args->{"firstvalue"} //= $args->{"lastvalue"}
$args->{"firstvalue"} $args->{"lastvalue"}
Truth table66%2/3
A //= B
A= $args->{"firstvalue"}B= $args->{"lastvalue"}
ABresult
1X1
011
000
633  
634 }
635 else {
636 13 ▶ $args->{lastvalue} ||= $value;
Condition66%2/3
$args->{"lastvalue"} ||= $value
l!l&&r!l&&!r
3100
MC/DC0%0/2
$args->{"lastvalue"} ||= $value
$args->{"lastvalue"} $value
Truth table66%2/3
A ||= B
A= $args->{"lastvalue"}B= $value
ABresult
1X1
011
000
637 13 ▶ $args->{firstvalue} ||= $value;
Condition66%2/3
$args->{"firstvalue"} ||= $value
l!l&&r!l&&!r
3100
MC/DC0%0/2
$args->{"firstvalue"} ||= $value
$args->{"firstvalue"} $value
Truth table66%2/3
A ||= B
A= $args->{"firstvalue"}B= $value
ABresult
1X1
011
000
638 }
639  
640 31 my $match = 0;
641 31 foreach ( @{ $self->args->elements } ) {
642  
643 $match++
644 if $_->valsubst( $pattern,
645 $match == 0 ? $args->{firstvalue}
646 : $match == ( $nmatch - 1 ) ? $args->{lastvalue}
647 55 ▶ : $value );
Branch: 3 branches
BranchTrueFalseTotal
if $_->valsubst($pattern, $match == 0 ? $args->{"firstvalue"} : ($match == $nmatch - 1 ? $args->{"lastvalue"} : $value)) 43 12 55
$match == 0 ? : 34 21 55
$match == $nmatch - 1 ? : 12 9 21
648 }
649  
650 31 return $match;
651 }
652  
653  
654 20 1;
655  
656 #
657 # This file is part of IPC-PrettyPipe
658 #
659 # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
660 #
661 # This is free software, licensed under:
662 #
663 # The GNU General Public License, Version 3, June 2007
664 #
665  
666 __END__
↑ Summary blib/lib/IPC/PrettyPipe.pm »