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 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package File::Util::Interface::Modern;
5 22 $File::Util::Interface::Modern::VERSION = '4.161200';
6 # ABSTRACT: Modern call interface to File::Util
7  
8 22 ▶use lib 'lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 22 ▶use File::Util::Interface::Classic qw( _myargs );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use File::Util::Definitions qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 22 use vars qw(
14 @ISA $AUTHORITY
15 @EXPORT_OK %EXPORT_TAGS
16 22 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 22 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 22 $AUTHORITY = 'cpan:TOMMY';
21 22 @ISA = qw( Exporter File::Util::Interface::Classic );
22 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 22 %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 172 ▶ if ( ref $_[1] eq 'HASH' ) {
Subroutine _names_values: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq "HASH") 2 170 172
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 170 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 254 ▶ shift; # we don't need "$this" here
Subroutine _remove_opts: called CC=9 SCAR=22.0
59  
60 254 my $args = shift @_;
61  
62 254 ▶ return unless ref $args eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "ARRAY" 7 247 254
63  
64 247 my @triage = @$args; @$args = ();
65 247 my $opts = { };
66  
67 247 ▶ while ( @triage ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@triage) 363 247 610
68  
69 363 my $arg = shift @triage;
70  
71 # if an argument is '', 0, or undef, it's obviously not an --option ...
72 363 ▶ push @$args, $arg and next unless $arg; # ...so give it back to the @$args
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 38 325 363
Condition60%3/5
$arg || push(@$args, $arg) && (next)
l!l&&r!l&&!r
325380
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 325 ▶ if ( UNIVERSAL::isa( $arg, 'HASH' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, 'HASH')) { } 124 201 325
elsif ($arg =~ /^--/) { } 24 177 201
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 177 push @$args, $arg;
110 }
111 }
112  
113 247 return $opts;
114 }
115  
116  
117 # --------------------------------------------------------
118 # File::Util::Interface::Modern::_parse_in()
119 # --------------------------------------------------------
120 sub _parse_in {
121 163 ▶ my ( $this, @in ) = @_;
Subroutine _parse_in: called CC=1 SCAR=0.0
122  
123 163 my $opts = $this->_remove_opts( \@in ); # always returns a hashref, given a listref
124 163 my $in = $this->_names_values( @in ); # always returns a hashref, given anything
125  
126 # merge two hashrefs
127 163 @$in{ keys %$opts } = values %$opts;
128  
129 163 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 22 1;
139  
140  
141 __END__
« lib/File/Util/Interface/Classic.pm ↑ Summary Build »