NestedKey.pm

filter: Statementstmt 96.5% 193 / 200 Branchbran 82.0% 87 / 106 Conditioncond 67.6% 48 / 71 MC/DCmcdc 41.4% 17 / 41 Subroutinesub 100% 19 / 19 Podpod 100% 6 / 6 totaltotal 83.5% 370 / 443 CC 87 SCAR 27.8
CC
87
Cov
87%
delete
30.5
set
18.8
exists_key
13.2
CRAP 16.1
« NestedKey/CLI.pm ↑ Summary
1 package Data::NestedKey;
2  
3 # This module provides an object-oriented way to manipulate deeply
4 # nested hash structures using dot-separated keys, with flexible
5 # serialization options.
6  
7 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use List::Util qw(pairs);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Storable qw(nfreeze);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use YAML::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 our $VERSION = '1.3.0';
19  
20 # Package variables for serialization options
21 9 our $JSON_PRETTY = 1; # Controls whether JSON output is pretty or compact
22 9 our $FORMAT = 'JSON'; # Default serialization format
23  
24 9 ▶use overload '""' => \&as_string;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 ########################################################################
27 24 ▶sub new {
Subroutine new: called CC=10 SCAR=24.9
Pod documented
28 ########################################################################
29 24 my ( $class, @args ) = @_;
30  
31 24 ▶ my $init_data = ref $args[0] ? shift @args : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 21 3 24
32 24 my @kv_list = @args;
33  
34 # Accept a hash ref OR an array ref as the data root; anything else
35 # (a non-ref first arg) is treated as the start of the kv list.
36 24 ▶ my $data
Branch: 1 branch
BranchTrueFalseTotal
_is_hash($init_data) || _is_array($init_data) ? : 24 0 24
Condition66%2/3
_is_hash($init_data) or _is_array($init_data)
l!l&&r!l&&!r
2130
MC/DC0%0/2
_is_hash($init_data) or _is_array($init_data)
_is_hash($init_data) _is_array($init_data)
Truth table66%2/3
A or B
A= _is_hash($init_data)B= _is_array($init_data)
ABresult
1X1
011
000
37 = ( _is_hash($init_data) || _is_array($init_data) ) ? $init_data : {};
38  
39 24 my $self = bless { data => $data }, $class;
40  
41 # If the first arg wasn't usable as init data, treat it as a kv element.
42 24 ▶ if ( !_is_hash($init_data) && !_is_array($init_data) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_hash($init_data) or _is_array($init_data)) 0 24 24
Condition66%2/3
_is_hash($init_data) or _is_array($init_data)
l!l&&r!l&&!r
2130
MC/DC0%0/2
_is_hash($init_data) or _is_array($init_data)
_is_hash($init_data) _is_array($init_data)
Truth table66%2/3
A or B
A= _is_hash($init_data)B= _is_array($init_data)
ABresult
1X1
011
000
43 0 @kv_list = ( $init_data, @kv_list );
44 }
45  
46 24 ▶ return $self
Branch: 1 branch
BranchTrueFalseTotal
unless @kv_list 21 3 24
47 if !@kv_list;
48  
49 3 ▶ croak 'Must provide key-value pairs'
Branch: 1 branch
BranchTrueFalseTotal
if @kv_list % 2 != 0 0 3 3
50 if @kv_list % 2 != 0;
51  
52 # set() only makes sense against a hash root; reject kv pairs on an array root.
53 croak 'Cannot set key-value pairs on an array-rooted structure'
54 3 ▶ if _is_array( $self->{data} ) && @kv_list;
Branch: 1 branch
BranchTrueFalseTotal
if _is_array($self->{"data"}) and @kv_list 0 3 3
Condition33%1/3
_is_array($self->{"data"}) and @kv_list
!ll&&!rl&&r
300
MC/DC0%0/2
_is_array($self->{"data"}) and @kv_list
_is_array($self->{"data"}) @kv_list
Truth table33%1/3
A and B
A= _is_array($self->{"data"})B= @kv_list
ABresult
0X0
100
111
55  
56 3 $self->set(@kv_list);
57  
58 3 return $self;
59 }
60  
61 ########################################################################
62 50 ▶sub _is_array { return ref $_[0] && reftype( $_[0] ) eq 'ARRAY'; }
Subroutine _is_array: called CC=2 SCAR=6.9
Condition100%3/3
ref $_[0] && builtin::reftype($_[0]) eq 'ARRAY'
!ll&&!rl&&r
3542
MC/DC100%2/2
ref $_[0] && builtin::reftype($_[0]) eq 'ARRAY'
ref $_[0] builtin::reftype($_[0]) eq 'ARRAY'
Truth table100%3/3
A && B
A= ref $_[0]B= builtin::reftype($_[0]) eq 'ARRAY'
ABresult
0X0
100
111
63 ########################################################################
64  
65 ########################################################################
66 174 ▶sub _is_hash { return ref $_[0] && reftype( $_[0] ) eq 'HASH'; }
Subroutine _is_hash: called CC=2 SCAR=6.9
Condition100%3/3
ref $_[0] && builtin::reftype($_[0]) eq 'HASH'
!ll&&!rl&&r
257142
MC/DC100%2/2
ref $_[0] && builtin::reftype($_[0]) eq 'HASH'
ref $_[0] builtin::reftype($_[0]) eq 'HASH'
Truth table100%3/3
A && B
A= ref $_[0]B= builtin::reftype($_[0]) eq 'HASH'
ABresult
0X0
100
111
67 ########################################################################
68  
69 # Parse a dot-separated path into a list of segment hashrefs.
70 # Each segment has:
71 # key => the hash key name
72 # index => array index (integer, possibly negative), or undef if none
73 #
74 # Examples:
75 # 'foo.bar' -> [{key=>'foo'},{key=>'bar'}]
76 # 'repositories[0].uri' -> [{key=>'repositories',index=>0},{key=>'uri'}]
77 # 'items[-1]' -> [{key=>'items',index=>-1}]
78  
79 ########################################################################
80 60 ▶sub _parse_path {
Subroutine _parse_path: called CC=4 SCAR=13.9
81 ########################################################################
82 60 my ($path) = @_;
83  
84 60 my @segments;
85  
86 60 for my $part ( split /[.]/, $path ) {
87 111 ▶ if ( $part =~ /\A (.*?) \[ (-?\d+) \] \z/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /\A (.*?) \[ (-?\d+) \] \z/msx) { } 34 77 111
88 34 my ( $key, $idx ) = ( $1, $2 + 0 );
89  
90 34 ▶ if ( length $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $key) { } 22 12 34
91 22 push @segments, { key => $key, index => $idx };
92 }
93 else {
94 12 push @segments, { index => $idx }; # bare subscript: [0], [-1]
95 }
96 }
97 else {
98 77 push @segments, { key => $part, index => undef };
99 }
100 }
101  
102 60 return @segments;
103 }
104  
105 ########################################################################
106 26 ▶sub set {
Subroutine set: called CC=18 SCAR=29.4
Pod documented
107 ########################################################################
108 26 my ( $self, @kv_list ) = @_;
109  
110 26 ▶ croak 'Must provide key-value pairs'
Branch: 1 branch
BranchTrueFalseTotal
if @kv_list % 2 != 0 0 26 26
111 if @kv_list % 2 != 0;
112  
113 26 for my $p ( pairs @kv_list ) {
114 28 my ( $key_path, $value ) = @{$p};
115 28 ▶ my $action = $key_path =~ s/\A([+-])//xsm ? $1 : q{};
Branch: 1 branch
BranchTrueFalseTotal
$key_path =~ s/\A([+-])//msx ? : 7 21 28
116  
117 # set() does not support array subscripts in paths; use plain dot-keys only.
118 28 ▶ croak "Array subscripts (e.g. key[0]) are not supported in set() paths: '$key_path'"
Branch: 1 branch
BranchTrueFalseTotal
if $key_path =~ /\[/ 1 27 28
119 if $key_path =~ /\[/;
120  
121 27 my @keys = split /[.]/, $key_path;
122 27 my $current = $self->{data};
123  
124 27 for my $key ( @keys[ 0 .. $#keys - 1 ] ) {
125 20 ▶ $current->{$key} //= {};
Condition100%2/2
$current->{$key} //= {}
l!l
146
MC/DC100%1/1
$current->{$key} //= {}
$current->{$key}
Truth table100%2/2
A
A= $current->{$key}
Aresult
11
00
126 20 $current = $current->{$key};
127 }
128  
129 27 my $final_key = $keys[-1];
130  
131 27 ▶ if ( $action eq q{+} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq '+') { } 4 23 27
elsif ($action eq '-') { } 3 20 23
132 4 ▶ if ( _is_array( $current->{$final_key} ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (_is_array($current->{$final_key})) { } 1 3 4
elsif (_is_hash($current->{$final_key}) and _is_hash($value)) { } 1 2 3
elsif (_is_hash($current->{$final_key})) { } 0 2 2
elsif (exists $current->{$final_key}) { } 2 0 2
133 1 push @{ $current->{$final_key} }, $value;
134 }
135 ▶ elsif ( _is_hash( $current->{$final_key} ) && _is_hash($value) ) {
Condition66%2/3
_is_hash($current->{$final_key}) and _is_hash($value)
!ll&&!rl&&r
201
MC/DC50%1/2
_is_hash($current->{$final_key}) and _is_hash($value)
_is_hash($current->{$final_key}) _is_hash($value)
Truth table66%2/3
A and B
A= _is_hash($current->{$final_key})B= _is_hash($value)
ABresult
0X0
100
111
136 1 %{ $current->{$final_key} } = ( %{ $current->{$final_key} }, %{$value} );
137 }
138 elsif ( _is_hash( $current->{$final_key} ) ) {
139 0 croak sprintf q{Error: Attempting to merge a non-hash into a hash at key '%s'.}, $final_key;
140 }
141 elsif ( exists $current->{$final_key} ) {
142 2 $current->{$final_key} = [ $current->{$final_key}, $value ];
143 }
144 else {
145 0 $current->{$final_key} = [$value];
146 }
147 }
148 elsif ( $action eq q{-} ) {
149 3 ▶ if ( _is_array( $current->{$final_key} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (_is_array($current->{$final_key})) { } 1 2 3
elsif (_is_hash($current->{$final_key})) { } 1 1 2
150 1 @{ $current->{$final_key} } = grep { $_ ne $value } @{ $current->{$final_key} };
151 }
152 elsif ( _is_hash( $current->{$final_key} ) ) {
153 1 delete $current->{$final_key}{$value};
154 }
155 else {
156 1 delete $current->{$final_key};
157 }
158 }
159 else {
160 croak sprintf q{Error: Attempting to replace a hash reference at key '%s' with a scalar value.}, $final_key
161 20 ▶ if _is_hash( $current->{$final_key} ) && !_is_hash($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_hash($current->{$final_key}) and not _is_hash($value) 0 20 20
Condition33%1/3
_is_hash($current->{$final_key}) and not _is_hash($value)
!ll&&!rl&&r
2000
MC/DC0%0/2
_is_hash($current->{$final_key}) and not _is_hash($value)
_is_hash($current->{$final_key}) not _is_hash($value)
Truth table33%1/3
A and not(B)
A= _is_hash($current->{$final_key})B= not _is_hash($value)
ABresult
0X0
100
111
162  
163 20 $current->{$final_key} = $value;
164 }
165 }
166  
167 25 return $self;
168 }
169  
170 ########################################################################
171 32 ▶sub get {
Subroutine get: called CC=10 SCAR=23.1
Pod documented
172 ########################################################################
173 32 my ( $self, @key_paths ) = @_;
174 32 my @results;
175  
176 32 for my $key_path (@key_paths) {
177 33 my @segments = _parse_path($key_path);
178 33 my $current = $self->{data};
179 33 my $ok = 1;
180  
181 33 for my $seg (@segments) {
182 58 my ( $key, $idx ) = @{$seg}{qw(key index)};
183  
184 # Step into a hash key only if this segment has one.
185 58 ▶ if ( exists $seg->{key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seg->{"key"}) 55 3 58
186 55 ▶ if ( _is_hash($current) && exists $current->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_hash($current) and exists $current->{$key}) { } 53 2 55
Condition100%3/3
_is_hash($current) and exists $current->{$key}
!ll&&!rl&&r
1153
MC/DC100%2/2
_is_hash($current) and exists $current->{$key}
_is_hash($current) exists $current->{$key}
Truth table100%3/3
A and B
A= _is_hash($current)B= exists $current->{$key}
ABresult
0X0
100
111
187 53 $current = $current->{$key};
188 }
189 else {
190 2 $current = undef;
191 2 $ok = 0;
192 2 last;
193 }
194 }
195  
196 56 ▶ if ( defined $idx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $idx) 16 40 56
197 16 ▶ if ( _is_array($current) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_array($current)) { } 16 0 16
198 16 $current = $current->[$idx];
199 }
200 else {
201 0 $current = undef;
202 0 $ok = 0;
203 0 last;
204 }
205 }
206 }
207  
208 33 ▶ push @results, $ok ? $current : undef;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 31 2 33
209 }
210  
211 32 ▶ return wantarray ? @results : $results[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 13 19 32
212 }
213  
214 ########################################################################
215 19 ▶sub as_string {
Subroutine as_string: called CC=8 SCAR=21.6
Pod documented
216 ########################################################################
217 19 my ($self) = @_;
218  
219 19 ▶ return JSON->new->pretty->encode( $self->{data} ) if $FORMAT eq 'JSON' && $JSON_PRETTY;
Branch: 1 branch
BranchTrueFalseTotal
if $Data::NestedKey::FORMAT eq "JSON" and $Data::NestedKey::JSON_PRETTY 12 7 19
Condition66%2/3
$Data::NestedKey::FORMAT eq "JSON" and $Data::NestedKey::JSON_PRETTY
!ll&&!rl&&r
7012
MC/DC50%1/2
$Data::NestedKey::FORMAT eq "JSON" and $Data::NestedKey::JSON_PRETTY
$Data::NestedKey::FORMAT eq "JSON" $Data::NestedKey::JSON_PRETTY
Truth table66%2/3
A and B
A= $Data::NestedKey::FORMAT eq "JSON"B= $Data::NestedKey::JSON_PRETTY
ABresult
0X0
100
111
220 7 ▶ return JSON->new->encode( $self->{data} ) if $FORMAT eq 'JSON';
Branch: 1 branch
BranchTrueFalseTotal
if $Data::NestedKey::FORMAT eq "JSON" 0 7 7
221 7 ▶ return YAML::XS::Dump( $self->{data} ) if $FORMAT eq 'YML';
Branch: 1 branch
BranchTrueFalseTotal
if $Data::NestedKey::FORMAT eq "YML" 0 7 7
222 7 ▶ return YAML::XS::Dump( $self->{data} ) if $FORMAT eq 'YAML';
Branch: 1 branch
BranchTrueFalseTotal
if $Data::NestedKey::FORMAT eq "YAML" 3 4 7
223 4 ▶ return Dumper( $self->{data} ) if $FORMAT eq 'Dumper';
Branch: 1 branch
BranchTrueFalseTotal
if $Data::NestedKey::FORMAT eq "Dumper" 2 2 4
224 2 ▶ return nfreeze( $self->{data} ) if $FORMAT eq 'Storable';
Branch: 1 branch
BranchTrueFalseTotal
if $Data::NestedKey::FORMAT eq "Storable" 2 0 2
225  
226 0 croak "Unsupported format: $FORMAT";
227 }
228  
229 ########################################################################
230 13 ▶sub delete { ## no critic
Subroutine delete: called CC=28 SCAR=34.2
Pod documented
231 ########################################################################
232 13 my ( $self, @key_paths ) = @_;
233  
234 13 for my $key_path (@key_paths) {
235 13 my @segments = _parse_path($key_path);
236 13 ▶ next if !@segments;
Branch: 1 branch
BranchTrueFalseTotal
unless @segments 0 13 13
237  
238 13 my $current = $self->{data};
239 13 my @parents;
240 13 my $valid = 1;
241  
242 13 for my $seg ( @segments[ 0 .. $#segments - 1 ] ) {
243 13 ▶ if ( exists $seg->{key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seg->{"key"}) 12 1 13
244 12 ▶ if ( !_is_hash($current) || !exists $current->{ $seg->{key} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_hash($current) and exists $$current{$seg->{"key"}}) 2 10 12
Condition66%2/3
_is_hash($current) and exists $$current{$seg->{"key"}}
!ll&&!rl&&r
0210
MC/DC50%1/2
_is_hash($current) and exists $$current{$seg->{"key"}}
_is_hash($current) exists $$current{$seg->{"key"}}
Truth table66%2/3
A and B
A= _is_hash($current)B= exists $$current{$seg->{"key"}}
ABresult
0X0
100
111
245 2 $valid = 0;
246 2 last;
247 }
248  
249 10 push @parents, [ $current, $seg->{key} ];
250 10 $current = $current->{ $seg->{key} };
251 }
252  
253 11 ▶ if ( defined $seg->{index} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seg->{"index"}) 2 9 11
254 2 my $idx = $seg->{index};
255 2 ▶ if ( !_is_array($current)
Branch: 1 branch
BranchTrueFalseTotal
if (not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;} or not defined $current->[$idx]) 1 1 2
Condition66%6/9
not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;} or not defined $current->[$idx]
l!l&&r!l&&!r
101
not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;}
l!l&&r!l&&!r
101
not _is_array($current) or $idx >= @{$current;}
l!l&&r!l&&!r
011
MC/DC25%1/4
not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;} or not defined $current->[$idx]
not _is_array($current) $idx >= @{$current;} $idx < -@{$current;} not defined $current->[$idx]
Truth table40%2/5
not(A) or B or C or not(D)
A= not _is_array($current)B= $idx >= @{$current;}C= $idx < -@{$current;}D= not defined $current->[$idx]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
256 2 || $idx >= @{$current}
257 1 || $idx < -@{$current}
258 || !defined $current->[$idx] ) {
259 1 $valid = 0;
260 1 last;
261 }
262  
263 1 $current = $current->[$idx];
264 1 @parents = ();
265 }
266 }
267  
268 13 ▶ next if !$valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 3 10 13
269  
270 10 my $final = $segments[-1];
271 10 my $changed = 0;
272  
273 10 ▶ if ( exists $final->{key} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $final->{'key'}) { } 8 2 10
elsif (defined $final->{'index'}) { } 2 0 2
274 8 ▶ next if !_is_hash($current) || !exists $current->{ $final->{key} };
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hash($current) and exists $$current{$final->{"key"}} 0 8 8
Condition33%1/3
_is_hash($current) and exists $$current{$final->{"key"}}
!ll&&!rl&&r
008
MC/DC0%0/2
_is_hash($current) and exists $$current{$final->{"key"}}
_is_hash($current) exists $$current{$final->{"key"}}
Truth table33%1/3
A and B
A= _is_hash($current)B= exists $$current{$final->{"key"}}
ABresult
0X0
100
111
275  
276 8 ▶ if ( defined $final->{index} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $final->{'index'}) { } 4 4 8
277 4 my $array = $current->{ $final->{key} };
278 4 my $idx = $final->{index};
279 4 ▶ next if !_is_array($array) || $idx >= @{$array} || $idx < -@{$array};
Branch: 1 branch
BranchTrueFalseTotal
if not _is_array($array) or $idx >= @{$array;} or $idx < -@{$array;} 2 2 4
Condition83%5/6
not _is_array($array) or $idx >= @{$array;} or $idx < -@{$array;}
l!l&&r!l&&!r
112
not _is_array($array) or $idx >= @{$array;}
l!l&&r!l&&!r
013
MC/DC66%2/3
not _is_array($array) or $idx >= @{$array;} or $idx < -@{$array;}
not _is_array($array) $idx >= @{$array;} $idx < -@{$array;}
Truth table75%3/4
not(A) or B or C
A= not _is_array($array)B= $idx >= @{$array;}C= $idx < -@{$array;}
ABCresult
1XX1
01X1
0011
0000
280  
281 2 splice @{$array}, $idx, 1;
282 2 @parents = (); # Do not prune through an indexed array.
283 }
284 else {
285 4 delete $current->{ $final->{key} };
286 }
287 6 $changed = 1;
288 }
289 elsif ( defined $final->{index} ) {
290 2 my $idx = $final->{index};
291 2 ▶ next if !_is_array($current) || $idx >= @{$current} || $idx < -@{$current};
Branch: 1 branch
BranchTrueFalseTotal
if not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;} 0 2 2
Condition33%2/6
not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;}
l!l&&r!l&&!r
002
not _is_array($current) or $idx >= @{$current;}
l!l&&r!l&&!r
002
MC/DC0%0/3
not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;}
not _is_array($current) $idx >= @{$current;} $idx < -@{$current;}
Truth table25%1/4
not(A) or B or C
A= not _is_array($current)B= $idx >= @{$current;}C= $idx < -@{$current;}
ABCresult
1XX1
01X1
0011
0000
292  
293 2 splice @{$current}, $idx, 1;
294 2 @parents = ();
295 2 $changed = 1;
296 }
297  
298 8 ▶ next if !$changed;
Branch: 1 branch
BranchTrueFalseTotal
unless $changed 0 8 8
299  
300 8 ▶ while (@parents) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parents) 5 6 11
301 5 my ( $parent, $key ) = @{ pop @parents };
302 5 ▶ last if !_is_hash( $parent->{$key} ) || %{ $parent->{$key} };
Branch: 1 branch
BranchTrueFalseTotal
if not _is_hash($parent->{$key}) or %{$parent->{$key};} 2 3 5
Condition66%2/3
not _is_hash($parent->{$key}) or %{$parent->{$key};}
l!l&&r!l&&!r
023
MC/DC50%1/2
not _is_hash($parent->{$key}) or %{$parent->{$key};}
not _is_hash($parent->{$key}) %{$parent->{$key};}
Truth table66%2/3
not(A) or B
A= not _is_hash($parent->{$key})B= %{$parent->{$key};}
ABresult
1X1
011
000
303 3 delete $parent->{$key};
304 }
305 }
306  
307 13 return $self;
308 }
309  
310 ########################################################################
311 14 ▶sub exists_key {
Subroutine exists_key: called CC=13 SCAR=25.8
Pod documented
312 ########################################################################
313 14 my ( $self, @key_paths ) = @_;
314 14 my @results;
315  
316 14 for my $key_path (@key_paths) {
317 14 my @segments = _parse_path($key_path);
318 14 my $current = $self->{data};
319 14 ▶ my $found = @segments ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@segments ? : 14 0 14
320  
321 14 for my $seg (@segments) {
322 24 ▶ if ( exists $seg->{key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seg->{"key"}) 18 6 24
323 18 ▶ if ( !_is_hash($current) || !exists $current->{ $seg->{key} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_hash($current) and exists $$current{$seg->{"key"}}) 5 13 18
Condition66%2/3
_is_hash($current) and exists $$current{$seg->{"key"}}
!ll&&!rl&&r
0513
MC/DC50%1/2
_is_hash($current) and exists $$current{$seg->{"key"}}
_is_hash($current) exists $$current{$seg->{"key"}}
Truth table66%2/3
A and B
A= _is_hash($current)B= exists $$current{$seg->{"key"}}
ABresult
0X0
100
111
324 5 $found = 0;
325 5 last;
326 }
327 13 $current = $current->{ $seg->{key} };
328 }
329  
330 19 ▶ if ( defined $seg->{index} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seg->{"index"}) 10 9 19
331 10 my $idx = $seg->{index};
332 10 ▶ if ( !_is_array($current)
Branch: 1 branch
BranchTrueFalseTotal
if (not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;} or not defined $current->[$idx]) 3 7 10
Condition77%7/9
not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;} or not defined $current->[$idx]
l!l&&r!l&&!r
217
not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;}
l!l&&r!l&&!r
208
not _is_array($current) or $idx >= @{$current;}
l!l&&r!l&&!r
028
MC/DC50%2/4
not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;} or not defined $current->[$idx]
not _is_array($current) $idx >= @{$current;} $idx < -@{$current;} not defined $current->[$idx]
Truth table60%3/5
not(A) or B or C or not(D)
A= not _is_array($current)B= $idx >= @{$current;}C= $idx < -@{$current;}D= not defined $current->[$idx]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
333 10 || $idx >= @{$current}
334 8 || $idx < -@{$current}
335 || !defined $current->[$idx] ) {
336 3 $found = 0;
337 3 last;
338 }
339 7 $current = $current->[$idx];
340 }
341 }
342  
343 14 push @results, $found;
344 }
345  
346 14 ▶ return wantarray ? @results : $results[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 14 14
347 }
348  
349 9 1;
350  
351 __END__
« NestedKey/CLI.pm ↑ Summary