Layout.pm

filter: Statementstmt 93.0% 266 / 286 Branchbran 66.4% 97 / 146 Conditioncond 65.2% 60 / 92 MC/DCmcdc 42.8% 24 / 56 Subroutinesub 92.1% 47 / 51 Podpod 95.4% 21 / 22 totaltotal 78.8% 515 / 653 CC 114 SCAR 22.7
CC
114
Cov
81%
_load
20.6
lookup_leaf_path
20.0
_coerce_levels_param
16.9
CRAP 9.7
↑ Summary Layout/Level.pm »
1 package Hash::Layout;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: hashes with predefined layouts, composite keys and default values
6  
7 5 our $VERSION = '1.02';
8  
9 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Types::Standard qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Hash::Merge::Simple 'merge';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Clone;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use Hash::Layout::Level;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 has 'levels', is => 'ro', isa => ArrayRef[
18 InstanceOf['Hash::Layout::Level']
19 ], required => 1, coerce => \&_coerce_levels_param;
20  
21 263 ▶sub num_levels { scalar(@{(shift)->levels}) }
Subroutine num_levels: called CC=1 SCAR=0.0
Pod documented
22  
23 12 ▶has 'default_value', is => 'ro', default => sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
24 12 ▶has 'default_key', is => 'ro', isa => Str, default => sub { '*' };
Subroutine __ANON__: called CC=1 SCAR=0.0
25 11 ▶has 'allow_deep_values', is => 'ro', isa => Bool, default => sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
26 12 ▶has 'deep_delimiter', is => 'ro', isa => Str, default => sub { '.' };
Subroutine __ANON__: called CC=1 SCAR=0.0
27 11 ▶has 'no_fill', is => 'ro', isa => Bool, default => sub { 0 };
Subroutine __ANON__: called CC=1 SCAR=0.0
28 11 ▶has 'no_pad', is => 'ro', isa => Bool, default => sub { 0 };
Subroutine __ANON__: called CC=1 SCAR=0.0
29  
30 has 'lookup_mode', is => 'rw', isa => Enum[qw(get fallback merge)],
31 12 ▶ default => sub { 'merge' };
Subroutine __ANON__: called CC=1 SCAR=0.0
32  
33 12 ▶has '_Hash', is => 'ro', isa => HashRef, default => sub {{}}, init_arg => undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
34 12 ▶has '_all_level_keys', is => 'ro', isa => HashRef, default => sub {{}}, init_arg => undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
35  
36 # List of bitmasks representing every key path which includes
37 # a default_key, with each bit representing the level and '1' toggled on
38 # where the key is the default
39 12 ▶has '_def_key_bitmasks', is => 'ro', isa => HashRef, default => sub {{}}, init_arg => undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
40  
41 242 ▶sub Data { Clone::clone( (shift)->_Hash ) }
Subroutine Data: called CC=1 SCAR=0.0
Pod documented
42  
43 sub level_keys {
44 9 ▶ my ($self, $index) = @_;
Subroutine level_keys: called CC=4 SCAR=14.9
Pod documented
45 9 ▶ die 'level_keys() expects level index argument'
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($index) 0 9 9
46 unless (looks_like_number $index);
47
48 9 ▶ die "No such level index '$index'"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->levels->[$index] 0 9 9
49 unless ($self->levels->[$index]);
50  
51 9 ▶ return $self->_all_level_keys->{$index} || {};
Condition50%1/2
$self->_all_level_keys->{$index} || {}
l!l
90
MC/DC0%0/1
$self->_all_level_keys->{$index} || {}
$self->_all_level_keys->{$index}
Truth table50%1/2
A
A= $self->_all_level_keys->{$index}
Aresult
11
00
52 }
53  
54 # Clears the Hash of any existing data
55 sub reset {
56 13 ▶ my $self = shift;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
57 13 %{$self->_Hash} = ();
58 13 %{$self->_all_level_keys} = ();
59 13 %{$self->_def_key_bitmasks} = ();
60 13 return $self;
61 }
62  
63 17 ▶sub clone { Clone::clone(shift) }
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
64  
65  
66 around BUILDARGS => sub {
67 12 ▶ my ($orig, $self, @args) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.3
68 12 ▶ my %opt = (ref($args[0]) eq 'HASH') ? %{ $args[0] } : @args; # <-- arg as hash or hashref
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] eq 'HASH' ? : 12 0 12
69
70 # Accept 'levels' as shorthand numeric value:
71 12 ▶ if($opt{levels} && looks_like_number $opt{levels}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"levels"} and &looks_like_number($opt{"levels"})) 5 7 12
Condition66%2/3
$opt{"levels"} and &looks_like_number($opt{"levels"})
!ll&&!rl&&r
075
MC/DC50%1/2
$opt{"levels"} and &looks_like_number($opt{"levels"})
$opt{"levels"} &looks_like_number($opt{"levels"})
Truth table66%2/3
A and B
A= $opt{"levels"}B= &looks_like_number($opt{"levels"})
ABresult
0X0
100
111
72 5 my $num = $opt{levels} - 1;
73 5 ▶ $opt{delimiter} ||= '/';
Condition50%1/2
$opt{"delimiter"} ||= "/"
l!l
05
MC/DC0%0/1
$opt{"delimiter"} ||= "/"
$opt{"delimiter"}
Truth table50%1/2
A
A= $opt{"delimiter"}
Aresult
11
00
74 5 my @levels = ({ delimiter => $opt{delimiter} }) x $num;
75 5 $opt{levels} = [ @levels, {} ];
76 5 delete $opt{delimiter};
77 }
78  
79 12 return $self->$orig(%opt);
80 5 };
81  
82  
83 sub BUILD {
84 12 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
85 12 $self->_post_validate;
86 }
87  
88 sub _post_validate {
89 12 ▶ my $self = shift;
Subroutine _post_validate: called CC=5 SCAR=16.3
90  
91 12 ▶ if($self->allow_deep_values) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->allow_deep_values) 11 1 12
92 11 for my $Lvl (@{$self->levels}) {
93 76 ▶ die join("",
Branch: 1 branch
BranchTrueFalseTotal
if $Lvl->delimiter and $Lvl->delimiter eq $self->deep_delimiter 0 76 76
Condition66%2/3
$Lvl->delimiter and $Lvl->delimiter eq $self->deep_delimiter
!ll&&!rl&&r
11650
MC/DC0%0/2
$Lvl->delimiter and $Lvl->delimiter eq $self->deep_delimiter
$Lvl->delimiter $Lvl->delimiter eq $self->deep_delimiter
Truth table66%2/3
A and B
A= $Lvl->delimiterB= $Lvl->delimiter eq $self->deep_delimiter
ABresult
0X0
100
111
94 "Level delimiters must be different from the deep_delimiter ('",
95 $self->deep_delimiter,"').\n",
96 "Please specify a different level delimiter or change 'deep_delimiter'"
97 ) if ($Lvl->delimiter && $Lvl->delimiter eq $self->deep_delimiter);
98 }
99 }
100  
101 }
102  
103 sub coercer {
104 2 ▶ my $self = (shift)->clone;
Subroutine coercer: called CC=1 SCAR=0.0
Pod documented
105 2 ▶ return sub { $self->coerce(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
106 }
107  
108 sub coerce {
109 7 ▶ my ($self, @args) = @_;
Subroutine coerce: called CC=9 SCAR=26.7
Pod documented
110 7 ▶ die 'coerce() is not a class method' unless (blessed $self);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 7 7
111 7 ▶ if(scalar(@args) == 1){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args == 1) 4 3 7
112 4 ▶ if(ref($args[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args[0]) { } 1 3 4
elsif (not defined $args[0]) { } 0 3 3
113 1 ▶ return $args[0] if (blessed($args[0]) && blessed($args[0]) eq __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($args[0]) and builtin::blessed($args[0]) eq __PACKAGE__ 1 0 1
Condition33%1/3
builtin::blessed($args[0]) and builtin::blessed($args[0]) eq __PACKAGE__
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($args[0]) and builtin::blessed($args[0]) eq __PACKAGE__
builtin::blessed($args[0]) builtin::blessed($args[0]) eq __PACKAGE__
Truth table33%1/3
A and B
A= builtin::blessed($args[0])B= builtin::blessed($args[0]) eq __PACKAGE__
ABresult
0X0
100
111
114 0 ▶ @args = @{$args[0]} if (ref($args[0]) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[0] eq "ARRAY" 0 0 0
115 }
116 elsif(! defined $args[0]) {
117 0 return $self->clone->reset;
118 }
119 }
120 6 my $new = $self->clone->reset;
121 6 ▶ return scalar(@args) > 0 ? $new->load(@args) : $new;
Branch: 1 branch
BranchTrueFalseTotal
scalar @args > 0 ? : 6 0 6
122 }
123  
124 sub lookup {
125 50 ▶ my ($self, $key_str, @addl) = @_;
Subroutine lookup: called CC=3 SCAR=11.4
Pod documented
126 50 ▶ return undef unless (defined $key_str);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key_str 0 50 50
127 50 ▶ die join(' ',
Branch: 1 branch
BranchTrueFalseTotal
if scalar @addl > 0 0 50 50
128 "lookup() expects a single composite key string argument",
129 "(did you mean to use 'lookup_path'?)"
130 ) if (scalar(@addl) > 0);
131 50 return $self->lookup_path( $self->resolve_key_path($key_str) );
132 }
133  
134 sub lookup_path {
135 63 ▶ my ($self, @path) = @_;
Subroutine lookup_path: called CC=16 SCAR=28.2
Pod documented
136 # lookup_path() is the same as get_path() when lookup_mode is 'get':
137 63 ▶ return $self->get_path(@path) if ($self->lookup_mode eq 'get');
Branch: 1 branch
BranchTrueFalseTotal
if $self->lookup_mode eq "get" 2 61 63
138
139 61 ▶ return undef unless (defined $path[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path[0] 0 61 61
140
141 61 my $hash_val;
142  
143 # If the exact path is set and is NOT a hash (that may need merging),
144 # return it outright:
145 61 ▶ if($self->exists_path(@path)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exists_path(@path)) 28 33 61
146 28 my $val = $self->get_path(@path);
147 28 ▶ return $val unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val and ref $val eq "HASH" and $self->lookup_mode eq "merge" 8 8 16
Condition83%5/6
ref $val and ref $val eq "HASH" and $self->lookup_mode eq "merge"
!ll&&!rl&&r
1288
ref $val and ref $val eq "HASH"
!ll&&!rl&&r
12016
MC/DC66%2/3
ref $val and ref $val eq "HASH" and $self->lookup_mode eq "merge"
ref $val ref $val eq "HASH" $self->lookup_mode eq "merge"
Truth table75%3/4
A and B and C
A= ref $valB= ref $val eq "HASH"C= $self->lookup_mode eq "merge"
ABCresult
0XX0
10X0
1100
1111
148 ref $val && ref($val) eq 'HASH'
149 && $self->lookup_mode eq 'merge'
150 );
151 # Set the first hash_val:
152 8 ▶ $hash_val = $val if(ref $val && ref($val) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $val and ref $val eq "HASH" 8 0 8
Condition33%1/3
ref $val and ref $val eq "HASH"
!ll&&!rl&&r
008
MC/DC0%0/2
ref $val and ref $val eq "HASH"
ref $val ref $val eq "HASH"
Truth table33%1/3
A and B
A= ref $valB= ref $val eq "HASH"
ABresult
0X0
100
111
153 }
154
155 41 my @set = $self->_enumerate_default_paths(@path);
156
157 41 my @values = ();
158 41 for my $dpath (@set) {
159 75 ▶ $self->exists_path(@$dpath) or next;
Branch: 1 branch
BranchTrueFalseTotal
$self->exists_path(@$dpath) or next 30 45 75
160 45 my $val = $self->get_path(@$dpath);
161 45 ▶ return $val unless ($self->lookup_mode eq 'merge');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->lookup_mode eq "merge" 6 39 45
162 39 ▶ if (ref $val && ref($val) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val and ref $val eq 'HASH') { } 23 16 39
Condition66%2/3
ref $val and ref $val eq "HASH"
!ll&&!rl&&r
16023
MC/DC50%1/2
ref $val and ref $val eq "HASH"
ref $val ref $val eq "HASH"
Truth table66%2/3
A and B
A= ref $valB= ref $val eq "HASH"
ABresult
0X0
100
111
163 # Set/merge hashes:
164 23 ▶ $hash_val = $hash_val ? merge($val,$hash_val) : $val;
Branch: 1 branch
BranchTrueFalseTotal
$hash_val ? : 20 3 23
165 }
166 else {
167 # Return the first non-hash value unless a hash has already been
168 # encountered, and if that is the case, we can't merge a non-hash,
169 # return the hash we already had now
170 16 ▶ return $hash_val ? $hash_val : $val;
Branch: 1 branch
BranchTrueFalseTotal
$hash_val ? : 0 16 16
171 }
172 }
173
174 # If nothing was found, $hash_val will still be undef:
175 19 return $hash_val;
176 }
177  
178 # Only returns the lookup_path value if it is a "leaf" -
179 # any value that is NOT a populated HashRef
180 sub lookup_leaf_path {
181 0 ▶ my ($self, @path) = @_;
Subroutine lookup_leaf_path: not called CC=4 SCAR=30.0
Pod documented
182 0 my $v = $self->lookup_path(@path);
183 0 ▶ return (ref $v && ref($v) eq 'HASH' && scalar(keys %$v) > 0) ? undef : $v;
Branch: 1 branch
BranchTrueFalseTotal
ref $v && ref $v eq 'HASH' && scalar keys %$v > 0 ? : 0 0 0
Condition0%0/6
ref $v and ref $v eq "HASH" and scalar keys %$v > 0
!ll&&!rl&&r
000
ref $v and ref $v eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
ref $v and ref $v eq "HASH" and scalar keys %$v > 0
ref $v ref $v eq "HASH" scalar keys %$v > 0
Truth table0%0/4
A and B and C
A= ref $vB= ref $v eq "HASH"C= scalar keys %$v > 0
ABCresult
0XX0
10X0
1100
1111
184 }
185  
186 sub get {
187 3 ▶ my ($self, $key_str, @addl) = @_;
Subroutine get: called CC=3 SCAR=11.4
Pod documented
188 3 ▶ return undef unless (defined $key_str);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key_str 0 3 3
189 3 ▶ die join(' ',
Branch: 1 branch
BranchTrueFalseTotal
if scalar @addl > 0 0 3 3
190 "get() expects a single composite key string argument",
191 "(did you mean to use 'get_path'?)"
192 ) if (scalar(@addl) > 0);
193 3 return $self->get_path( $self->resolve_key_path($key_str) );
194 }
195  
196 sub get_path {
197 82 ▶ my ($self, @path) = @_;
Subroutine get_path: called CC=2 SCAR=7.0
Pod documented
198 82 ▶ return undef unless (defined $path[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path[0] 0 82 82
199  
200 82 my $value;
201 82 my $ev_path = $self->_as_eval_path(@path);
202 82 eval join('','$value = $self->Data->',$ev_path);
203
204 82 return $value;
205 }
206  
207 sub exists {
208 0 ▶ my ($self, $key_str, @addl) = @_;
Subroutine exists: not called CC=3 SCAR=24.8
Pod documented
209 0 ▶ return undef unless (defined $key_str);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key_str 0 0 0
210 0 ▶ die join(' ',
Branch: 1 branch
BranchTrueFalseTotal
if scalar @addl > 0 0 0 0
211 "exists() expects a single composite key string argument",
212 "(did you mean to use 'exists_path'?)"
213 ) if (scalar(@addl) > 0);
214 0 return $self->exists_path( $self->resolve_key_path($key_str) );
215 }
216  
217 sub exists_path {
218 142 ▶ my ($self, @path) = @_;
Subroutine exists_path: called CC=2 SCAR=7.0
Pod documented
219 142 ▶ return 0 unless (defined $path[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path[0] 0 142 142
220  
221 142 my $ev_path = $self->_as_eval_path(@path);
222 142 return eval join('','exists $self->Data->',$ev_path);
223 }
224  
225 sub delete {
226 4 ▶ my ($self, $key_str, @addl) = @_;
Subroutine delete: called CC=3 SCAR=11.4
Pod documented
227 4 ▶ return undef unless (defined $key_str);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key_str 0 4 4
228 4 ▶ die join(' ',
Branch: 1 branch
BranchTrueFalseTotal
if scalar @addl > 0 0 4 4
229 "delete() expects a single composite key string argument",
230 "(did you mean to use 'delete_path'?)"
231 ) if (scalar(@addl) > 0);
232 4 return $self->delete_path( $self->resolve_key_path($key_str) );
233 }
234  
235 sub delete_path {
236 6 ▶ my ($self, @path) = @_;
Subroutine delete_path: called CC=3 SCAR=11.0
Pod documented
237 6 ▶ return 0 unless (defined $path[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path[0] 0 6 6
238
239 # TODO: should this die?
240 6 ▶ return undef unless ($self->exists_path(@path));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists_path(@path) 2 4 6
241
242 4 my $data = $self->Data; #<-- this is a *copy* of the data
243 4 my $ev_path = $self->_as_eval_path(@path);
244
245 # Delete teh value from our copy:
246 4 my $ret; eval join('','$ret = delete $data->',$ev_path);
247
248 # To delete safely, we actually have to reload all the data
249 # (except what is being deleted), from scratch. This is to
250 # make sure all the other indexes and counters remain in a
251 # consistent state:
252 4 $self->reset->load($data);
253
254 # Return whatever was actually returned from the "real" delete:
255 4 return $ret;
256 }
257  
258 # Use bitwise math to enumerate all possible prefix, default key paths:
259 sub _enumerate_default_paths {
260 41 ▶ my ($self, @path) = @_;
Subroutine _enumerate_default_paths: called CC=5 SCAR=16.1
261  
262 41 my $def_val = $self->default_key;
263 41 my $depth = $self->num_levels;
264  
265 41 my @set = ();
266 41 my %seen_combo = ();
267  
268 ## enumerate every possible default path bitmask (slow with many levels):
269 #my $bits = 2**$depth;
270 #my @mask_sets = ();
271 #push @mask_sets, $bits while(--$bits >= 0);
272
273 # default path bitmasks only for paths we know are set (much faster):
274 41 my @mask_sets = keys %{$self->_def_key_bitmasks};
275
276 # Re-sort the mask sets as reversed *strings*, because we want
277 # '011' to come before '110'
278 @mask_sets = sort {
279 41 reverse(sprintf('%0'.$depth.'b',$a)) cmp
280 reverse(sprintf('%0'.$depth.'b',$b))
281 } @mask_sets;
282
283 41 for my $mask (@mask_sets) {
284 107 my @combo = ();
285 107 my $check_mask = 2**$depth >> 1;
286 107 for my $k (@path) {
287 # Use bitwise AND to decide whether or not to swap the
288 # default value for the actual key:
289 514 ▶ push @combo, $check_mask & $mask ? $def_val : $k;
Branch: 1 branch
BranchTrueFalseTotal
$check_mask & $mask ? : 372 142 514
290
291 # Shift the check bit position by one for the next key:
292 514 $check_mask = $check_mask >> 1;
293 }
294 107 ▶ push @set, \@combo unless ($seen_combo{join('/',@combo)}++);
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_combo{join "/", @combo}++ 90 17 107
295 }
296  
297 41 return @set;
298 }
299  
300 sub load {
301 26 ▶ my $self = shift;
Subroutine load: called CC=1 SCAR=0.0
Pod documented
302 26 return $self->_load(0,$self->_Hash,@_);
303 }
304  
305 sub _load {
306 371 ▶ my ($self, $index, $noderef, @args) = @_;
Subroutine _load: called CC=20 SCAR=30.2
307
308 371 ▶ my $Lvl = $self->levels->[$index] or die "Bad level index '$index'";
Branch: 1 branch
BranchTrueFalseTotal
my $Lvl = $self->levels->[$index] or die "Bad level index '${index}'" 0 371 371
309 371 my $last_level = ! $self->levels->[$index+1];
310
311 371 for my $arg (@args) {
312 394 ▶ die "Undef keys are not allowed" unless (defined $arg);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 394 394
313
314 394 ▶ my $force_composite = $self->{_force_composite} || 0;
Condition100%2/2
$self->{'_force_composite'} || 0
l!l
3391
MC/DC100%1/1
$self->{'_force_composite'} || 0
$self->{'_force_composite'}
Truth table100%2/2
A
A= $self->{'_force_composite'}
Aresult
11
00
315 394 local $self->{_force_composite} = 0; #<-- clear if set to prevetn deep recursion
316 394 ▶ unless (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $arg) 18 376 394
317 # hanging string/scalar, convert using default value
318 18 $arg = { $arg => $self->default_value };
319 18 $force_composite = 1;
320 }
321
322 394 ▶ die "Cannot load non-hash reference!" unless (ref($arg) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg eq "HASH" 0 394 394
323
324 394 for my $key (keys %$arg) {
325 412 ▶ die "Only scalar/string keys are allowed"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and not ref $key 0 412 412
Condition33%1/3
defined $key and not ref $key
!ll&&!rl&&r
00412
MC/DC0%0/2
defined $key and not ref $key
defined $key not ref $key
Truth table33%1/3
A and not(B)
A= defined $keyB= not ref $key
ABresult
0X0
100
111
326 unless (defined $key && ! ref($key));
327
328 412 my $val = $arg->{$key};
329 412 ▶ my $is_hashval = ref $val && ref($val) eq 'HASH';
Condition66%2/3
ref $val && ref $val eq 'HASH'
!ll&&!rl&&r
1000312
MC/DC50%1/2
ref $val && ref $val eq 'HASH'
ref $val ref $val eq 'HASH'
Truth table66%2/3
A && B
A= ref $valB= ref $val eq 'HASH'
ABresult
0X0
100
111
330
331 412 ▶ if( $force_composite || $self->_is_composite_key($key,$index) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_composite or $self->_is_composite_key($key, $index)) { } 56 356 412
Condition100%3/3
$force_composite or $self->_is_composite_key($key, $index)
l!l&&r!l&&!r
2135356
MC/DC100%2/2
$force_composite or $self->_is_composite_key($key, $index)
$force_composite $self->_is_composite_key($key, $index)
Truth table100%3/3
A or B
A= $force_compositeB= $self->_is_composite_key($key, $index)
ABresult
1X1
011
000
332 56 my $no_fill = $is_hashval;
333 56 my @path = $self->resolve_key_path($key,$index,$no_fill);
334 56 my $lkey = pop @path;
335 56 my $hval = {};
336 56 ▶ if(scalar(@path) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @path > 0) { } 56 0 56
337 56 $self->_init_hash_path($hval,@path)->{$lkey} = $val;
338 }
339 else {
340 0 $hval->{$lkey} = $val;
341 }
342 56 $self->_load($index,$noderef,$hval);
343 }
344 else {
345
346 356 local $self->{_path_bitmask} = $self->{_path_bitmask};
347 356 ▶ my $bm = 0; $self->{_path_bitmask} ||= \$bm;
Condition100%2/2
$self->{"_path_bitmask"} ||= \$bm
l!l
29165
MC/DC100%1/1
$self->{"_path_bitmask"} ||= \$bm
$self->{"_path_bitmask"}
Truth table100%2/2
A
A= $self->{"_path_bitmask"}
Aresult
11
00
348 356 my $bmref = $self->{_path_bitmask};
349 356 ▶ if($key eq $self->default_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq $self->default_key) 222 134 356
350 222 my $depth = 2**($self->num_levels);
351 222 $$bmref = $$bmref | ($depth >> $index+1);
352 }
353
354 356 $self->_all_level_keys->{$index}{$key} = 1;
355 356 ▶ if($is_hashval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hashval) { } 304 52 356
356 304 $self->_init_hash_path($noderef,$key);
357 304 ▶ if($last_level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_level) { } 15 289 304
358 15 $noderef->{$key} = merge($noderef->{$key}, $val);
359 }
360 else {
361 # Set via recursive:
362 289 $self->_load($index+1,$noderef->{$key},$val);
363 }
364 }
365 else {
366 52 $noderef->{$key} = $val;
367 }
368
369 356 ▶ if($index == 0 && $$bmref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == 0 and $$bmref) 42 314 356
Condition100%3/3
$index == 0 and $$bmref
!ll&&!rl&&r
2912342
MC/DC100%2/2
$index == 0 and $$bmref
$index == 0 $$bmref
Truth table100%3/3
A and B
A= $index == 0B= $$bmref
ABresult
0X0
100
111
370 42 $self->_def_key_bitmasks->{$$bmref} = 1;
371 }
372 }
373 }
374 }
375
376 371 return $self;
377 }
378  
379  
380 sub path_to_composite_key {
381 3 ▶ my ($self, @path) = @_;
Subroutine path_to_composite_key: called CC=1 SCAR=0.0
Pod documented
382 3 return $self->_path_to_composite_key(0,@path);
383 }
384  
385 sub _path_to_composite_key {
386 9 ▶ my ($self, $index, @path) = @_;
Subroutine _path_to_composite_key: called CC=5 SCAR=16.4
387  
388 9 ▶ my $Lvl = $self->levels->[$index] or die "Bad level index '$index'";
Branch: 1 branch
BranchTrueFalseTotal
my $Lvl = $self->levels->[$index] or die "Bad level index '${index}'" 0 9 9
389 9 my $last_level = ! $self->levels->[$index+1];
390
391 9 ▶ if($last_level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_level) { } 3 6 9
392 3 ▶ my $del = $self->deep_delimiter || '.';
Condition50%1/2
$self->deep_delimiter || '.'
l!l
30
MC/DC0%0/1
$self->deep_delimiter || '.'
$self->deep_delimiter
Truth table50%1/2
A
A= $self->deep_delimiter
Aresult
11
00
393 3 return join($del,@path);
394 }
395 else {
396 6 my $key = shift @path;
397 6 ▶ return scalar(@path) > 0 ? join(
Branch: 1 branch
BranchTrueFalseTotal
scalar @path > 0 ? : 6 0 6
398 $Lvl->delimiter,$key,
399 $self->_path_to_composite_key($index+1,@path)
400 ) : join('',$key,$Lvl->delimiter);
401 }
402 }
403  
404  
405 sub _init_hash_path {
406 360 ▶ my ($self,$hash,@path) = @_;
Subroutine _init_hash_path: called CC=6 SCAR=19.9
407 360 ▶ die "Not a hash" unless (ref $hash && ref($hash) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash and ref $hash eq "HASH" 0 360 360
Condition33%1/3
ref $hash and ref $hash eq "HASH"
!ll&&!rl&&r
00360
MC/DC0%0/2
ref $hash and ref $hash eq "HASH"
ref $hash ref $hash eq "HASH"
Truth table33%1/3
A and B
A= ref $hashB= ref $hash eq "HASH"
ABresult
0X0
100
111
408 360 ▶ die "No path supplied" unless (scalar(@path) > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @path > 0 0 360 360
409
410 360 my $ev_path = $self->_as_eval_path( @path );
411
412 360 my $hval;
413 360 eval join('','$hash->',$ev_path,' ||= {}');
414 360 eval join('','$hval = $hash->',$ev_path);
415 360 ▶ eval join('','$hash->',$ev_path,' = {}') unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hval and ref $hval eq "HASH" 0 360 360
Condition33%1/3
ref $hval and ref $hval eq "HASH"
!ll&&!rl&&r
00360
MC/DC0%0/2
ref $hval and ref $hval eq "HASH"
ref $hval ref $hval eq "HASH"
Truth table33%1/3
A and B
A= ref $hvalB= ref $hval eq "HASH"
ABresult
0X0
100
111
416 ref $hval && ref($hval) eq 'HASH'
417 );
418
419 360 return $hval;
420 }
421  
422  
423 sub set {
424 3 ▶ my ($self,$key,$value) = @_;
Subroutine set: called CC=4 SCAR=15.2
Pod documented
425 3 ▶ die "bad number of arguments passed to set" unless (scalar(@_) == 3);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 3 0 3 3
426 3 ▶ die '$key value is required' unless ($key && $key ne '');
Branch: 1 branch
BranchTrueFalseTotal
unless $key and $key ne "" 0 3 3
Condition33%1/3
$key and $key ne ""
!ll&&!rl&&r
003
MC/DC0%0/2
$key and $key ne ""
$key $key ne ""
Truth table33%1/3
A and B
A= $keyB= $key ne ""
ABresult
0X0
100
111
427 3 local $self->{_force_composite} = 1;
428 3 $self->load({ $key => $value });
429 }
430  
431  
432 sub _as_eval_path {
433 588 ▶ my ($self,@path) = @_;
Subroutine _as_eval_path: called CC=2 SCAR=7.1
434 return (scalar(@path) > 0) ? join('',
435 588 ▶ map { '{"'.$_.'"}' } @path
Branch: 1 branch
BranchTrueFalseTotal
scalar @path > 0 ? : 588 0 588
436 ) : undef;
437 }
438  
439 sub _eval_key_path {
440 0 ▶ my ($self, $key, $index) = @_;
Subroutine _eval_key_path: not called CC=1 SCAR=6.9
441 0 return $self->_as_eval_path(
442 $self->resolve_key_path($key,$index)
443 );
444 }
445  
446 # recursively scans the supplied key for any special delimiters defined
447 # by any of the levels, or the deep delimiter, if deep values are enabled
448 sub _is_composite_key {
449 3308 ▶ my ($self, $key, $index) = @_;
Subroutine _is_composite_key: called CC=7 SCAR=19.5
450 3308 ▶ $index ||= 0;
Condition100%2/2
$index ||= 0
l!l
321692
MC/DC100%1/1
$index ||= 0
$index
Truth table100%2/2
A
A= $index
Aresult
11
00
451
452 3308 my $Lvl = $self->levels->[$index];
453  
454 3308 ▶ if ($Lvl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Lvl) { } 2955 353 3308
455 2955 ▶ return 0 if ($Lvl->registered_keys && $Lvl->registered_keys->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if $Lvl->registered_keys and $Lvl->registered_keys->{$key} 5 2950 2955
Condition100%3/3
$Lvl->registered_keys and $Lvl->registered_keys->{$key}
!ll&&!rl&&r
2939115
MC/DC100%2/2
$Lvl->registered_keys and $Lvl->registered_keys->{$key}
$Lvl->registered_keys $Lvl->registered_keys->{$key}
Truth table100%3/3
A and B
A= $Lvl->registered_keysB= $Lvl->registered_keys->{$key}
ABresult
0X0
100
111
456 2950 ▶ return $Lvl->_peel_str_key($key) || $self->_is_composite_key($key,$index+1);
Condition100%3/3
$Lvl->_peel_str_key($key) || $self->_is_composite_key($key, $index + 1)
l!l&&r!l&&!r
3382909
MC/DC100%2/2
$Lvl->_peel_str_key($key) || $self->_is_composite_key($key, $index + 1)
$Lvl->_peel_str_key($key) $self->_is_composite_key($key, $index + 1)
Truth table100%3/3
A || B
A= $Lvl->_peel_str_key($key)B= $self->_is_composite_key($key, $index + 1)
ABresult
1X1
011
000
457 }
458 else {
459 353 ▶ if($self->allow_deep_values) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->allow_deep_values) { } 353 0 353
460 353 my $del = $self->deep_delimiter;
461 353 return $key =~ /\Q${del}\E/;
462 }
463 else {
464 0 return 0;
465 }
466 }
467 }
468  
469 sub resolve_key_path {
470 950 ▶ my ($self, $key, $index, $no_fill) = @_;
Subroutine resolve_key_path: called CC=12 SCAR=24.9
Pod documented
471 950 ▶ $index ||= 0;
Condition100%2/2
$index ||= 0
l!l
817133
MC/DC100%1/1
$index ||= 0
$index
Truth table100%2/2
A
A= $index
Aresult
11
00
472 950 ▶ $no_fill ||= $self->no_fill;
Condition100%3/3
$no_fill ||= $self->no_fill
l!l&&r!l&&!r
433904
MC/DC100%2/2
$no_fill ||= $self->no_fill
$no_fill $self->no_fill
Truth table100%3/3
A ||= B
A= $no_fillB= $self->no_fill
ABresult
1X1
011
000
473
474 950 my $Lvl = $self->levels->[$index];
475 950 my $last_level = ! $self->levels->[$index+1];
476
477 950 ▶ if ($Lvl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Lvl) { } 817 133 950
478 817 my ($peeled,$leftover) = $Lvl->_peel_str_key($key);
479 817 ▶ if($peeled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($peeled) { } 210 607 817
480 210 local $self->{_composite_key_peeled} = 1;
481 # If a key was peeled, move on to the next level with leftovers:
482 210 ▶ return ($peeled, $self->resolve_key_path($leftover,$index+1,$no_fill)) if ($leftover);
Branch: 1 branch
BranchTrueFalseTotal
if $leftover 192 18 210
483
484 # If there were no leftovers, recurse again only for the last level,
485 # otherwise, return now (this only makes a difference for deep values)
486 18 ▶ return $last_level ? $self->resolve_key_path($peeled,$index+1,$no_fill) : $peeled;
Branch: 1 branch
BranchTrueFalseTotal
$last_level ? : 0 18 18
487 }
488 else {
489 # If a key was not peeled, add the default key at the top of the path
490 # only if we're not already at the last level and 'no_fill' is not set
491 # (and we've already peeled at least one key)
492 607 my @path = $self->resolve_key_path($key,$index+1,$no_fill);
493 607 ▶ my $as_is = $last_level || ($no_fill && $self->{_composite_key_peeled});
Condition100%6/6
$last_level || $no_fill && $self->{'_composite_key_peeled'}
l!l&&r!l&&!r
12910468
$no_fill and $self->{"_composite_key_peeled"}
!ll&&!rl&&r
465310
MC/DC100%3/3
$last_level || $no_fill && $self->{'_composite_key_peeled'}
$last_level $no_fill $self->{"_composite_key_peeled"}
Truth table100%4/4
A || B and C
A= $last_levelB= $no_fillC= $self->{"_composite_key_peeled"}
ABCresult
1XX1
0111
00X0
0100
494 607 ▶ return $self->no_pad || $as_is ? @path : ($self->default_key,@path);
Branch: 1 branch
BranchTrueFalseTotal
$self->no_pad || $as_is ? : 146 461 607
Condition100%3/3
$self->no_pad or $as_is
l!l&&r!l&&!r
10136461
MC/DC100%2/2
$self->no_pad or $as_is
$self->no_pad $as_is
Truth table100%3/3
A or B
A= $self->no_padB= $as_is
ABresult
1X1
011
000
495 }
496 }
497 else {
498 133 ▶ if($self->allow_deep_values) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->allow_deep_values) { } 132 1 133
499 132 my $del = $self->deep_delimiter;
500 132 return split(/\Q${del}\E/,$key);
501 }
502 else {
503 1 return $key;
504 }
505 }
506 }
507  
508  
509 sub _coerce_levels_param {
510 12 ▶ my $val = shift;
Subroutine _coerce_levels_param: called CC=12 SCAR=28.3
511 12 ▶ return $val unless (ref($val) && ref($val) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val and ref $val eq "ARRAY" 0 12 12
Condition33%1/3
ref $val and ref $val eq "ARRAY"
!ll&&!rl&&r
0012
MC/DC0%0/2
ref $val and ref $val eq "ARRAY"
ref $val ref $val eq "ARRAY"
Truth table33%1/3
A and B
A= ref $valB= ref $val eq "ARRAY"
ABresult
0X0
100
111
512
513 12 my %seen = ();
514 12 my $i = 0;
515 12 my @new = ();
516 12 for my $itm (@$val) {
517 79 ▶ return $val if (blessed $itm);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($itm) 0 79 79
518
519 79 ▶ die "'levels' must be an arrayref of hashrefs" unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ref $itm and ref $itm eq "HASH" 0 79 79
Condition33%1/3
ref $itm and ref $itm eq "HASH"
!ll&&!rl&&r
0079
MC/DC0%0/2
ref $itm and ref $itm eq "HASH"
ref $itm ref $itm eq "HASH"
Truth table33%1/3
A and B
A= ref $itmB= ref $itm eq "HASH"
ABresult
0X0
100
111
520 ref($itm) && ref($itm) eq 'HASH'
521 );
522
523 die "duplicate level name '$itm->{name}'" if (
524 79 ▶ $itm->{name} && $seen{$itm->{name}}++
Branch: 1 branch
BranchTrueFalseTotal
if $itm->{"name"} and $seen{$itm->{"name"}}++ 0 79 79
Condition66%2/3
$itm->{"name"} and $seen{$itm->{"name"}}++
!ll&&!rl&&r
64150
MC/DC0%0/2
$itm->{"name"} and $seen{$itm->{"name"}}++
$itm->{"name"} $seen{$itm->{"name"}}++
Truth table66%2/3
A and B
A= $itm->{"name"}B= $seen{$itm->{"name"}}++
ABresult
0X0
100
111
525 );
526
527 die "the last level is not allowed to have a delimiter" if(
528 scalar(@$val) == ++$i
529 && $itm->{delimiter}
530 79 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$val == ++$i and $itm->{"delimiter"} 0 79 79
Condition66%2/3
scalar @$val == ++$i and $itm->{"delimiter"}
!ll&&!rl&&r
67120
MC/DC0%0/2
scalar @$val == ++$i and $itm->{"delimiter"}
scalar @$val == ++$i $itm->{"delimiter"}
Truth table66%2/3
A and B
A= scalar @$val == ++$iB= $itm->{"delimiter"}
ABresult
0X0
100
111
531
532 79 push @new, Hash::Layout::Level->new({
533 %$itm,
534 index => $i-1
535 });
536 }
537
538 12 ▶ die "no levels specified" unless (scalar(@new) > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @new > 0 0 12 12
539
540 12 return \@new;
541 }
542  
543  
544 # debug method:
545 sub def_key_bitmask_strings {
546 0 ▶ my $self = shift;
Subroutine def_key_bitmask_strings: not called CC=1 SCAR=6.9
Pod documented
547 0 my $depth = $self->num_levels;
548 0 my @masks = keys %{$self->_def_key_bitmasks};
549 0 map { sprintf('%0'.$depth.'b',$_) } @masks;
550 }
551  
552 5 1;
553  
554  
555 __END__
↑ Summary Layout/Level.pm »