File Coverage

blib/lib/IPC/Run3/Shell/CLIWrapper.pm
Criterion Covered Total %
statement 62 63 100.0
branch 22 26 88.4
condition n/a
subroutine 12 13 92.3
pod 1 1 100.0
total 97 103 96.1


line stmt bran cond sub pod time code
1             #!perl
2             package IPC::Run3::Shell::CLIWrapper;
3 2     2   109089 use warnings;
  2         14  
  2         99  
4 2     2   11 use strict;
  2         4  
  2         152  
5              
6             our $VERSION = '0.58';
7             # For AUTHOR, COPYRIGHT, AND LICENSE see the bottom of this file
8              
9             BEGIN {
10 2     2   484 require IPC::Run3::Shell;
11 2         7 *__pp = \&IPC::Run3::Shell::pp; # double underscore to not clutter up the namespace too much
12 2         1161 *__debug = \&IPC::Run3::Shell::debug;
13             }
14 16     16   72 sub __DEBUG { $IPC::Run3::Shell::DEBUG } # don't alias because that doesn't work with `local`
15              
16             our %DEFAULTS = ( opt_char=>'--', val_sep=>undef, under2dash=>1 );
17              
18             my %NEW_ARGS = map {$_=>1} qw/ opt_char val_sep under2dash /;
19              
20             sub new {
21             # The arguments to new() are the same as make_cmd():
22             # option hashrefs can be at the *beginning* of the argument list
23 6     6 1 9693 my ($class, @mcmd) = @_;
24 6         19 my %opt;
25 6         50 %opt = ( %opt, %{shift @mcmd} ) while ref $mcmd[0] eq 'HASH';
  4         49  
26             # now extract the arguments we care about
27 6         16 my %self;
28 6 100       27 for (keys %NEW_ARGS) { $self{$_} = delete $opt{$_} if exists $opt{$_} }
  18         65  
29             # set up ourselves
30 6 100       50 for (keys %DEFAULTS) { $self{$_} = $DEFAULTS{$_} unless exists $self{$_} }
  18         90  
31 6 50       37 __debug "new CLIWrapper, self=",__pp(\%self),", opt=",__pp(\%opt),", cmd=",__pp(\@mcmd) if __DEBUG;
32             # ok, now set up the command
33 6         29 $self{cmd} = IPC::Run3::Shell::make_cmd(\%opt, @mcmd);
34 6         54 return bless \%self, $class;
35             }
36              
37             sub __argconv {
38 10     10   21 my $self = shift;
39 10         24 my @args;
40 10 100       115 my $oc = $self->{opt_char}; $oc = '' unless defined $oc;
  10         37  
41 10         30 my $vs = $self->{val_sep};
42 10         23 my $u2d = $self->{under2dash};
43 10         29 for my $x (@_) {
44 21 100       69 if ( ref $x eq 'ARRAY' ) {
45 10 100       33 if ( @$x%2 ) {
46             # ... work around a Carp issue in really old Perls ...
47             # uncoverable branch true
48             # uncoverable condition true
49 1 50       26 if ( $] lt '5.008' ) {
50 0         0 warn "Odd number of elements in argument list"; # uncoverable statement
51 1         669 } else { warnings::warnif('IPC::Run3::Shell',
52             'Odd number of elements in argument list') }
53             }
54 10         211 for (my $i=0;$i<@$x;$i+=2) {
55 15         29 my ($k,$v) = @{$x}[$i,$i+1];
  15         56  
56 15 100       70 $k =~ s/_/-/g if $u2d;
57 15 100       95 push @args, defined $v
    100          
58             ? ( defined $vs ? $oc.$k.$vs.$v : ($oc.$k, $v) )
59             : $oc.$k;
60             }
61             }
62 11         32 else { push @args, $x }
63             }
64 10         53 return @args;
65             };
66              
67             use overload
68             '&{}' => sub {
69 3     3   15 my $self = shift;
70             return sub {
71 3     3   35 my @args = __argconv($self, @_);
72 3 50       22 __debug "plain command, args=",__pp(\@args) if __DEBUG;
73 3         16 $self->{cmd}->(@args);
74             }
75 2     2   16 };
  2         4  
  2         27  
  3         81  
76              
77             our $AUTOLOAD;
78             sub AUTOLOAD { ## no critic (ProhibitAutoloading)
79 2     2   619 my $meth = $AUTOLOAD;
80 2         22 $meth =~ s/^.*:://;
81             my $sub = sub {
82 7     7   677 my $self = shift;
83 7         15 my $cmd = $meth;
84 7         28 my @args = __argconv($self, @_);
85 7 100       63 $cmd =~ s/_/-/g if $self->{under2dash};
86 7 50       28 __debug "method ",__pp($cmd),", args=",__pp(\@args) if __DEBUG;
87 7         33 $self->{cmd}->($cmd, @args);
88 2         21 };
89 2     2   503 no strict 'refs'; ## no critic (ProhibitNoStrict)
  2         14  
  2         194  
90 2         16 *$AUTOLOAD = $sub;
91 2         12 goto &$AUTOLOAD;
92             }
93       0     sub DESTROY {} # so AUTOLOAD isn't called on destruction
94              
95             1;
96             __END__