blib/lib/Hash/WithDefaults.pm

filter: Statementstmt 9.9% 37 / 373 Branchbran 0.0% 0 / 206 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 23.4% 11 / 47 Podpod 66.6% 2 / 3 totaltotal 7.4% 50 / 668 CC 120 SCAR 39.9
CC
120
Cov
6%
NEXTKEY
110.0
NEXTKEY
110.0
NEXTKEY
110.0
CRAP 54.2
↑ Summary
1 package Hash::WithDefaults;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 require Tie::Hash;
5 1 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 @ISA = qw(Tie::StdHash);
7 1 $VERSION = '0.04';
8  
9 sub DATA () {0}
10 sub DEFAULTS () {1}
11 sub ACTDEFAULT () {2}
12 sub SEEN () {3}
13  
14 6 ▶sub makeTIEHASH {
Subroutine makeTIEHASH: called CC=1 SCAR=6.7
Pod undocumented
15 6 my ($class, $set) = @_;
16 6 $class = 'Hash::WithDefaults::' . $class;
17 0 ▶ eval "sub ${class}::TIEHASH {" . <<'*END*' . "\t\t\t" . $set . <<'*END*' . "\t\t\t" . $set . <<'*END*';
Subroutine TIEHASH: not called CC=8 SCAR=42.6
Subroutine TIEHASH: not called CC=8 SCAR=42.6
Subroutine TIEHASH: not called CC=8 SCAR=42.6
Subroutine TIEHASH: not called CC=8 SCAR=42.6
Subroutine TIEHASH: not called CC=8 SCAR=42.6
Subroutine TIEHASH: not called CC=8 SCAR=42.6
Branch: 36 branches
BranchTrueFalseTotal
unless (@_) 0 0 0
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 0 0
if (() = each %$input) 0 0 0
if (ref $_[0] eq 'ARRAY') { } 0 0 0
elsif (@_ % 2 == 0) { } 0 0 0
if ($i <= $arr->$#*) 0 0 0
unless (@_) 0 0 0
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 0 0
if (() = each %$input) 0 0 0
if (ref $_[0] eq 'ARRAY') { } 0 0 0
elsif (@_ % 2 == 0) { } 0 0 0
if ($i <= $arr->$#*) 0 0 0
unless (@_) 0 0 0
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 0 0
if (() = each %$input) 0 0 0
if (ref $_[0] eq 'ARRAY') { } 0 0 0
elsif (@_ % 2 == 0) { } 0 0 0
if ($i <= $arr->$#*) 0 0 0
unless (@_) 0 0 0
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 0 0
if (() = each %$input) 0 0 0
if (ref $_[0] eq 'ARRAY') { } 0 0 0
elsif (@_ % 2 == 0) { } 0 0 0
if ($i <= $arr->$#*) 0 0 0
unless (@_) 0 0 0
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 0 0
if (() = each %$input) 0 0 0
if (ref $_[0] eq 'ARRAY') { } 0 0 0
elsif (@_ % 2 == 0) { } 0 0 0
if ($i <= $arr->$#*) 0 0 0
unless (@_) 0 0 0
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 0 0
if (() = each %$input) 0 0 0
if (ref $_[0] eq 'ARRAY') { } 0 0 0
elsif (@_ % 2 == 0) { } 0 0 0
if ($i <= $arr->$#*) 0 0 0
Condition0%0/18
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
18 my $class = shift();
19 my $data = {};
20  
21 if (! @_) {
22 # no parameters
23 return bless [ $data, []], $class;
24 }
25  
26 if (@_ == 1 and ref $_[0] eq 'HASH') {
27 my $input=$_[0];
28 my ($key,$value);
29 while (($key,$value) = each(%$input)) {
30 *END*
31  
32 }
33 } else {
34 my ($i, $arr) = (0);
35 if (ref $_[0] eq 'ARRAY') {
36 $arr = $_[0];
37 } elsif (@_ % 2 == 0) {
38 $arr = \@_;
39 } else {
40 croak "Ussage: tie %hashname, $class, \%hash\n or tie %hashname, $class, \\\%hash\n or tie %hashname, $class, \\\@array\n";
41 }
42 while ($i <= $#$arr) {
43 my ($key,$value)=($arr->[$i],$arr->[$i+1]); $i+=2;
44 *END*
45  
46 }
47 }
48  
49 bless [$data, []];
50 }
51 *END*
52 }
53  
54 1 makeTIEHASH 'sensitive', '$data->{$key} = $value;';
55 1 makeTIEHASH 'tolower', '$data->{lc $key} = $value;';
56 1 makeTIEHASH 'toupper', '$data->{uc $key} = $value;';
57 1 makeTIEHASH 'lower', '$data->{lc $key} = $value;';
58 1 makeTIEHASH 'upper', '$data->{uc $key} = $value;';
59 1 makeTIEHASH 'preserve', '$data->{lc $key} = [$key,$value];';
60  
61 0 ▶sub TIEHASH {
Subroutine TIEHASH: not called CC=6 SCAR=34.5
62 0 shift(); # shift out class name
63 0 ▶ if (@_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 0 0 0
64 # no parameters
65 0 unshift @_, 'Hash::WithDefaults::preserve';
66 0 goto &Hash::WithDefaults::preserve::TIEHASH;
67 }
68  
69 0 ▶ if (!ref $_[0] and (ref $_[1] eq 'HASH' or @_ % 2 == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] and ref $_[1] eq 'HASH' || @_ % 2 == 1) { } 0 0 0
Condition0%0/6
not ref $_[0] and ref $_[1] eq "HASH" || @_ % 2 == 1
!ll&&!rl&&r
000
ref $_[1] eq "HASH" or @_ % 2 == 1
l!l&&r!l&&!r
000
MC/DC0%0/3
not ref $_[0] and ref $_[1] eq "HASH" || @_ % 2 == 1
not ref $_[0] ref $_[1] eq "HASH" @_ % 2 == 1
Truth table0%0/4
not(A) and B or C
A= not ref $_[0]B= ref $_[1] eq "HASH"C= @_ % 2 == 1
ABCresult
0XX0
1000
11X1
1011
70 # type plus either \%hash or %hash
71 0 my $type = lc(splice(@_, 0, 1));
72 0 ▶ if ($type =~ /^(?:sensitive|preserve|lower|upper|tolower|toupper)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^(?:sensitive|preserve|lower|upper|tolower|toupper)$/) { } 0 0 0
73 0 unshift @_, 'Hash::WithDefaults::' . $type;
74 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
75 0 goto &{"Hash::WithDefaults::".$type."::TIEHASH"};
76 } else {
77 0 croak "Unknown type '$type'! Use one of:\n\tsensitive, preserve, lower, upper, tolower, toupper";
78 }
79 } else {
80 0 unshift @_, 'Hash::WithDefaults::preserve';
81 0 goto &Hash::WithDefaults::preserve::TIEHASH;
82 }
83 }
84  
85 0 ▶sub AddDefault {
Subroutine AddDefault: not called CC=1 SCAR=6.9
Pod documented
86 0 push @{$_[0]->[DEFAULTS]}, $_[1];
87 0 return 1;
88 }
89  
90 0 ▶sub GetDefaults {
Subroutine GetDefaults: not called CC=1 SCAR=6.9
Pod documented
91 0 my $self = shift;
92 0 return $self->[DEFAULTS];
93 }
94  
95 0 ▶sub CLEAR {
Subroutine CLEAR: not called CC=1 SCAR=6.9
96 0 my $self = shift;
97 0 undef $self->[SEEN];
98 0 undef $self->[ACTDEFAULT];
99 0 $self
100 }
101  
102  
103 #############################
104  
105 package Hash::WithDefaults::preserve;
106 1 ▶BEGIN {*Hash::WithDefaults::Preserve:: = \%Hash::WithDefaults::preserve::;}
Subroutine BEGIN: called CC=1 SCAR=0.0
107 1 @Hash::WithDefaults::preserve::ISA = qw(Hash::WithDefaults);
108 sub DATA () {0}
109 sub DEFAULTS () {1}
110 sub ACTDEFAULT () {2}
111 sub SEEN () {3}
112  
113 sub TIEHASH {
114 splice( @_, 1, 0, 'preserve');
115 goto &Hash::WithDefaults::TIEHASH;
116 }
117  
118 0 ▶sub STORE {
Subroutine STORE: not called CC=1 SCAR=6.9
119 0 $_[0]->[DATA]->{lc $_[1]} = [$_[1],$_[2]];
120 }
121  
122 0 ▶sub FETCH {
Subroutine FETCH: not called CC=4 SCAR=30.0
123 0 my $lc_key = lc $_[1];
124 0 return ${$_[0]->[DATA]->{$lc_key}}[1]
125 0 ▶ if exists $_[0]->[DATA]->{$lc_key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0][0]{$lc_key} 0 0 0
126  
127 0 foreach my $default (@{$_[0]->[DEFAULTS]}) {
128 return $default->{$_[1]}
129 0 ▶ if exists($default->{$_[1]});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$default{$_[1]} 0 0 0
130 }
131  
132 0 return;
133 }
134  
135 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=4 SCAR=30.0
136 return 1
137 0 ▶ if exists $_[0]->[DATA]->{lc $_[1]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0][0]{lc $_[1]} 0 0 0
138  
139 0 foreach my $default (@{$_[0]->[DEFAULTS]}) {
140 return 1
141 0 ▶ if exists($default->{$_[1]});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$default{$_[1]} 0 0 0
142 }
143  
144 0 return;
145 }
146  
147 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
148 0 delete $_[0]->[DATA]->{lc $_[1]}
149 }
150  
151 0 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: not called CC=4 SCAR=30.0
152 0 my $self = $_[0];
153 0 undef $self->[ACTDEFAULT];
154 0 $self->[SEEN] = {};
155 0 keys %{$self->[DATA]};
156 0 my ($key,$val);
157 0 ▶ if (($key,$val) = each %{$self->[DATA]}) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %{$$self[0];}) { } 0 0 0
elsif (@{$self->[1];}) { } 0 0 0
158 0 $self->[SEEN]->{$key}=1;
159 0 ▶ return wantarray ? ($val->[0], $val->[1]) : $val->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
160 0 } elsif (@{$self->[DEFAULTS]}) {
161 0 return $self->NEXTKEY();
162 } else {
163 0 return;
164 }
165 }
166  
167 0 ▶sub NEXTKEY {
Subroutine NEXTKEY: not called CC=10 SCAR=47.0
168 0 my $self = $_[0];
169 0 my $seen = $self->[SEEN];
170 0 my ($key,$val);
171 0 ▶ if (!defined $self->[ACTDEFAULT]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->[2]) 0 0 0
172 # processing the base hash
173 0 ▶ if (($key,$val) = each %{$self->[DATA]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self[0];}) { } 0 0 0
174 0 $seen->{$key}=1;
175 0 ▶ return wantarray ? ($val->[0], $val->[1]) : $val->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
176 } else {
177 # base hash done
178 0 ▶ if (! @{$self->[DEFAULTS]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$self->[1];}) { } 0 0 0
179 # no defaults
180 0 return;
181 } else {
182 0 $self->[ACTDEFAULT]=0;
183 # reset the first default
184 0 keys %{$self->[DEFAULTS]->[0]};
185 }
186 }
187 }
188  
189 0 ▶ while (exists $self->[DEFAULTS]->[$self->[ACTDEFAULT]]) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->[1][$self->[2]]) 0 0 0
190 0 ▶ while (($key,$val) = each %{$self->[DEFAULTS]->[$self->[ACTDEFAULT]]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->[1][$self->[2]];}) 0 0 0
191 return wantarray ? ($key, $val) : $key
192 0 ▶ unless $seen->{lc $key}++;
Branch: 2 branches
BranchTrueFalseTotal
unless $$seen{lc $key}++ 0 0 0
wantarray ? : 0 0 0
193 }
194  
195 0 $self->[ACTDEFAULT]++;
196 0 ▶ keys %{$self->[DEFAULTS]->[$self->[ACTDEFAULT]]}
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->[1][$self->[2]] 0 0 0
197 if exists $self->[DEFAULTS]->[$self->[ACTDEFAULT]];
198 }
199  
200 # all hashes done. Cleanup
201 0 undef $self->[SEEN];
202 0 undef $self->[ACTDEFAULT];
203 0 return;
204 }
205  
206 #############################
207  
208 package Hash::WithDefaults::lower;
209 1 ▶BEGIN {*Hash::WithDefaults::Lower:: = \%Hash::WithDefaults::lower::;}
Subroutine BEGIN: called CC=1 SCAR=0.0
210 1 @Hash::WithDefaults::lower::ISA = qw(Hash::WithDefaults::preserve);
211 sub DATA () {0}
212 sub DEFAULTS () {1}
213 sub ACTDEFAULT () {2}
214 sub SEEN () {3}
215  
216 sub TIEHASH {
217 splice( @_, 1, 0, 'lower');
218 goto &Hash::WithDefaults::TIEHASH;
219 }
220  
221 0 ▶sub STORE {
Subroutine STORE: not called CC=1 SCAR=6.9
222 0 $_[0]->[DATA]->{lc $_[1]} = $_[2];
223 }
224  
225 0 ▶sub FETCH {
Subroutine FETCH: not called CC=4 SCAR=30.0
226 return $_[0]->[DATA]->{lc $_[1]}
227 0 ▶ if exists $_[0]->[DATA]->{lc $_[1]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0][0]{lc $_[1]} 0 0 0
228  
229 0 foreach my $default (@{$_[0]->[DEFAULTS]}) {
230 return $default->{$_[1]}
231 0 ▶ if exists($default->{$_[1]});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$default{$_[1]} 0 0 0
232 }
233  
234 0 return;
235 }
236  
237 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=4 SCAR=30.0
238 return 1
239 0 ▶ if exists $_[0]->[DATA]->{lc $_[1]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0][0]{lc $_[1]} 0 0 0
240  
241 0 foreach my $default (@{$_[0]->[DEFAULTS]}) {
242 return 1
243 0 ▶ if exists($default->{$_[1]});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$default{$_[1]} 0 0 0
244 }
245  
246 0 return;
247 }
248  
249 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
250 0 delete $_[0]->[DATA]->{lc $_[1]}
251 }
252  
253 0 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: not called CC=4 SCAR=30.0
254 0 my $self = $_[0];
255 0 $self->[ACTDEFAULT] = -1;
256 0 $self->[SEEN] = {};
257 0 keys %{$self->[DATA]};
258 0 my ($key,$val);
259 0 ▶ if (($key,$val) = each %{$self->[DATA]}) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %{$$self[0];}) { } 0 0 0
elsif (@{$self->[1];}) { } 0 0 0
260 0 $self->[SEEN]->{$key}=1;
261 0 ▶ return wantarray ? ($key, $val) : $key;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
262 0 } elsif (@{$self->[DEFAULTS]}) {
263 0 return $self->NEXTKEY();
264 } else {
265 0 return;
266 }
267 }
268  
269 0 ▶sub NEXTKEY {
Subroutine NEXTKEY: not called CC=10 SCAR=47.0
270 0 my $self = $_[0];
271 0 my $seen = $self->[SEEN];
272 0 my $defaults = $self->[DEFAULTS];
273 0 my ($key,$val);
274 0 ▶ if ($self->[ACTDEFAULT] == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[2] == -1) 0 0 0
275 # processing the base hash
276 0 ▶ if (($key,$val) = each %{$self->[DATA]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self[0];}) { } 0 0 0
277 0 $seen->{$key}=1;
278 0 ▶ return wantarray ? ($key, $val) : $key;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
279 } else {
280 # base hash done
281 0 $self->[ACTDEFAULT]=0;
282 0 ▶ if (! @$defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$defaults) { } 0 0 0
283 # no defaults
284 0 return;
285 } else {
286 # reset the first default
287 0 keys %{$defaults->[0]};
288 }
289 }
290 }
291 0 ▶ while (exists $defaults->[$self->[ACTDEFAULT]]) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$defaults[$self->[2]]) 0 0 0
292 0 ▶ while (($key,$val) = each %{$defaults->[$self->[ACTDEFAULT]]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$defaults[$self->[2]];}) 0 0 0
293 return wantarray ? ($key, $val) : $key
294 0 ▶ unless $seen->{lc $key}++;
Branch: 2 branches
BranchTrueFalseTotal
unless $$seen{lc $key}++ 0 0 0
wantarray ? : 0 0 0
295 }
296  
297 0 $self->[ACTDEFAULT]++;
298 0 ▶ keys %{$defaults->[$self->[ACTDEFAULT]]}
Branch: 1 branch
BranchTrueFalseTotal
if exists $$defaults[$self->[2]] 0 0 0
299 if exists $defaults->[$self->[ACTDEFAULT]];
300 }
301  
302 # all hashes done. Cleanup
303 0 undef $self->[SEEN];
304 0 undef $self->[ACTDEFAULT];
305 0 return;
306 }
307  
308 #############################
309  
310 package Hash::WithDefaults::upper;
311 1 ▶BEGIN {*Hash::WithDefaults::Upper:: = \%Hash::WithDefaults::upper::;}
Subroutine BEGIN: called CC=1 SCAR=0.0
312 1 @Hash::WithDefaults::upper::ISA = qw(Hash::WithDefaults::preserve);
313 sub DATA () {0}
314 sub DEFAULTS () {1}
315 sub ACTDEFAULT () {2}
316 sub SEEN () {3}
317  
318 sub TIEHASH {
319 splice( @_, 1, 0, 'upper');
320 goto &Hash::WithDefaults::TIEHASH;
321 }
322  
323 0 ▶sub STORE {
Subroutine STORE: not called CC=1 SCAR=6.9
324 0 $_[0]->[DATA]->{uc $_[1]} = $_[2];
325 }
326  
327 0 ▶sub FETCH {
Subroutine FETCH: not called CC=4 SCAR=30.0
328 return $_[0]->[DATA]->{uc $_[1]}
329 0 ▶ if exists $_[0]->[DATA]->{uc $_[1]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0][0]{uc $_[1]} 0 0 0
330  
331 0 foreach my $default (@{$_[0]->[DEFAULTS]}) {
332 return $default->{$_[1]}
333 0 ▶ if exists($default->{$_[1]});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$default{$_[1]} 0 0 0
334 }
335  
336 0 return;
337 }
338  
339 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=4 SCAR=30.0
340 return 1
341 0 ▶ if exists $_[0]->[DATA]->{uc $_[1]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0][0]{uc $_[1]} 0 0 0
342  
343 0 foreach my $default (@{$_[0]->[DEFAULTS]}) {
344 return 1
345 0 ▶ if exists($default->{$_[1]});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$default{$_[1]} 0 0 0
346 }
347  
348 0 return;
349 }
350  
351 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
352 0 delete $_[0]->[DATA]->{uc $_[1]}
353 }
354  
355 0 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: not called CC=4 SCAR=30.0
356 0 my $self = $_[0];
357 0 $self->[ACTDEFAULT] = -1;
358 0 $self->[SEEN] = {};
359 0 keys %{$self->[DATA]};
360 0 my ($key,$val);
361 0 ▶ if (($key,$val) = each %{$self->[DATA]}) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %{$$self[0];}) { } 0 0 0
elsif (@{$self->[1];}) { } 0 0 0
362 0 $self->[SEEN]->{$key}=1;
363 0 ▶ return wantarray ? ($key, $val) : $key;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
364 0 } elsif (@{$self->[DEFAULTS]}) {
365 0 return $self->NEXTKEY();
366 } else {
367 0 return;
368 }
369 }
370  
371 0 ▶sub NEXTKEY {
Subroutine NEXTKEY: not called CC=10 SCAR=47.0
372 0 my $self = $_[0];
373 0 my $seen = $self->[SEEN];
374 0 my $defaults = $self->[DEFAULTS];
375 0 my ($key,$val);
376 0 ▶ if ($self->[ACTDEFAULT] == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[2] == -1) 0 0 0
377 # processing the base hash
378 0 ▶ if (($key,$val) = each %{$self->[DATA]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self[0];}) { } 0 0 0
379 0 $seen->{$key}=1;
380 0 ▶ return wantarray ? ($key, $val) : $key;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
381 } else {
382 # base hash done
383 0 $self->[ACTDEFAULT]=0;
384 0 ▶ if (! @$defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$defaults) { } 0 0 0
385 # no defaults
386 0 return;
387 } else {
388 # reset the first default
389 0 keys %{$defaults->[0]};
390 }
391 }
392 }
393 0 ▶ while (exists $defaults->[$self->[ACTDEFAULT]]) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$defaults[$self->[2]]) 0 0 0
394 0 ▶ while (($key,$val) = each %{$defaults->[$self->[ACTDEFAULT]]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$defaults[$self->[2]];}) 0 0 0
395 return wantarray ? ($key, $val) : $key
396 0 ▶ unless $seen->{uc $key}++;
Branch: 2 branches
BranchTrueFalseTotal
unless $$seen{uc $key}++ 0 0 0
wantarray ? : 0 0 0
397 }
398  
399 0 $self->[ACTDEFAULT]++;
400 0 ▶ keys %{$defaults->[$self->[ACTDEFAULT]]}
Branch: 1 branch
BranchTrueFalseTotal
if exists $$defaults[$self->[2]] 0 0 0
401 if exists $defaults->[$self->[ACTDEFAULT]];
402 }
403  
404 # all hashes done. Cleanup
405 0 undef $self->[SEEN];
406 0 undef $self->[ACTDEFAULT];
407 0 return;
408 }
409  
410  
411 #############################
412  
413 package Hash::WithDefaults::sensitive;
414 1 ▶BEGIN {*Hash::WithDefaults::Sensitive:: = \%Hash::WithDefaults::sensitive::;}
Subroutine BEGIN: called CC=1 SCAR=0.0
415 1 @Hash::WithDefaults::sensitive::ISA = qw(Hash::WithDefaults);
416 sub DATA () {0}
417 sub DEFAULTS () {1}
418 sub ACTDEFAULT () {2}
419 sub SEEN () {3}
420  
421 sub TIEHASH {
422 splice( @_, 1, 0, 'sensitive');
423 goto &Hash::WithDefaults::TIEHASH;
424 }
425  
426 0 ▶sub STORE {
Subroutine STORE: not called CC=1 SCAR=6.9
427 0 $_[0]->[DATA]->{$_[1]} = $_[2];
428 }
429  
430 0 ▶sub FETCH {
Subroutine FETCH: not called CC=4 SCAR=30.0
431 return $_[0]->[DATA]->{$_[1]}
432 0 ▶ if exists $_[0]->[DATA]->{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0][0]{$_[1]} 0 0 0
433  
434 0 foreach my $default (@{$_[0]->[DEFAULTS]}) {
435 return $default->{$_[1]}
436 0 ▶ if exists($default->{$_[1]});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$default{$_[1]} 0 0 0
437 }
438  
439 0 return;
440 }
441  
442 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=4 SCAR=30.0
443 return 1
444 0 ▶ if exists $_[0]->[DATA]->{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0][0]{$_[1]} 0 0 0
445  
446 0 foreach my $default (@{$_[0]->[DEFAULTS]}) {
447 return 1
448 0 ▶ if exists($default->{$_[1]});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$default{$_[1]} 0 0 0
449 }
450  
451 0 return;
452 }
453  
454 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
455 0 delete $_[0]->[DATA]->{$_[1]}
456 }
457  
458 0 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: not called CC=4 SCAR=30.0
459 0 my $self = $_[0];
460 0 $self->[ACTDEFAULT] = -1;
461 0 $self->[SEEN] = {};
462 0 keys %{$self->[DATA]};
463 0 my ($key,$val);
464 0 ▶ if (($key,$val) = each %{$self->[DATA]}) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %{$$self[0];}) { } 0 0 0
elsif (@{$self->[1];}) { } 0 0 0
465 0 $self->[SEEN]->{$key}=1;
466 0 ▶ return wantarray ? ($key, $val) : $key;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
467 0 } elsif (@{$self->[DEFAULTS]}) {
468 0 return $self->NEXTKEY();
469 } else {
470 0 return;
471 }
472 }
473  
474 0 ▶sub NEXTKEY {
Subroutine NEXTKEY: not called CC=10 SCAR=47.0
475 0 my $self = $_[0];
476 0 my $seen = $self->[SEEN];
477 0 my $defaults = $self->[DEFAULTS];
478 0 my ($key,$val);
479 0 ▶ if ($self->[ACTDEFAULT] == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[2] == -1) 0 0 0
480 # processing the base hash
481 0 ▶ if (($key,$val) = each %{$self->[DATA]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self[0];}) { } 0 0 0
482 0 $seen->{$key}=1;
483 0 ▶ return wantarray ? ($key, $val) : $key;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
484 } else {
485 # base hash done
486 0 $self->[ACTDEFAULT]=0;
487 0 ▶ if (! @$defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$defaults) { } 0 0 0
488 # no defaults
489 0 return;
490 } else {
491 # reset the first default
492 0 keys %{$defaults->[0]};
493 }
494 }
495 }
496 0 ▶ while (exists $defaults->[$self->[ACTDEFAULT]]) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$defaults[$self->[2]]) 0 0 0
497 0 ▶ while (($key,$val) = each %{$defaults->[$self->[ACTDEFAULT]]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$defaults[$self->[2]];}) 0 0 0
498 return wantarray ? ($key, $val) : $key
499 0 ▶ unless $seen->{$key}++;
Branch: 2 branches
BranchTrueFalseTotal
unless $seen->{$key}++ 0 0 0
wantarray ? : 0 0 0
500 }
501  
502 0 $self->[ACTDEFAULT]++;
503 0 ▶ keys %{$defaults->[$self->[ACTDEFAULT]]}
Branch: 1 branch
BranchTrueFalseTotal
if exists $$defaults[$self->[2]] 0 0 0
504 if exists $defaults->[$self->[ACTDEFAULT]];
505 }
506  
507 # all hashes done. Cleanup
508 0 undef $self->[SEEN];
509 0 undef $self->[ACTDEFAULT];
510 0 return;
511 }
512  
513  
514 #############################
515  
516 package Hash::WithDefaults::toupper;
517 1 ▶BEGIN {*Hash::WithDefaults::Toupper:: = \%Hash::WithDefaults::toupper::;}
Subroutine BEGIN: called CC=1 SCAR=0.0
518 1 @Hash::WithDefaults::toupper::ISA = qw(Hash::WithDefaults::sensitive);
519 sub DATA () {0}
520 sub DEFAULTS () {1}
521 sub ACTDEFAULT () {2}
522 sub SEEN () {3}
523  
524 sub TIEHASH {
525 splice( @_, 1, 0, 'toupper');
526 goto &Hash::WithDefaults::TIEHASH;
527 }
528  
529 0 ▶sub STORE {
Subroutine STORE: not called CC=1 SCAR=6.9
530 0 $_[0]->[DATA]->{uc $_[1]} = $_[2];
531 }
532  
533 #############################
534  
535 package Hash::WithDefaults::tolower;
536 1 ▶BEGIN {*Hash::WithDefaults::Tolower:: = \%Hash::WithDefaults::tolower::;}
Subroutine BEGIN: called CC=1 SCAR=0.0
537 1 @Hash::WithDefaults::tolower::ISA = qw(Hash::WithDefaults::sensitive);
538 sub DATA () {0}
539 sub DEFAULTS () {1}
540 sub ACTDEFAULT () {2}
541 sub SEEN () {3}
542  
543 sub TIEHASH {
544 splice( @_, 1, 0, 'tolower');
545 goto &Hash::WithDefaults::TIEHASH;
546 }
547  
548 0 ▶sub STORE {
Subroutine STORE: not called CC=1 SCAR=6.9
549 0 $_[0]->[DATA]->{lc $_[1]} = $_[2];
550 }
551  
552 1 1;
553  
554 __END__
↑ Summary