| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
_is_hash($init_data) or _is_array($init_data)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
_is_hash($init_data) or _is_array($init_data)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | @kv_list = ( $init_data, @kv_list ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 24 | ▶ | return $self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | if !@kv_list; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 3 | ▶ | croak 'Must provide key-value pairs' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
_is_array($self->{"data"}) and @kv_list
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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'
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'
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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'
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'
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 34 | my ( $key, $idx ) = ( $1, $2 + 0 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 34 | ▶ | if ( length $key ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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} //= {}
MC/DC100%1/1
$current->{$key} //= {}
$current->{$key}
Truth table100%2/2
A
A= $current->{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 20 | $current = $current->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 27 | my $final_key = $keys[-1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 27 | ▶ | if ( $action eq q{+} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 4 | ▶ | if ( _is_array( $current->{$final_key} ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
_is_hash($current->{$final_key}) and not _is_hash($value)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 55 | ▶ | if ( _is_hash($current) && exists $current->{$key} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
_is_hash($current) and exists $current->{$key}
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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 16 | ▶ | if ( _is_array($current) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 32 | ▶ | return wantarray ? @results : $results[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$Data::NestedKey::FORMAT eq "JSON" and $Data::NestedKey::JSON_PRETTY
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 7 | ▶ | return JSON->new->encode( $self->{data} ) if $FORMAT eq 'JSON'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 7 | ▶ | return YAML::XS::Dump( $self->{data} ) if $FORMAT eq 'YML'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 7 | ▶ | return YAML::XS::Dump( $self->{data} ) if $FORMAT eq 'YAML'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 4 | ▶ | return Dumper( $self->{data} ) if $FORMAT eq 'Dumper'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 2 | ▶ | return nfreeze( $self->{data} ) if $FORMAT eq 'Storable'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 12 | ▶ | if ( !_is_hash($current) || !exists $current->{ $seg->{key} } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
_is_hash($current) and exists $$current{$seg->{"key"}}
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"}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 2 | my $idx = $seg->{index}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 2 | ▶ | if ( !_is_array($current) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;} or not defined $current->[$idx]
not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;}
not _is_array($current) or $idx >= @{$current;}
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 10 | my $final = $segments[-1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 10 | my $changed = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 10 | ▶ | if ( exists $final->{key} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 8 | ▶ | next if !_is_hash($current) || !exists $current->{ $final->{key} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
_is_hash($current) and exists $$current{$final->{"key"}}
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"}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 8 | ▶ | if ( defined $final->{index} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
not _is_array($array) or $idx >= @{$array;} or $idx < -@{$array;}
not _is_array($array) or $idx >= @{$array;}
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;}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;}
not _is_array($current) or $idx >= @{$current;}
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;}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 2 | splice @{$current}, $idx, 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 2 | @parents = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 2 | $changed = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 8 | ▶ | next if !$changed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 8 | ▶ | while (@parents) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 5 | my ( $parent, $key ) = @{ pop @parents }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 5 | ▶ | last if !_is_hash( $parent->{$key} ) || %{ $parent->{$key} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not _is_hash($parent->{$key}) or %{$parent->{$key};}
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};}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 14 | for my $seg (@segments) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 24 | ▶ | if ( exists $seg->{key} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 18 | ▶ | if ( !_is_hash($current) || !exists $current->{ $seg->{key} } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
_is_hash($current) and exists $$current{$seg->{"key"}}
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"}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 10 | my $idx = $seg->{index}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 10 | ▶ | if ( !_is_array($current) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition77%7/9
not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;} or not defined $current->[$idx]
not _is_array($current) or $idx >= @{$current;} or $idx < -@{$current;}
not _is_array($current) or $idx >= @{$current;}
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||