blib/lib/File/Util/Interface/Modern.pm untested

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