Util/Interface/Modern.pm

filter: Statementstmt 100% 50 / 50 Branchbran 100% 16 / 16 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 95.1% 79 / 83 CC 10 SCAR 15.9
CC
10
Cov
97%
_remove_opts
9.0
_names_values
2.0
BEGIN
1.0
CRAP 4.9
« Util/Definitions.pm ↑ Summary Util/Exception/Diagnostic.pm »
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.201720';
6 # ABSTRACT: Modern call interface to File::Util
7  
8 23 ▶use File::Util::Interface::Classic qw( _myargs );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use File::Util::Definitions qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 23 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 @ISA $AUTHORITY
13 @EXPORT_OK %EXPORT_TAGS
14 23 );
15  
16 23 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 23 $AUTHORITY = 'cpan:TOMMY';
19 23 @ISA = qw( Exporter File::Util::Interface::Classic );
20 23 @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 23 %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
30  
31  
32 # --------------------------------------------------------
33 # File::Util::Interface::Modern::_names_values()
34 # --------------------------------------------------------
35 173 ▶sub _names_values {
Subroutine _names_values: called CC=2 SCAR=6.9
36  
37 # ignore $_[0] File::Util object reference
38  
39 173 ▶ if ( ref $_[1] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq "HASH") 2 171 173
40  
41 # method was called like $f->method( { name => val } )
42 2 return $_[1]
43 }
44  
45 # ...method called like $f->methd( name => val );
46  
47 171 goto \&File::Util::Interface::Classic::_names_values;
48 }
49  
50  
51 # --------------------------------------------------------
52 # File::Util::Interface::Modern::_remove_opts()
53 # --------------------------------------------------------
54 260 ▶sub _remove_opts {
Subroutine _remove_opts: called CC=9 SCAR=22.0
55  
56 260 shift; # we don't need "$this" here
57  
58 260 my $args = shift @_;
59  
60 260 ▶ return unless ref $args eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "ARRAY" 7 253 260
61  
62 253 my @triage = @$args; @$args = ();
63 253 my $opts = { };
64  
65 253 ▶ while ( @triage ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@triage) 367 253 620
66  
67 367 my $arg = shift @triage;
68  
69 # if an argument is '', 0, or undef, it's obviously not an --option ...
70 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
71  
72 329 ▶ if ( UNIVERSAL::isa( $arg, 'HASH' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, 'HASH')) { } 124 205 329
elsif ($arg =~ /^--/) { } 24 181 205
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 @$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 my ( $opt, $value ) = split /=/, $arg;
89  
90 # bare version
91 24 ▶ $opts->{ $opt } = defined $value ? $value : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 11 13 24
92 # ^^^^^^^ if $value is undef it's a --flag, and value=1
93  
94 # sanitized version, remove leading "--" ...
95 24 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 $clean_name =~ s/[^[:alnum:]]/_/g;
100  
101 24 ▶ $opts->{ $clean_name } = defined $value ? $value : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 11 13 24
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 push @$args, $arg;
108 }
109 }
110  
111 253 return $opts;
112 }
113  
114  
115 # --------------------------------------------------------
116 # File::Util::Interface::Modern::_parse_in()
117 # --------------------------------------------------------
118 164 ▶sub _parse_in {
Subroutine _parse_in: called CC=1 SCAR=0.0
119 164 my ( $this, @in ) = @_;
120  
121 164 my $opts = $this->_remove_opts( \@in ); # always returns a hashref, given a listref
122 164 my $in = $this->_names_values( @in ); # always returns a hashref, given anything
123  
124 # merge two hashrefs
125 164 @$in{ keys %$opts } = values %$opts;
126  
127 164 return $in;
128 }
129  
130  
131 # --------------------------------------------------------
132 # File::Util::Interface::Modern::DESTROY()
133 # --------------------------------------------------------
134 2 ▶sub DESTROY { }
Subroutine DESTROY: called CC=1 SCAR=0.0
135  
136 23 1;
137  
138  
139 __END__
« Util/Definitions.pm ↑ Summary Util/Exception/Diagnostic.pm »