lib/File/Util/Interface/Modern.pm

filter: Statementstmt 100% 53 / 53 Branchbran 100% 16 / 16 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 95.4% 83 / 87 SCAR 23.0
CC
10
Cov
97%
_remove_opts
9.0
_names_values
2.0
BEGIN
1.0
CRAP 10.0
« lib/File/Util/Interface/Classic.pm ↑ Summary Build »
1 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package File::Util::Interface::Modern;
5 23 $File::Util::Interface::Modern::VERSION = '4.161950';
6 # ABSTRACT: Modern call interface to File::Util
7  
8 23 ▶use lib 'lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 23 ▶use File::Util::Interface::Classic qw( _myargs );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use File::Util::Definitions qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 23 use vars qw(
14 @ISA $AUTHORITY
15 @EXPORT_OK %EXPORT_TAGS
16 23 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 23 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 23 $AUTHORITY = 'cpan:TOMMY';
21 23 @ISA = qw( Exporter File::Util::Interface::Classic );
22 23 @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 23 %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 173 ▶ if ( ref $_[1] eq 'HASH' ) {
Subroutine _names_values: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq "HASH") 2 171 173
42  
43 # method was called like $f->method( { name => val } )
44 2 return $_[1]
45 }
46  
47 # ...method called like $f->methd( name => val );
48  
49 171 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 260 ▶ shift; # we don't need "$this" here
Subroutine _remove_opts: called CC=9 SCAR=22.0
59  
60 260 my $args = shift @_;
61  
62 260 ▶ return unless ref $args eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "ARRAY" 7 253 260
63  
64 253 my @triage = @$args; @$args = ();
65 253 my $opts = { };
66  
67 253 ▶ while ( @triage ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@triage) 367 253 620
68  
69 367 my $arg = shift @triage;
70  
71 # if an argument is '', 0, or undef, it's obviously not an --option ...
72 367 ▶ push @$args, $arg and next unless $arg; # ...so give it back to the @$args
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 38 329 367
Condition60%3/5
$arg || push(@$args, $arg) && (next)
l!l&&r!l&&!r
329380
push @$args, $arg and next
!ll
038
MC/DC0%0/2
$arg || push(@$args, $arg) && (next)
$arg push @$args, $arg
Truth table66%2/3
A || B
A= $argB= push @$args, $arg
ABresult
1X1
011
000
73  
74 329 ▶ if ( UNIVERSAL::isa( $arg, 'HASH' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, 'HASH')) { } 124 205 329
elsif ($arg =~ /^--/) { } 24 181 205
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 124 @$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 24 my ( $opt, $value ) = split /=/, $arg;
91  
92 # bare version
93 24 ▶ $opts->{ $opt } = defined $value ? $value : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 11 13 24
94 # ^^^^^^^ if $value is undef it's a --flag, and value=1
95  
96 # sanitized version, remove leading "--" ...
97 24 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 24 $clean_name =~ s/[^[:alnum:]]/_/g;
102  
103 24 ▶ $opts->{ $clean_name } = defined $value ? $value : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 11 13 24
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 181 push @$args, $arg;
110 }
111 }
112  
113 253 return $opts;
114 }
115  
116  
117 # --------------------------------------------------------
118 # File::Util::Interface::Modern::_parse_in()
119 # --------------------------------------------------------
120 sub _parse_in {
121 164 ▶ my ( $this, @in ) = @_;
Subroutine _parse_in: called CC=1 SCAR=0.0
122  
123 164 my $opts = $this->_remove_opts( \@in ); # always returns a hashref, given a listref
124 164 my $in = $this->_names_values( @in ); # always returns a hashref, given anything
125  
126 # merge two hashrefs
127 164 @$in{ keys %$opts } = values %$opts;
128  
129 164 return $in;
130 }
131  
132  
133 # --------------------------------------------------------
134 # File::Util::Interface::Modern::DESTROY()
135 # --------------------------------------------------------
136 ▶sub DESTROY { }
Subroutine DESTROY: called CC=1 SCAR=0.0
137  
138 23 1;
139  
140  
141 __END__
« lib/File/Util/Interface/Classic.pm ↑ Summary Build »