blib/lib/Hash/AutoHash.pm

filter: Statementstmt 98.3% 291 / 296 Branchbran 92.8% 91 / 98 Conditioncond 88.8% 16 / 18 MC/DCmcdc 81.8% 9 / 11 Subroutinesub 96.2% 51 / 53 Podpod 100% 1 / 1 totaltotal 96.2% 459 / 477 SCAR 41.3
CC
62
Cov
97%
EXPORT_OK
12.0
autohash_tied
9.0
autohash_new
7.0
CRAP 62.2
↑ Summary Build »
1 package Hash::AutoHash;
2 30 our $VERSION='1.17_01';
3 30 $VERSION=eval $VERSION; # I think this is the accepted idiom..
4  
5 #################################################################################
6 #
7 # Author: Nat Goodman
8 # Created: 09-02-24
9 # $Id:
10 #
11 # Wrapper that provides accessor and mutator methods for hashes (real or tied)
12 # Hash can be externally supplied or this object itself
13 # Tying of hash can be done by application or by this class
14 # Can also wrap object tied to hash
15 # (actually, any object with suitable FETCH and STORE methods)
16 #
17 #################################################################################
18  
19 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 30 ▶use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 30 our @CONSTRUCTORS_EXPORT_OK=
23 qw(autohash_new autohash_hash autohash_tie autohash_wrap autohash_wrapobj autohash_wraptie);
24 30 our @SUBCLASS_EXPORT_OK=
25 qw(autohash_clear autohash_delete autohash_each autohash_exists autohash_keys autohash_values
26 autohash_get autohash_set autohash_count autohash_empty autohash_notempty
27 autohash_alias autohash_tied
28 autohash_destroy autohash_untie);
29 30 our @EXPORT_OK=(@CONSTRUCTORS_EXPORT_OK,@SUBCLASS_EXPORT_OK);
30  
31 # following are used by subclasses
32 30 our @RENAME_EXPORT_OK=();
33 30 our %RENAME_EXPORT_OK=();
34  
35 # our @EXPORT_OK=qw(autohash_new autohash_tie
36 # autohash_wraphash autohash_wraptie autohash_wrapobject
37 # autohash2hash autohash2object
38 # autohash_clear autohash_delete autohash_exists autohash_keys autohash_values
39 # autohash_count autohash_empty autohash_notempty
40 # autohash_destroy autohash_untie
41 # autohash_get autohash_set);
42  
43 sub import {
44 142 ▶ my $class_or_self=shift;
Subroutine import: called CC=2 SCAR=6.9
45 142 ▶ if (ref $class_or_self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class_or_self) 11 131 142
46 # called as object method. access hash slot via AUTOLOAD
47 11 $AUTOLOAD='import';
48 11 return $class_or_self->AUTOLOAD(@_);
49 }
50 # called as class method. do regular 'import'
51 131 my $caller=caller;
52 131 my $helper_class=$class_or_self.'::helper';
53 131 $helper_class->_import($class_or_self,$caller,@_);
54 }
55 sub new {
56 79 ▶ my $class_or_self=shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
57 79 ▶ if (ref $class_or_self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class_or_self) 12 67 79
58 # called as object method. access hash slot via AUTOLOAD
59 12 $AUTOLOAD='new';
60 12 return $class_or_self->AUTOLOAD(@_);
61 }
62 # called as class method. do regular 'new' via helper class
63 67 my $helper_class=$class_or_self.'::helper';
64 67 $helper_class->_new($class_or_self,@_);
65 }
66 # NG 12-09-02: no longer possible to use method notation for keys with same names as methods
67 # inherited from UNIVERSAL. 'Cuz as of Perl 5.9.3, calling UNIVERSAL methods as
68 # functions is deprecated and developers encouraged to use method form instead.
69 # sub can {
70 # my $class_or_self=shift;
71 # if (ref $class_or_self) {
72 # # called as object method. access hash slot via AUTOLOAD
73 # $AUTOLOAD='can';
74 # return $class_or_self->AUTOLOAD(@_);
75 # }
76 # # called as class method. do regular 'can' via base class
77 # return $class_or_self->SUPER::can(@_);
78 # }
79 # sub isa {
80 # my $class_or_self=shift;
81 # if (ref $class_or_self) {
82 # # called as object method. access hash slot via AUTOLOAD
83 # $AUTOLOAD='isa';
84 # return $class_or_self->AUTOLOAD(@_);
85 # }
86 # # called as function or class method. do regular 'isa' via base class
87 # return $class_or_self->SUPER::isa(@_);
88 # }
89 # sub DOES { # in perl 5.10, UNIVERSAL provides this
90 # my $class_or_self=shift;
91 # if (ref $class_or_self) {
92 # # called as object method. access hash slot via AUTOLOAD
93 # $AUTOLOAD='DOES';
94 # return $class_or_self->AUTOLOAD(@_);
95 # }
96 # # called as function or class method. do regular 'DOES' via base class
97 # # illegal and will die in perls < 5.10
98 # return $class_or_self->SUPER::DOES(@_);
99 # }
100 # sub VERSION {
101 # my $class_or_self=shift;
102 # if (ref $class_or_self) {
103 # # called as object method. access hash slot via AUTOLOAD
104 # $AUTOLOAD='VERSION';
105 # return $class_or_self->AUTOLOAD(@_);
106 # }
107 # # called as function or class method. do regular 'VERSION' via base class
108 # return $class_or_self->SUPER::VERSION(@_);
109 # }
110 sub DESTROY {
111 # CAUTION: do NOT shift - need $_[0] intact
112 772 ▶ if (ref($_[0])) {
Subroutine DESTROY: called CC=2 SCAR=7.9
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 772 0 772
113 # called as object method. inish up in helper class where namespace more complete
114 772 my $helper_class=ref($_[0]).'::helper';
115 772 my $helper_function=__PACKAGE__.'::helper::_destroy';
116 772 return $helper_class->$helper_function(@_);
117 }
118 # called as class method. pass to base class. not sure this ever happens...
119 0 my $class_or_self=shift;
120 0 return $class_or_self->SUPER::DESTROY(@_);
121 }
122  
123 # my $self=$_[0]; # CAUTION: do NOT shift - need $_[0] intact
124 # return unless ref $self; # shouldn't happen, but...
125 # if (@_==1) { # called as destructor or accessor
126 # # perlobj says that $_[0] is read-only when DESTROY called as destructor
127 # local $@=undef;
128 # eval { $_[0]=undef };
129 # return if $@; # eval failed, so it's destructor.
130 # $_[0]=$self; # not destructor. restore $_[0]
131 # }
132 # # not destructor. access hash slot via AUTOLOAD
133 # shift; # now shift $self out of @_
134 # $AUTOLOAD='DESTROY';
135 # $self->AUTOLOAD(@_)
136  
137 sub AUTOLOAD {
138 1785 ▶ my $self=shift;
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
139 1785 $AUTOLOAD=~s/^.*:://; # strip class qualification
140 # return if $AUTOLOAD eq 'DESTROY'; # the books say you should do this
141 1785 my $key=$AUTOLOAD;
142 1785 ▶ defined $key or $key='AUTOLOAD';
Branch: 1 branch
BranchTrueFalseTotal
defined $key or $key = "AUTOLOAD" 11 1774 1785
143 1785 $AUTOLOAD=undef; # reset for next time
144 # finish up in helper class where namespace more complete
145 1785 my $helper_function=__PACKAGE__.'::helper::_autoload';
146 1785 $self->$helper_function($key,@_);
147 }
148  
149 #################################################################################
150 # helper package exists to avoid polluting Hash::AutoHash namespace with
151 # subs that would mask accessor/mutator AUTOLOADs
152 # functions herein (except _new, _autoload) are exportable by Hash::AutoHash
153 #################################################################################
154 package Hash::AutoHash::helper;
155 30 our $VERSION=$Hash::AutoHash::VERSION;
156 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
157 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
158 30 ▶use Scalar::Util qw(blessed readonly reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
159 30 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
160 30 ▶use Tie::ToObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
161 30 ▶use vars qw(%SELF2HASH %SELF2OBJECT %SELF2EACH %CLASS2ANCESTORS %EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
162  
163 sub _import {
164 131 ▶ my($helper_class,$class,$caller,@want)=@_;
Subroutine _import: called CC=4 SCAR=13.9
165 131 $helper_class->EXPORT_OK; # initializes %EXPORT_OK if necessary
166 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
167 131 my %caller2export=%{$class.'::EXPORT_OK'};
168 # my @export_ok=keys %caller2export;
169 131 for my $want (@want) {
170 231 ▶ confess("\"$want\" not exported by $class module") unless exists $caller2export{$want};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $caller2export{$want} 7 224 231
171 224 ▶ confess("\"$want\" not defined by $class module") unless defined $caller2export{$want};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $caller2export{$want} 2 222 224
172 222 my $caller_sym=$caller.'::'.$want;
173 222 my $export_sym=$caller2export{$want};
174 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
175 222 *{$caller_sym}=\&{$export_sym};
176 }
177 }
178
179 # front-end to autohash_new constructor function, which in turn is front-end
180 # to other constructor functions.
181 sub _new {
182 57 ▶ my($helper_class,$class)=splice @_,0,2;
Subroutine _new: called CC=1 SCAR=0.0
183 57 my $self=autohash_new(@_);
184 57 bless $self,$class; # re-bless in case called via subclass
185 }
186  
187 sub _destroy {
188 772 ▶ my $helper_class=shift;
Subroutine _destroy: called CC=3 SCAR=11.0
189 # $_[0] is now original object.
190 # CAUTION: do NOT shift further - need $_[0] intact
191 # perlobj says that $_[0] is read-only when DESTROY called as destructor
192 772 ▶ return if @_==1 && readonly($_[0]); # destructor. nothing to do.
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and &readonly($_[0]) 761 11 772
Condition100%3/3
@_ == 1 and &readonly($_[0])
!ll&&!rl&&r
65761
MC/DC100%2/2
@_ == 1 and &readonly($_[0])
@_ == 1 &readonly($_[0])
Truth table100%3/3
A and B
A= @_ == 1B= &readonly($_[0])
ABresult
0X0
100
111
193 # not destructor. access hash slot via AUTOLOAD
194 11 my $self=shift;
195 11 my $helper_function=__PACKAGE__.'::_autoload';
196 11 $self->$helper_function('DESTROY',@_)
197 }
198  
199 sub _autoload {
200 1796 ▶ my($self,$key)=splice(@_,0,2);
Subroutine _autoload: called CC=4 SCAR=13.9
201 1796 ▶ if (my $object=tied %$self) { # tied hash, so invoke FETCH/STORE methods
Branch: 1 branch
BranchTrueFalseTotal
if (my $object = tied %$self) { } 1184 612 1796
202 1184 ▶ return @_==0? $object->FETCH($key): $object->STORE($key,@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 ? : 782 402 1184
203 } else { # regular hash
204 612 ▶ return @_==0? ($self->{$key}): ($self->{$key}=$_[0]);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 ? : 402 210 612
205 }
206 }
207  
208 # use vars qw(%CLASS2ANCESTORS);
209 sub _ancestors {
210 42 ▶ my($class,$visited)=@_;
Subroutine _ancestors: called CC=6 SCAR=17.9
211 42 my $ancestors=$CLASS2ANCESTORS{$class};
212 42 ▶ defined $visited or $visited={};
Branch: 1 branch
BranchTrueFalseTotal
defined $visited or $visited = {} 36 6 42
213 42 ▶ unless (defined($ancestors) || $visited->{$class}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ancestors or $visited->{$class}) 36 6 42
Condition66%2/3
defined $ancestors or $visited->{$class}
l!l&&r!l&&!r
6036
MC/DC50%1/2
defined $ancestors or $visited->{$class}
defined $ancestors $visited->{$class}
Truth table66%2/3
A or B
A= defined $ancestorsB= $visited->{$class}
ABresult
1X1
011
000
214 # first call, so compute it
215 36 $ancestors=[$class]; # include self
216 36 $visited->{$class}++;
217 36 my @isa;
218 30 ▶ {no strict "refs"; @isa = @{ $class . '::ISA' };}
Subroutine BEGIN: called CC=1 SCAR=0.0
219 36 for my $super (@isa) {
220 6 push(@$ancestors,_ancestors($super,$visited));
221 }
222 36 @$ancestors=uniq(@$ancestors);
223 36 $CLASS2ANCESTORS{$class}=$ancestors
224 }
225 42 ▶ wantarray? @$ancestors: $ancestors;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 36 42
226 }
227  
228 sub EXPORT_OK {
229 149 ▶ my $helper_class=shift;
Subroutine EXPORT_OK: called CC=12 SCAR=24.9
230 149 my($class)=$helper_class=~/^(.*)::helper$/;
231 # for Hash::AutoHash::helper, @EXPORT_OK is given and function computes %EXPORT_OK
232 149 ▶ if ($helper_class eq __PACKAGE__) { # NOTE: change this if you copy-and-paste into subclass
Branch: 1 branch
BranchTrueFalseTotal
if ($helper_class eq __PACKAGE__) 113 36 149
233 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
234 113 my $export_ok_list=\@{$class.'::EXPORT_OK'};
235 113 my $export_ok_hash=\%{$class.'::EXPORT_OK'};
236 113 ▶ unless(%$export_ok_hash) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$export_ok_hash) 30 83 113
237 30 my $ancestors=$helper_class->_ancestors;
238 30 for my $func (@$export_ok_list) {
239 630 $export_ok_hash->{$func}=_export_sym($func,$class,$ancestors);
240 }}
241 113 return @$export_ok_list;
242 }
243 # for subclasses, @EXPORT_OK and %EXPORT_OK must both be computed
244 36 my($export_ok_list,$export_ok_hash,@isa,@normal_export_ok,@rename_export_ok,%rename_export_ok);
245 {
246 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
247 36 $export_ok_list=\@{$class.'::EXPORT_OK'};
248 # NG 12-11-29: 'defined @array' deprecated in 5.16 or so
249 # return @$export_ok_list if defined @$export_ok_list;
250 36 ▶ return @$export_ok_list if @$export_ok_list;
Branch: 1 branch
BranchTrueFalseTotal
if @$export_ok_list 30 6 36
251 6 $export_ok_hash=\%{$class.'::EXPORT_OK'};
252 6 @isa=@{$helper_class.'::ISA'};
253 6 @normal_export_ok=@{$class.'::NORMAL_EXPORT_OK'};
254 6 @rename_export_ok=@{$class.'::RENAME_EXPORT_OK'};
255 6 %rename_export_ok=%{$class.'::RENAME_EXPORT_OK'};
256 };
257 6 map {$_->EXPORT_OK} @isa; # mqke sure EXPORT_OK setup in ancestors
258 6 my $ancestors=$helper_class->_ancestors;
259  
260 6 for my $func (@normal_export_ok) {
261 14 $export_ok_hash->{$func}=_export_sym($func,$class,$ancestors);
262 }
263 6 ▶ while(my($caller_func,$our_func)=each %rename_export_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %rename_export_ok) 6 6 12
264 6 $export_ok_hash->{$caller_func}=_export_sym($our_func,$class,$ancestors);
265 }
266 6 ▶ if (@rename_export_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rename_export_ok) 6 0 6
267 6 my($sub,@our_funcs)=@rename_export_ok;
268 6 my %skip;
269 6 ▶ unless (@our_funcs) { # rename list empty, so use default
Branch: 1 branch
BranchTrueFalseTotal
unless (@our_funcs) 2 4 6
270 # start with all subclass-exportable functions from base classes
271 @our_funcs=uniq
272 2 ▶ map {UNIVERSAL::can($_,'SUBCLASS_EXPORT_OK')? $_->SUBCLASS_EXPORT_OK: ()} @isa;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can($_, 'SUBCLASS_EXPORT_OK') ? : 2 0 2
273 # %skip contains ones dealt with in @NORMAL_EXPORT_OK or %RENAME_EXPORT_OK
274 2 @skip{@normal_export_ok}=(1) x @normal_export_ok;
275 2 @skip{keys %rename_export_ok}=(1) x keys %rename_export_ok;
276 # @skip{values %rename_export_ok}=(1) x values %rename_export_ok;
277 }
278 6 for my $our_func (@our_funcs) {
279 40 local $_=$our_func;
280 40 my $caller_func=&$sub(); # sub operates on $_
281 40 ▶ next if $skip{$caller_func};
Branch: 1 branch
BranchTrueFalseTotal
if $skip{$caller_func} 0 40 40
282 40 $export_ok_hash->{$caller_func}=_export_sym($our_func,$class,$ancestors);
283 }
284 }
285 6 @$export_ok_list=keys %$export_ok_hash;
286 }
287 sub SUBCLASS_EXPORT_OK {
288 8 ▶ my $helper_class=shift;
Subroutine SUBCLASS_EXPORT_OK: called CC=3 SCAR=11.0
289 8 my($class)=$helper_class=~/^(.*)::helper$/;
290 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
291 # for Hash::AutoHash::helper, @SUBCLASS_EXPORT_OK is given
292 8 ▶ if ($helper_class eq __PACKAGE__) { # NOTE: change this if you copy-and-paste into subclass
Branch: 1 branch
BranchTrueFalseTotal
if ($helper_class eq __PACKAGE__) 2 6 8
293 2 return @{$class.'::SUBCLASS_EXPORT_OK'};
294 }
295 # for subclasses, @SUBCLASS_EXPORT_OK must be computed
296 6 my $subclass_export_ok=\@{$class.'::SUBCLASS_EXPORT_OK'};
297 # NG 12-11-29: 'defined @array' deprecated in 5.16 or so
298 # return @$subclass_export_ok if defined @$subclass_export_ok;
299 6 ▶ return @$subclass_export_ok if @$subclass_export_ok;
Branch: 1 branch
BranchTrueFalseTotal
if @$subclass_export_ok 0 6 6
300 6 return @$subclass_export_ok=$helper_class->EXPORT_OK;
301 }
302  
303 sub _export_sym {
304 690 ▶ my($func,$class,$ancestors)=@_;
Subroutine _export_sym: called CC=4 SCAR=13.9
305 690 for my $export_class (@$ancestors) { # @$ancestors includes self
306 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
307 738 my $export_sym=$export_class.'::'.$func;
308 738 ▶ return $export_sym if defined *{$export_sym}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{$export_sym;}{"CODE"} 676 62 738
309 # see if ancestor renames it
310 62 my($class)=$export_class=~/^(.*)::helper$/;
311 62 my $export_sym=${$class.'::EXPORT_OK'}{$func};
312 62 ▶ return $export_sym if defined $export_sym;
Branch: 1 branch
BranchTrueFalseTotal
if defined $export_sym 10 52 62
313 }
314 4 undef;
315 }
316  
317 #################################################################################
318 # constructor functions. recommended over 'new'
319 #################################################################################
320 # make real autohash
321 # any extra params are key=>value pairs stored in object
322 sub autohash_hash {
323 160 ▶ my(@hash)=@_;
Subroutine autohash_hash: called CC=1 SCAR=0.0
324 # store params in self. can do in one step since no special semantics to worry about
325 160 my $self=bless {@hash},'Hash::AutoHash';
326 160 $self;
327 }
328 # tie autohash
329 # any extra params passed to tie
330 sub autohash_tie (*@) {
331 115 ▶ my($hash_class,@hash_params)=@_;
Subroutine autohash_tie: called CC=1 SCAR=0.0
332 115 my $self=bless {},'Hash::AutoHash';
333 115 tie %$self,$hash_class,@hash_params;
334 115 $self;
335 }
336 # wrap pre-existing hash.
337 # any extra params are key=>value pairs passed to hash
338 sub autohash_wrap (\%@) {
339 240 ▶ my($hash,@hash)=@_;
Subroutine autohash_wrap: called CC=3 SCAR=11.0
340 # pass params to hash in loop in case it's tied hash with special semantics
341 240 ▶ while (@hash>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hash > 1) 498 240 738
342 498 my($key,$value)=splice @hash,0,2; # shift 1st two elements
343 498 $hash->{$key}=$value;
344 }
345 240 my $self=bless {},'Hash::AutoHash';
346 # if $hash is real, tie to 'alias', so autohash will alias hash
347 240 ▶ if (my $object=tied(%$hash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $object = tied %$hash) { } 122 118 240
348 122 tie %$self,'Tie::ToObject',$object;
349 } else {
350 118 tie %$self,'Hash::AutoHash::alias',$hash;
351 }
352 240 $self;
353 }
354 # wrap pre-existing tied object. (ie, object returned by tie),
355 # any extra params are key=>value pairs passed to object's STORE method
356 sub autohash_wrapobj {
357 128 ▶ my($object,@hash)=@_;
Subroutine autohash_wrapobj: called CC=2 SCAR=6.9
358 # pass params to hash in loop in case it's tied hash with special semantics
359 128 ▶ while (@hash>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hash > 1) 259 128 387
360 259 my($key,$value)=splice @hash,0,2; # shift 1st two elements
361 259 $object->STORE($key,$value);
362 }
363 128 my $self=bless {},'Hash::AutoHash';
364 128 tie %$self,'Tie::ToObject',$object;
365 128 $self;
366 }
367 # tie and wrap hash in one step. any extra params passed to tie
368 # kinda silly, but oh well...
369 sub autohash_wraptie (\%*@) {
370 128 ▶ my($hash,$hash_class,@hash_params)=@_;
Subroutine autohash_wraptie: called CC=1 SCAR=0.0
371 128 my $object=tie %$hash,$hash_class,@hash_params;
372 128 my $self=bless {},'Hash::AutoHash';
373 128 tie %$self,'Tie::ToObject',$object;
374 128 $self;
375 }
376 # autohash_new - CAUTION: must come after other constructors because of prototypes
377 # front-end to other constructor functions
378 # cases:
379 # 1) 0 params - autohash_hash
380 # 2) >0 params - 1st param unblessed ARRAY - autohash_tie or autohash_wraptie
381 # 0th element scalar - autohash_tie
382 # 0th element HASH - autohash_wraptie
383 # 3) >0 params - 1st param unblessed HASH - autohash_wrap
384 # 4) >0 params - 1st param blessed HASH apparently not tied hash - autohash_wrap
385 # 5) >0 params - 1st param blessed and looks like tied hash object - autohash_wrapobj
386 # 6) other - autohash_hash
387 sub autohash_new {
388 348 ▶ if (@_) {
Subroutine autohash_new: called CC=7 SCAR=19.5
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 302 46 348
389 302 ▶ if ('ARRAY' eq ref $_[0]) { # autohash_tie or autohash_wraptie
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" eq ref $_[0]) 94 208 302
390 94 my $autohash;
391 94 my $params=shift;
392 94 my $class_or_hash=shift @$params;
393 94 ▶ unless (ref $class_or_hash) { # it's a class. so tie it
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class_or_hash) { } 57 37 94
394 37 $autohash=autohash_tie($class_or_hash,@$params);
395 } else { # it's a hash. next param is class
396 57 my $hash=$class_or_hash;
397 57 my $class=shift @$params;
398 57 $autohash=autohash_wraptie(%$hash,$class,@$params);
399 }
400 94 return autohash_set($autohash,@_);
401 }
402 208 ▶ if ('HASH' eq reftype($_[0]) && !_looks_wrappable($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" eq builtin::reftype($_[0]) and not _looks_wrappable($_[0])) 102 106 208
Condition100%3/3
"HASH" eq builtin::reftype($_[0]) and not _looks_wrappable($_[0])
!ll&&!rl&&r
5452102
MC/DC100%2/2
"HASH" eq builtin::reftype($_[0]) and not _looks_wrappable($_[0])
"HASH" eq builtin::reftype($_[0]) not _looks_wrappable($_[0])
Truth table100%3/3
A and not(B)
A= "HASH" eq builtin::reftype($_[0])B= not _looks_wrappable($_[0])
ABresult
0X0
100
111
403 102 my $hash=shift;
404 102 return autohash_wrap(%$hash,@_);
405 }
406 106 ▶ if (_looks_wrappable($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_looks_wrappable($_[0])) 54 52 106
407 54 return autohash_wrapobj(@_);
408 }}
409 # none of the above, so must be real
410 98 autohash_hash(@_);
411 }
412  
413 # try to decide if object tied to hash. very approximate...
414 # say yes if blessed and has TIEHASH method
415 260 ▶sub _looks_wrappable {blessed($_[0]) && UNIVERSAL::can($_[0],'TIEHASH');}
Subroutine _looks_wrappable: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and UNIVERSAL::can($_[0], "TIEHASH") 106 154 260
416
417 #################################################################################
418 # following functions provide standard hash operations on Hash::AutoHash
419 # objects. they delegate to wrapped goodie
420 #################################################################################
421 15 ▶sub autohash_clear {%{$_[0]}=()}
Subroutine autohash_clear: called CC=1 SCAR=0.0
422 sub autohash_delete {
423 185 ▶ my $self=shift;
Subroutine autohash_delete: called CC=1 SCAR=0.0
424 185 delete @$self{@_};
425 }
426 490 ▶sub autohash_each {each %{$_[0]}}
Subroutine autohash_each: called CC=1 SCAR=0.0
427 327 ▶sub autohash_exists {exists $_[0]->{$_[1]}}
Subroutine autohash_exists: called CC=1 SCAR=0.0
428 98 ▶sub autohash_keys {keys %{$_[0]}}
Subroutine autohash_keys: called CC=1 SCAR=0.0
429 95 ▶sub autohash_values {values %{$_[0]}}
Subroutine autohash_values: called CC=1 SCAR=0.0
430  
431 #################################################################################
432 # convenience methods easily be built on top of keys
433 #################################################################################
434 26 ▶sub autohash_count {scalar(keys %{$_[0]}) || 0}
Subroutine autohash_count: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %{$_[0];} or 0 12 14 26
435 27 ▶sub autohash_empty {scalar(%{$_[0]})? undef: 1}
Subroutine autohash_empty: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
scalar %{$_[0];} ? : 14 13 27
436 27 ▶sub autohash_notempty {scalar(%{$_[0]})? 1: undef}
Subroutine autohash_notempty: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
scalar %{$_[0];} ? : 14 13 27
437  
438 ################################################################################
439 # alias - connect autohash to hash - can be used to do the opposite of wrap
440 ################################################################################
441 sub autohash_alias (\$\%@) {
442 49 ▶ my($autohash_ref,$hash,@hash)=@_;
Subroutine autohash_alias: called CC=2 SCAR=6.9
443 49 ▶ if (!defined $$autohash_ref) { # no autohash, so create alias from hash to autohash
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $$autohash_ref) { } 8 41 49
444 8 return $$autohash_ref=autohash_wrap(%$hash,@hash);
445 } else { # create alias from autohash to hash
446 41 my $autohash=$$autohash_ref;
447 41 autohash_set($autohash,@hash);
448 41 tie %$hash,'Hash::AutoHash::alias',$autohash;
449 }
450 }
451 ################################################################################
452 # functional access to tied object. works on aliased hash, also
453 ################################################################################
454 # sub autohash_options (\[$%]) {
455 # my($ref)=@_;
456 # my $autohash;
457 # if ('REF' eq ref $ref) { # it's autohash (we hope :)
458 # $autohash=$$ref; # dereference to get autohash
459 # my $object=tied %$autohash;
460 # return undef unless $object; # real hash
461 # return undef if 'Hash::AutoHash::alias' eq ref $object; # aliased to real
462 # return $object; # tied or aliased to tied
463 # } elsif ('HASH' eq ref $ref) { # HASH may be tied to 'real object or 'alias'
464 # my $object=tied %$ref;
465 # return undef unless $object;
466 # return $object unless 'Hash::AutoHash::alias' eq ref $object;
467 # # hash aliased to autohash. recurse to get underlying tied object
468 # $autohash=$object->[0]; # extract autohash from alias
469 # return &autohash_options(\$autohash); # use old-style call to turn off prototyping
470 # }
471 # undef;
472 # }
473 # sub autohash_options (\[$%]) {
474 # my($ref)=@_;
475 # my($autohash,$hash);
476 # $autohash=$$ref if 'REF' eq ref $ref; # it's autohash (we hope :)
477 # $hash=$ref if 'HASH' eq ref $ref;
478 # if ($hash) { # do hash case first. sometimes falls into autohash case
479 # my $object=tied %$ref;
480 # return undef unless $object;
481 # return $object unless 'Hash::AutoHash::alias' eq ref $object;
482 # # hash aliased to autohash. extract autohash from alias and fall into authohash case
483 # $autohash=$object->[0];
484 # }
485 # if ($autohash) {
486 # my $object=tied %$autohash;
487 # return undef unless $object; # real hash
488 # return undef if 'Hash::AutoHash::alias' eq ref $object; # aliased to real
489 # return $object; # tied or aliased to tied
490 # }
491 # undef;
492 # }
493 # sub autohash_option (\[$%]@) {
494 # my($ref,$option,@params)=@_;
495 # my $object=&autohash_options($ref); # use old-style call to turn off prototyping
496 # return undef unless $object;
497 # $object->$option(@params);
498 # }
499 sub autohash_tied (\[$%]@) {
500 376 ▶ my $ref=shift;
Subroutine autohash_tied: called CC=9 SCAR=22.0
501 376 my($autohash,$hash,$tied);
502 376 ▶ $autohash=$$ref if 'REF' eq ref $ref; # it's autohash (we hope :)
Branch: 1 branch
BranchTrueFalseTotal
if "REF" eq ref $ref 205 171 376
503 376 ▶ $hash=$ref if 'HASH' eq ref $ref;
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq ref $ref 171 205 376
504 376 ▶ if ($hash) { # do hash case first. sometimes falls into autohash case
Branch: 1 branch
BranchTrueFalseTotal
if ($hash) 171 205 376
505 171 $tied=tied %$ref;
506 # hash aliased to autohash. extract autohash from alias and fall into authohash case
507 171 ▶ $autohash=$tied->[0] if 'Hash::AutoHash::alias' eq ref $tied;
Branch: 1 branch
BranchTrueFalseTotal
if "Hash::AutoHash::alias" eq ref $tied 91 80 171
508 }
509 376 ▶ if ($autohash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autohash) 296 80 376
510 296 $tied=tied %$autohash;
511 296 ▶ $tied=undef if 'Hash::AutoHash::alias' eq ref $tied; # aliased to real
Branch: 1 branch
BranchTrueFalseTotal
if "Hash::AutoHash::alias" eq ref $tied 57 239 296
512 }
513 376 ▶ return $tied unless @_ && $tied;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ and $tied 208 168 376
Condition100%3/3
@_ and $tied
!ll&&!rl&&r
104104168
MC/DC100%2/2
@_ and $tied
@_ $tied
Truth table100%3/3
A and B
A= @_B= $tied
ABresult
0X0
100
111
514 # have tied object and there are more params. this means 'run method on tied object'
515 168 my($method,@params)=@_;
516 168 $tied->$method(@params);
517 }
518  
519 #################################################################################
520 # get and set offer extended functionality for users of this interface.
521 # 'set' is the useful one. 'get' provided for symmetry
522 #################################################################################
523 # get values for one or more keys.
524 sub autohash_get {
525 137 ▶ my $self=shift;
Subroutine autohash_get: called CC=1 SCAR=0.0
526 137 @$self{@_};
527 }
528 # set one or more key=>value pairs in hash
529 sub autohash_set {
530 183 ▶ my $self=shift;
Subroutine autohash_set: called CC=6 SCAR=17.9
531 183 ▶ 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]) { } 19 164 183
Condition83%5/6
@_ == 2 and "ARRAY" eq ref $_[0] and "ARRAY" eq ref $_[1]
!ll&&!rl&&r
164019
@_ == 2 and "ARRAY" eq ref $_[0]
!ll&&!rl&&r
1501419
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
532 19 my($keys,$values)=@_;
533 19 ▶ for (my $i=0; $i<@$keys; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$keys) 23 19 42
534 23 my($key,$value)=($keys->[$i],$values->[$i]);
535 23 $self->{$key}=$value;
536 }} else { # key=>value form
537 164 ▶ while (@_>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 255 164 419
538 255 my($key,$value)=splice @_,0,2; # shift 1st two elements
539 255 $self->{$key}=$value;
540 }}
541 183 $self;
542 }
543  
544 #################################################################################
545 # destroy and untie rarely used but needed for full tied hash functionality.
546 # destroy nop. untie calls tied object's untie method
547 #################################################################################
548 ▶sub autohash_destroy {}
Subroutine autohash_destroy: not called CC=1 SCAR=0.0
549 sub autohash_untie {
550 0 ▶ my $object=tied(%{$_[0]});
Subroutine autohash_untie: not called CC=2 SCAR=17.9
551 0 ▶ $object->UNTIE() if $object;
Branch: 1 branch
BranchTrueFalseTotal
if $object 0 0 0
552 }
553  
554 # #################################################################################
555 # # this package used to 'dup' autohash to externally supplied real hash
556 # # amazing that nothing in CPAN does this! I found several 'alias' packages but
557 # # none could connect new variable to old one without changing the type of old
558 # #################################################################################
559 # package Hash::AutoHash::dup;
560 # use strict;
561 # use Tie::Hash;
562 # our @ISA=qw(Tie::ExtraHash);
563  
564 # sub TIEHASH {
565 # my($class,$existing_hash)=@_;
566 # bless [$existing_hash],$class;
567 # }
568 #################################################################################
569 # this package used to 'alias' hash to externally supplied hash
570 # amazing that nothing in CPAN does this! I found several 'alias' packages but
571 # none could connect new variable to old one without changing the type of old
572 #################################################################################
573 package Hash::AutoHash::alias;
574 30 our $VERSION=$Hash::AutoHash::VERSION;
575 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
576 30 ▶use Tie::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
577 30 our @ISA=qw(Tie::ExtraHash);
578  
579 sub TIEHASH {
580 159 ▶ my($class,$existing_autohash)=@_;
Subroutine TIEHASH: called CC=1 SCAR=0.0
581 159 bless [$existing_autohash],$class;
582 }
583 30 1;
584  
585 __END__
↑ Summary Build »