File Coverage

lib/File/Util/Interface/Modern.pm
Criterion Covered Total %
statement 44 44 100.0
branch 14 14 100.0
condition 1 2 50.0
subroutine 10 10 100.0
pod n/a
total 69 70 98.5


line stmt bran cond sub pod time code
1 21     21   129 use strict;
  21         33  
  21         593  
2 21     21   93 use warnings;
  21         33  
  21         1038  
3              
4             package File::Util::Interface::Modern;
5             $File::Util::Interface::Modern::VERSION = '4.201720';
6             # ABSTRACT: Modern call interface to File::Util
7              
8 21     21   7001 use File::Util::Interface::Classic qw( _myargs );
  21         41  
  21         1118  
9 21     21   130 use File::Util::Definitions qw( :all );
  21         38  
  21         3667  
10              
11 21         988 use vars qw(
12             @ISA $AUTHORITY
13             @EXPORT_OK %EXPORT_TAGS
14 21     21   147 );
  21         34  
15              
16 21     21   103 use Exporter;
  21         32  
  21         9460  
17              
18             $AUTHORITY = 'cpan:TOMMY';
19             @ISA = qw( Exporter File::Util::Interface::Classic );
20             @EXPORT_OK = qw(
21             _remove_opts
22             _myargs
23             _names_values
24             _parse_in
25             ); # some of the symbols above come from File::Util::Interface::Classic but
26             # the _remove_opts/_names_values methods are specifically overriden in
27             # this package
28              
29             %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
30              
31              
32             # --------------------------------------------------------
33             # File::Util::Interface::Modern::_names_values()
34             # --------------------------------------------------------
35             sub _names_values {
36              
37             # ignore $_[0] File::Util object reference
38              
39 173 100   173   372 if ( ref $_[1] eq 'HASH' ) {
40              
41             # method was called like $f->method( { name => val } )
42 2         8 return $_[1]
43             }
44              
45             # ...method called like $f->methd( name => val );
46              
47 171         518 goto \&File::Util::Interface::Classic::_names_values;
48             }
49              
50              
51             # --------------------------------------------------------
52             # File::Util::Interface::Modern::_remove_opts()
53             # --------------------------------------------------------
54             sub _remove_opts {
55              
56 260     260   327 shift; # we don't need "$this" here
57              
58 260         311 my $args = shift @_;
59              
60 260 100       855 return unless ref $args eq 'ARRAY';
61              
62 253         580 my @triage = @$args; @$args = ();
  253         326  
63 253         308 my $opts = { };
64              
65 253         462 while ( @triage ) {
66              
67 367         429 my $arg = shift @triage;
68              
69             # if an argument is '', 0, or undef, it's obviously not an --option ...
70 367 100 50     586 push @$args, $arg and next unless $arg; # ...so give it back to the @$args
71              
72 329 100       1206 if ( UNIVERSAL::isa( $arg, 'HASH' ) ) {
    100          
73              
74             # if we got hashref, then we were called with the new & improved syntax:
75             # e.g.- $ftl->method( arg => { opt => foo, opt2 => bar } );
76             #
77             # ...as oppsed to the classic syntax:
78             # e.g.- $ftl->method( arg => value, --opt1=value, --flag )
79             #
80             # the bit of code below makes it possible to support both call syntaxes
81              
82 124         510 @$opts{ keys %$arg } = values %$arg; # crane lower that rover (ahhhhh)
83             # err, Perl flatcopy that hashref
84             }
85             elsif ( $arg =~ /^--/ ) { # got old school "--option" argument?
86              
87             # it's either a bare "--option", or it's an "--option=value" pair
88 24         64 my ( $opt, $value ) = split /=/, $arg;
89              
90             # bare version
91 24 100       84 $opts->{ $opt } = defined $value ? $value : 1;
92             # ^^^^^^^ if $value is undef it's a --flag, and value=1
93              
94             # sanitized version, remove leading "--" ...
95 24         46 my $clean_name = substr $opt, 2;
96              
97             # ...and replace non-alnum chars with "_" so the names can be
98             # referenced as hash keys without superfluous quoting and escaping
99 24         56 $clean_name =~ s/[^[:alnum:]]/_/g;
100              
101 24 100       76 $opts->{ $clean_name } = defined $value ? $value : 1;
102             }
103             else {
104              
105             # but if it's not an "--option" type arg, or a hashref of options,
106             # then give it back to the caller's @$args arrayref
107 181         384 push @$args, $arg;
108             }
109             }
110              
111 253         471 return $opts;
112             }
113              
114              
115             # --------------------------------------------------------
116             # File::Util::Interface::Modern::_parse_in()
117             # --------------------------------------------------------
118             sub _parse_in {
119 164     164   361 my ( $this, @in ) = @_;
120              
121 164         342 my $opts = $this->_remove_opts( \@in ); # always returns a hashref, given a listref
122 164         358 my $in = $this->_names_values( @in ); # always returns a hashref, given anything
123              
124             # merge two hashrefs
125 164         448 @$in{ keys %$opts } = values %$opts;
126              
127 164         449 return $in;
128             }
129              
130              
131             # --------------------------------------------------------
132             # File::Util::Interface::Modern::DESTROY()
133             # --------------------------------------------------------
134       2     sub DESTROY { }
135              
136             1;
137              
138              
139             __END__