blib/lib/Hash/AutoHash/Args.pm

filter: Statementstmt 99.1% 122 / 123 Branchbran 89.4% 34 / 38 Conditioncond 72.2% 13 / 18 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 100% 34 / 34 Podpod n/a totaltotal 92.8% 208 / 224 SCAR 33.0
CC
27
Cov
94%
fix_args
9.0
set_args
6.0
fix_keyword
5.2
CRAP 27.1
↑ Summary Build »
1 package Hash::AutoHash::Args;
2 12 our $VERSION='1.18_01';
3 12 $VERSION=eval $VERSION; # I think this is the accepted idiom..
4  
5 #################################################################################
6 #
7 # Author: Nat Goodman
8 # Created: 09-03-05
9 # $Id:
10 #
11 # Simplifies processing of keyward argument lists.
12 # Replaces Class::AutoClass::Args using Class::AutoClass:Hash and tied hash to
13 # provide cleaner, more powerful interface.
14 # NOT completely compatible with Class::AutoClass::Args.
15 # Use Hash::AutoHash::Args::V0 if compatibility with Class::AutoClass::Args needed
16 #
17 #################################################################################
18 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶use Hash::AutoHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶use base qw(Hash::AutoHash);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 12 our @NORMAL_EXPORT_OK=
24 qw(get_args getall_args set_args fix_args fix_keyword fix_keywords is_keyword is_positional
25 autoargs_get autoargs_set);
26 180 ▶our @RENAME_EXPORT_OK=sub {s/^autohash/autoargs/; $_};
Subroutine __ANON__: called CC=1 SCAR=0.0
27 # our @EXPORT_OK=Hash::AutoHash::Args::helper->EXPORT_OK;
28 # our @SUBCLASS_EXPORT_OK=Hash::AutoHash::Args::helper->SUBCLASS_EXPORT_OK;
29 12 my $helper_class=__PACKAGE__.'::helper';
30 12 our @EXPORT_OK=$helper_class->EXPORT_OK;
31 12 our @SUBCLASS_EXPORT_OK=$helper_class->SUBCLASS_EXPORT_OK;
32  
33 # sub new {
34 # my $class_or_self=@_>0 && shift;
35 # # send to parent if called as object method. will access hash slot via AUTOLOAD
36 # return $class_or_self->SUPER::new(@_) if ref $class_or_self;
37 # # do regular 'new' via helper class if called as class method.
38 # my $helper_class=$class_or_self.'::helper';
39 # $helper_class->_new($class_or_self,@_);
40 # }
41  
42 #################################################################################
43 # helper package exists to avoid polluting Hash::AutoHash::Args namespace with
44 # subs that would mask accessor/mutator AUTOLOADs
45 # functions herein (except _new) are exportable by Hash::AutoHash::Args
46 #################################################################################
47 package Hash::AutoHash::Args::helper;
48 12 our $VERSION=$Hash::AutoHash::Args::VERSION;
49 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 12 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 BEGIN {
53 12 ▶ our @ISA=qw(Hash::AutoHash::helper);
Subroutine BEGIN: called CC=1 SCAR=0.0
54 }
55 12 ▶use Hash::AutoHash qw(autohash_tie);
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 sub _new {
58 76 ▶ my($helper_class,$class,@args)=@_;
Subroutine _new: called CC=1 SCAR=0.0
59 76 my $self=autohash_tie Hash::AutoHash::Args::tie,@args;
60 76 bless $self,$class;
61 }
62  
63 #################################################################################
64 # functions from Class::AutoClass::Args
65 # get_args, set_args are redundant w/ autoargs_get, autoargs_set
66 # getall_args is trivial wrapper for %$args....
67 #################################################################################
68 sub get_args {
69 17 ▶ my($self,@args)=@_;
Subroutine get_args: called CC=4 SCAR=14.2
70 17 ▶ @args=@{$args[0]} if @args==1 && 'ARRAY' eq ref $args[0];
Branch: 1 branch
BranchTrueFalseTotal
if @args == 1 and "ARRAY" eq ref $args[0] 0 17 17
Condition66%2/3
@args == 1 and "ARRAY" eq ref $args[0]
!ll&&!rl&&r
1340
MC/DC0%0/2
@args == 1 and "ARRAY" eq ref $args[0]
@args == 1 "ARRAY" eq ref $args[0]
Truth table66%2/3
A and B
A= @args == 1B= "ARRAY" eq ref $args[0]
ABresult
0X0
100
111
71 17 @args=fix_keyword(@args);
72 17 my @results=map {$self->{$_}} @args;
73 # NG 09-03-12: line below is ancient bug. see POD. scary it wasn't caught sooner
74 # wantarray? @results: $results[0];
75 17 ▶ wantarray? @results: \@results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 12 5 17
76 }
77 7 ▶sub autoargs_get { get_args(@_); } # do it this way so defined at compile-time
Subroutine autoargs_get: called CC=1 SCAR=0.0
78 # *autoargs_get=\&get_args; # NOT this way!
79  
80 sub getall_args {
81 6 ▶ my $self = shift;
Subroutine getall_args: called CC=2 SCAR=6.9
82 6 ▶ wantarray? %$self: {%$self};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 1 6
83 }
84 sub set_args {
85 12 ▶ my $self=shift;
Subroutine set_args: called CC=6 SCAR=17.9
86 12 ▶ if (@_==2 && 'ARRAY' eq ref $_[0] && 'ARRAY' eq ref $_[1]) { # separate arrays form
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and 'ARRAY' eq ref $_[0] and 'ARRAY' eq ref $_[1]) { } 4 8 12
Condition83%5/6
@_ == 2 and "ARRAY" eq ref $_[0] and "ARRAY" eq ref $_[1]
!ll&&!rl&&r
804
@_ == 2 and "ARRAY" eq ref $_[0]
!ll&&!rl&&r
444
MC/DC66%2/3
@_ == 2 and "ARRAY" eq ref $_[0] and "ARRAY" eq ref $_[1]
@_ == 2 "ARRAY" eq ref $_[0] "ARRAY" eq ref $_[1]
Truth table75%3/4
A and B and C
A= @_ == 2B= "ARRAY" eq ref $_[0]C= "ARRAY" eq ref $_[1]
ABCresult
0XX0
10X0
1100
1111
87 4 my($keys,$values)=@_;
88 4 my @keys=fix_keywords(@$keys);
89 4 my @values=@$values;
90 4 ▶ for (my $i=0; $i<@keys; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @keys) 8 4 12
91 8 my($key,$value)=($keys[$i],$values[$i]);
92 8 $self->{$key}=$value;
93 }} else {
94 8 my $args=fix_args(@_);
95 8 ▶ while(my($key,$value)=each %$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$args) 16 8 24
96 16 $self->$key($value);
97 }}
98 12 $self;
99 }
100 5 ▶sub autoargs_set { set_args(@_); } # do it this way so defined at compile-time
Subroutine autoargs_set: called CC=1 SCAR=0.0
101 # *autoargs_set=\&set_args; # NOT this way!
102  
103 sub fix_args {
104 12 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 88 ▶ my(@args)=@_;
Subroutine fix_args: called CC=9 SCAR=22.0
106 88 ▶ @args=@{$args[0]} if @args==1 && 'ARRAY' eq ref $args[0];
Branch: 1 branch
BranchTrueFalseTotal
if @args == 1 and "ARRAY" eq ref $args[0] 3 85 88
Condition100%3/3
@args == 1 and "ARRAY" eq ref $args[0]
!ll&&!rl&&r
7963
MC/DC100%2/2
@args == 1 and "ARRAY" eq ref $args[0]
@args == 1 "ARRAY" eq ref $args[0]
Truth table100%3/3
A and B
A= @args == 1B= "ARRAY" eq ref $args[0]
ABresult
0X0
100
111
107 88 ▶ @args=%{$args[0]} if @args==1 && 'HASH' eq reftype $args[0];
Branch: 1 branch
BranchTrueFalseTotal
if @args == 1 and "HASH" eq builtin::reftype($args[0]) 6 82 88
Condition66%2/3
@args == 1 and "HASH" eq builtin::reftype($args[0])
!ll&&!rl&&r
8206
MC/DC50%1/2
@args == 1 and "HASH" eq builtin::reftype($args[0])
@args == 1 "HASH" eq builtin::reftype($args[0])
Truth table66%2/3
A and B
A= @args == 1B= "HASH" eq builtin::reftype($args[0])
ABresult
0X0
100
111
108 88 ▶ confess("Malformed keyword argument list (odd number of elements): @args") if @args%2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 88 88
109 88 my $args={};
110 88 ▶ while(@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 176 88 264
111 176 my($keyword,$value)=(fix_keyword(shift @args),shift @args);
112 176 ▶ $args->{$keyword}=$value,next unless exists $args->{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$keyword} 133 43 176
113 43 my $old=$args->{$keyword};
114 # NG 09-12-31: breaks if $old is object.
115 # $args->{$keyword}=[$old,$value],next unless ref $old; # grow scalar slot into ARRAY
116 43 ▶ $args->{$keyword}=[$old,$value],next unless 'ARRAY' eq ref $old; # grow scalar slot into ARRAY
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $old 33 10 43
117 10 push(@$old,$value); # else add new value to ARRAY slot
118 }
119 88 $args;
120 }
121 sub fix_keyword {
122 649 ▶ my @keywords=@_; # copies input, so update-in-place doesn't munge it
Subroutine fix_keyword: called CC=5 SCAR=16.4
123 649 for my $keyword (@keywords) {
124 688 ▶ next unless defined $keyword;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keyword 0 688 688
125 688 ▶ $keyword=~s/^-*(.*)$/\L$1/ unless ref $keyword; # updates in place
Branch: 1 branch
BranchTrueFalseTotal
unless ref $keyword 688 0 688
126 }
127 649 ▶ wantarray? @keywords: $keywords[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 204 445 649
128 }
129 8 ▶sub fix_keywords {fix_keyword(@_);}
Subroutine fix_keywords: called CC=1 SCAR=0.0
130 7 ▶sub is_keyword {!(@_%2) && $_[0]=~/^-/;}
Subroutine is_keyword: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
not @_ % 2 and $_[0] =~ /^-/ 4 3 7
131 7 ▶sub is_positional {@_%2 || $_[0]!~/^-/;}
Subroutine is_positional: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 or not $_[0] =~ /^-/ 4 3 7
132  
133 #################################################################################
134 # Tied hash which provides the core capabilities of Hash::AutoHash::Args
135 #################################################################################
136 package Hash::AutoHash::Args::tie;
137 12 our $VERSION=$Hash::AutoHash::Args::VERSION;
138 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
139 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 12 ▶use Tie::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 12 our @ISA=qw(Tie::StdHash);
142 12 *fix_args=\&Hash::AutoHash::Args::helper::fix_args;
143 12 *fix_keyword=\&Hash::AutoHash::Args::helper::fix_keyword;
144  
145 sub TIEHASH {
146 76 ▶ my($class,@args)=@_;
Subroutine TIEHASH: called CC=2 SCAR=7.6
147 76 ▶ $class=(ref $class)||$class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0760
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
148 76 bless Hash::AutoHash::Args::helper::fix_args(@args), $class;
149 }
150 # following code adapted from Tie::StdHash
151 # sub TIEHASH { bless {}, $_[0] }
152 # sub STORE { $_[0]->{fix_keyword($_[1])} = $_[2] }
153 sub STORE {
154 113 ▶ my $self=shift;
Subroutine STORE: called CC=2 SCAR=6.9
155 113 my $keyword=fix_keyword(shift);
156 113 ▶ my $value=@_==1? $_[0]: [@_];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 108 5 113
157 113 $self->{$keyword}=$value;
158 }
159 sub FETCH {
160 291 ▶ my $self=shift;
Subroutine FETCH: called CC=2 SCAR=6.9
161 291 my $keyword=fix_keyword(shift);
162 # non-existent arg should return nothing. not undef! this works when accessing the
163 # object directly or using autoloaded methods from the main class. when accessing
164 # via the tied hash interface, Perl converts the result to undef anyway :(
165 291 ▶ return unless exists $self->{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$keyword} 18 273 291
166 273 return $self->{$keyword};
167 }
168 57 ▶sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
169 143 ▶sub NEXTKEY { each %{$_[0]} }
Subroutine NEXTKEY: called CC=1 SCAR=0.0
170 29 ▶sub EXISTS { exists $_[0]->{fix_keyword($_[1])} }
Subroutine EXISTS: called CC=1 SCAR=0.0
171 10 ▶sub DELETE { delete $_[0]->{fix_keyword($_[1])} }
Subroutine DELETE: called CC=1 SCAR=0.0
172 5 ▶sub CLEAR { %{$_[0]} = () }
Subroutine CLEAR: called CC=1 SCAR=0.0
173 11 ▶sub SCALAR { scalar %{$_[0]} }
Subroutine SCALAR: called CC=1 SCAR=0.0
174  
175 12 1;
176  
177 __END__
↑ Summary Build »