| 1 | package Data::DeepAccess; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Sentinel 'sentinel'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | our @EXPORT_OK = qw(deep_exists deep_get deep_set deep_val); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 19 | ▶ | sub deep_exists { | ||||||||||||||||||||||||||
|
Subroutine
deep_exists: called CC=27 SCAR=38.2
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 19 | ▶ | croak 'deep_exists called with no arguments' unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 19 | my ($parent, @keys) = @_; | |||||||||||||||||||||||||||
| 17 | 19 | ▶ | return !!1 unless @keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 18 | foreach my $key_i (0..$#keys) { | |||||||||||||||||||||||||||
| 19 | 38 | ▶ | return !!0 unless defined $parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 36 | my $reftype = ref $parent; | |||||||||||||||||||||||||||
| 21 | 36 | my $blessed = blessed $parent; | |||||||||||||||||||||||||||
| 22 | 36 | ▶ | my $type = defined $blessed ? 'method' : lc $reftype; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 36 | ▶ | my $type_str = length $reftype ? "$reftype ref" : 'scalar value'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 36 | my $key = $keys[$key_i]; | |||||||||||||||||||||||||||
| 25 | 36 | my $lvalue; | |||||||||||||||||||||||||||
| 26 | 36 | ▶ | if (ref $key eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | if (exists $key->{key}) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 28 | 0 | ($type, $key) = ('hash', $key->{key}); | |||||||||||||||||||||||||||
| 29 | } elsif (exists $key->{index}) { | ||||||||||||||||||||||||||||
| 30 | 0 | ($type, $key) = ('array', $key->{index}); | |||||||||||||||||||||||||||
| 31 | } elsif (exists $key->{method}) { | ||||||||||||||||||||||||||||
| 32 | 0 | ($type, $key) = ('method', $key->{method}); | |||||||||||||||||||||||||||
| 33 | } elsif (exists $key->{lvalue}) { | ||||||||||||||||||||||||||||
| 34 | 0 | ($type, $key, $lvalue) = ('method', $key->{lvalue}, 1); | |||||||||||||||||||||||||||
| 35 | } else { | ||||||||||||||||||||||||||||
| 36 | 0 | croak q{Traversal key hashref must contain "key", "index", "method", or "lvalue"}; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 36 | ▶ | if ($type eq 'hash') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 40 | 22 | ▶ | croak qq{Can't dereference $type_str as HASH to access key "$key"} if !defined $blessed and $reftype ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $blessed and $reftype ne "HASH"
MC/DC0%0/2
not defined $blessed and $reftype ne "HASH"
not defined $blessed
$reftype ne "HASH"
Truth table33%1/3
not(A) and B
A= not defined $blessedB= $reftype ne "HASH"
| |||||||||||||||||||||||||||||
| 41 | 22 | ▶ | return !!0 unless exists $parent->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 19 | ▶ | return !!1 if $key_i == $#keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 14 | $parent = $parent->{$key}; | |||||||||||||||||||||||||||
| 44 | } elsif ($type eq 'array') { | ||||||||||||||||||||||||||||
| 45 | 8 | ▶ | croak qq{Can't dereference $type_str as ARRAY to access index $key} if !defined $blessed and $reftype ne 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $blessed and $reftype ne "ARRAY"
MC/DC0%0/2
not defined $blessed and $reftype ne "ARRAY"
not defined $blessed
$reftype ne "ARRAY"
Truth table33%1/3
not(A) and B
A= not defined $blessedB= $reftype ne "ARRAY"
| |||||||||||||||||||||||||||||
| 46 | 8 | ▶ | return !!0 unless exists $parent->[$key]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 7 | ▶ | return !!1 if $key_i == $#keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 5 | $parent = $parent->[$key]; | |||||||||||||||||||||||||||
| 49 | } elsif ($type eq 'method') { | ||||||||||||||||||||||||||||
| 50 | 3 | ▶ | croak qq{Can't call method "$key" on unblessed reference} if length $reftype and !defined $blessed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $reftype and not defined $blessed
MC/DC0%0/2
length $reftype and not defined $blessed
length $reftype
not defined $blessed
Truth table33%1/3
A and not(B)
A= length $reftypeB= not defined $blessed
| |||||||||||||||||||||||||||||
| 51 | 3 | ▶ | return !!0 unless my $sub = $parent->can($key); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 2 | ▶ | return !!1 if $key_i == $#keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 1 | $parent = $parent->$sub; | |||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||
| 55 | 3 | croak qq{Can't traverse $type_str with key "$key"}; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 48 | ▶ | sub deep_get { | ||||||||||||||||||||||||||
|
Subroutine
deep_get: called CC=26 SCAR=34.7
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 48 | my ($parent, @keys) = @_; | |||||||||||||||||||||||||||
| 62 | 48 | ▶ | return $parent unless @keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 45 | foreach my $key_i (0..$#keys) { | |||||||||||||||||||||||||||
| 64 | 94 | ▶ | return undef unless defined $parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 90 | my $reftype = ref $parent; | |||||||||||||||||||||||||||
| 66 | 90 | my $blessed = blessed $parent; | |||||||||||||||||||||||||||
| 67 | 90 | ▶ | my $type = defined $blessed ? 'method' : lc $reftype; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 90 | ▶ | my $type_str = length $reftype ? "$reftype ref" : 'scalar value'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 90 | my $key = $keys[$key_i]; | |||||||||||||||||||||||||||
| 70 | 90 | my $lvalue; | |||||||||||||||||||||||||||
| 71 | 90 | ▶ | if (ref $key eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 4 | ▶ | if (exists $key->{key}) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 73 | 1 | ($type, $key) = ('hash', $key->{key}); | |||||||||||||||||||||||||||
| 74 | } elsif (exists $key->{index}) { | ||||||||||||||||||||||||||||
| 75 | 3 | ($type, $key) = ('array', $key->{index}); | |||||||||||||||||||||||||||
| 76 | } elsif (exists $key->{method}) { | ||||||||||||||||||||||||||||
| 77 | 0 | ($type, $key) = ('method', $key->{method}); | |||||||||||||||||||||||||||
| 78 | } elsif (exists $key->{lvalue}) { | ||||||||||||||||||||||||||||
| 79 | 0 | ($type, $key, $lvalue) = ('method', $key->{lvalue}, 1); | |||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||
| 81 | 0 | croak q{Traversal key hashref must contain "key", "index", "method", or "lvalue"}; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 90 | ▶ | if ($type eq 'hash') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 85 | 55 | ▶ | croak qq{Can't dereference $type_str as HASH to access key "$key"} if !defined $blessed and $reftype ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $blessed and $reftype ne "HASH"
MC/DC0%0/2
not defined $blessed and $reftype ne "HASH"
not defined $blessed
$reftype ne "HASH"
Truth table33%1/3
not(A) and B
A= not defined $blessedB= $reftype ne "HASH"
| |||||||||||||||||||||||||||||
| 86 | 55 | ▶ | return undef unless exists $parent->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 49 | ▶ | return $parent->{$key} if $key_i == $#keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 33 | $parent = $parent->{$key}; | |||||||||||||||||||||||||||
| 89 | } elsif ($type eq 'array') { | ||||||||||||||||||||||||||||
| 90 | 21 | ▶ | croak qq{Can't dereference $type_str as ARRAY to access index $key} if !defined $blessed and $reftype ne 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $blessed and $reftype ne "ARRAY"
MC/DC0%0/2
not defined $blessed and $reftype ne "ARRAY"
not defined $blessed
$reftype ne "ARRAY"
Truth table33%1/3
not(A) and B
A= not defined $blessedB= $reftype ne "ARRAY"
| |||||||||||||||||||||||||||||
| 91 | 21 | ▶ | return undef unless exists $parent->[$key]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 19 | ▶ | return $parent->[$key] if $key_i == $#keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 13 | $parent = $parent->[$key]; | |||||||||||||||||||||||||||
| 94 | } elsif ($type eq 'method') { | ||||||||||||||||||||||||||||
| 95 | 8 | ▶ | croak qq{Can't call method "$key" on unblessed reference} if length $reftype and !defined $blessed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $reftype and not defined $blessed
MC/DC0%0/2
length $reftype and not defined $blessed
length $reftype
not defined $blessed
Truth table33%1/3
A and not(B)
A= length $reftypeB= not defined $blessed
| |||||||||||||||||||||||||||||
| 96 | 8 | ▶ | return undef unless my $sub = $parent->can($key); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 6 | ▶ | return $parent->$sub if $key_i == $#keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 3 | $parent = $parent->$sub; | |||||||||||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||||||||||
| 100 | 6 | croak qq{Can't traverse $type_str with key "$key"}; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 27 | ▶ | sub deep_set { | ||||||||||||||||||||||||||
|
Subroutine
deep_set: called CC=32 SCAR=36.4
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 27 | ▶ | croak 'deep_set called with no arguments' unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 27 | ▶ | croak 'deep_set requires a value to set' unless @_ > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 27 | my $parent_ref = \$_[0]; | |||||||||||||||||||||||||||
| 109 | 27 | my @keys = @_[1..$#_-1]; | |||||||||||||||||||||||||||
| 110 | 27 | my $value = $_[-1]; | |||||||||||||||||||||||||||
| 111 | 27 | ▶ | return $$parent_ref = $value unless @keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 25 | foreach my $key_i (0..$#keys) { | |||||||||||||||||||||||||||
| 113 | 46 | my $reftype = ref $$parent_ref; | |||||||||||||||||||||||||||
| 114 | 46 | my $blessed = blessed $$parent_ref; | |||||||||||||||||||||||||||
| 115 | 46 | ▶ | my $type = defined $blessed ? 'method' : lc $reftype; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 46 | ▶ | my $type_str = length $reftype ? "$reftype ref" : 'scalar value'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 46 | ▶ | $type = 'hash' if !length $type and !defined $$parent_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $type or defined $$parent_ref
MC/DC50%1/2
length $type or defined $$parent_ref
length $type
defined $$parent_ref
Truth table66%2/3
A or B
A= length $typeB= defined $$parent_ref
| |||||||||||||||||||||||||||||
| 118 | 46 | my $key = $keys[$key_i]; | |||||||||||||||||||||||||||
| 119 | 46 | my $lvalue; | |||||||||||||||||||||||||||
| 120 | 46 | ▶ | if (ref $key eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 13 | ▶ | if (exists $key->{key}) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 122 | 2 | ($type, $key) = ('hash', $key->{key}); | |||||||||||||||||||||||||||
| 123 | } elsif (exists $key->{index}) { | ||||||||||||||||||||||||||||
| 124 | 7 | ($type, $key) = ('array', $key->{index}); | |||||||||||||||||||||||||||
| 125 | } elsif (exists $key->{method}) { | ||||||||||||||||||||||||||||
| 126 | 2 | ($type, $key) = ('method', $key->{method}); | |||||||||||||||||||||||||||
| 127 | } elsif (exists $key->{lvalue}) { | ||||||||||||||||||||||||||||
| 128 | 2 | ($type, $key, $lvalue) = ('method', $key->{lvalue}, 1); | |||||||||||||||||||||||||||
| 129 | } else { | ||||||||||||||||||||||||||||
| 130 | 0 | croak q{Traversal key hashref must contain "key", "index", "method", or "lvalue"}; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 46 | ▶ | if ($type eq 'hash') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 134 | 24 | ▶ | if (defined $$parent_ref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 14 | ▶ | croak qq{Can't dereference $type_str as HASH to access key "$key"} if !defined $blessed and $reftype ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $blessed and $reftype ne "HASH"
MC/DC0%0/2
not defined $blessed and $reftype ne "HASH"
not defined $blessed
$reftype ne "HASH"
Truth table33%1/3
not(A) and B
A= not defined $blessedB= $reftype ne "HASH"
| |||||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||
| 137 | 10 | $$parent_ref = {}; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 24 | ▶ | return $$parent_ref->{$key} = $value if $key_i == $#keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 12 | $parent_ref = \$$parent_ref->{$key}; | |||||||||||||||||||||||||||
| 141 | } elsif ($type eq 'array') { | ||||||||||||||||||||||||||||
| 142 | 11 | ▶ | if (defined $$parent_ref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 6 | ▶ | croak qq{Can't dereference $type_str as ARRAY to access index $key} if !defined $blessed and $reftype ne 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $blessed and $reftype ne "ARRAY"
MC/DC0%0/2
not defined $blessed and $reftype ne "ARRAY"
not defined $blessed
$reftype ne "ARRAY"
Truth table33%1/3
not(A) and B
A= not defined $blessedB= $reftype ne "ARRAY"
| |||||||||||||||||||||||||||||
| 144 | } else { | ||||||||||||||||||||||||||||
| 145 | 5 | $$parent_ref = []; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 11 | ▶ | return $$parent_ref->[$key] = $value if $key_i == $#keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 6 | $parent_ref = \$$parent_ref->[$key]; | |||||||||||||||||||||||||||
| 149 | } elsif ($type eq 'method') { | ||||||||||||||||||||||||||||
| 150 | 11 | ▶ | croak qq{Can't call method "$key" on an undefined value} unless defined $$parent_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 7 | ▶ | croak qq{Can't call method "$key" on unblessed reference} if !defined $blessed and length $reftype; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $blessed and length $reftype
MC/DC0%0/2
not defined $blessed and length $reftype
not defined $blessed
length $reftype
Truth table33%1/3
not(A) and B
A= not defined $blessedB= length $reftype
| |||||||||||||||||||||||||||||
| 152 | 7 | ▶ | my $package = length $reftype ? $reftype : $$parent_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 7 | ▶ | croak qq{Can't locate object method "$key" via package "$package"} unless my $sub = $$parent_ref->can($key); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 7 | ▶ | if ($key_i == $#keys) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 4 | ▶ | $lvalue ? $$parent_ref->$sub = $value : $$parent_ref->$sub($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 4 | return $value; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 3 | $parent_ref = \$$parent_ref->$sub; | |||||||||||||||||||||||||||
| 159 | } else { | ||||||||||||||||||||||||||||
| 160 | 0 | croak qq{Can't traverse $type_str with key "$key"}; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 31 | ▶ | sub deep_val :lvalue { | ||||||||||||||||||||||||||
|
Subroutine
deep_val: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 31 | ▶ | croak 'deep_val called with no arguments' unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 31 | my $parent_ref = \$_[0]; | |||||||||||||||||||||||||||
| 168 | 31 | my @keys = @_[1..$#_]; | |||||||||||||||||||||||||||
| 169 | 29 | ▶ | sentinel get => sub { deep_get $$parent_ref, @keys }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 170 | 31 | ▶ | set => sub { deep_set $$parent_ref, @keys, $_[0] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 1 | 1; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 NAME | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Data::DeepAccess - Access or set data in deep structures | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | use Data::DeepAccess qw(deep_exists deep_get deep_set); | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | my %things; | ||||||||||||||||||||||||||||
| 184 | deep_set(\%things, qw(foo bar), 42); | ||||||||||||||||||||||||||||
| 185 | say $things{foo}{bar}; # 42 | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | $things{foo}{baz} = ['a'..'z']; | ||||||||||||||||||||||||||||
| 188 | say deep_get(\%things, qw(foo baz 5)); # f | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | deep_set(\%things, qw(foo foo), undef); | ||||||||||||||||||||||||||||
| 191 | say deep_exists(\%things, qw(foo foo)); # 1 | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | deep_val(\%things, qw(bar bar)) = 'lvalue'; | ||||||||||||||||||||||||||||
| 194 | say deep_val(\%things, qw(bar bar)); # lvalue | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | Provides the functions L</"deep_exists">, L</"deep_get">, L</"deep_set">, and | ||||||||||||||||||||||||||||
| 199 | L</"deep_val"> that traverse nested data structures to retrieve or set the | ||||||||||||||||||||||||||||
| 200 | value located by a list of keys. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | When traversing, keys are applied according to the type of referenced data | ||||||||||||||||||||||||||||
| 203 | structure. A hash will be traversed by hash key, an array by array index, and | ||||||||||||||||||||||||||||
| 204 | an object by method call (scalar context with no arguments). If the data | ||||||||||||||||||||||||||||
| 205 | structure is not defined, it will be traversed as a hash by default (but not | ||||||||||||||||||||||||||||
| 206 | vivified unless in a set operation). | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | You can override how a key is applied, and thus what type of structure is | ||||||||||||||||||||||||||||
| 209 | vivified if necessary, by passing the key in a hashref as the value of C<key> | ||||||||||||||||||||||||||||
| 210 | (hash) or C<index> (array). | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | deep_set(my $structure, 'foo', 42); # {foo => 42} | ||||||||||||||||||||||||||||
| 213 | deep_set(my $structure, {index => 1}, 42); # [undef, 42] | ||||||||||||||||||||||||||||
| 214 | deep_set($object, {key => 'foo'}, 42); # sets $object->{foo} directly | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | For the rare case it's needed, you can also use one of the keys C<method> or | ||||||||||||||||||||||||||||
| 217 | C<lvalue>. | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | deep_set($object, {method => 'foo'}, 42); # $object->foo(42) | ||||||||||||||||||||||||||||
| 220 | deep_set($object, {lvalue => 'foo'}, 42); # $object->foo = 42 | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | Attempting to traverse intermediate structures that are defined and not a | ||||||||||||||||||||||||||||
| 223 | reference to a hash, array, or object will result in an exception. | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | If an object method call is the last key in a set operation or the next | ||||||||||||||||||||||||||||
| 226 | structure must be vivified, the method will be called passing the new value as | ||||||||||||||||||||||||||||
| 227 | an argument or assigned if it is treated as an lvalue. Attempting to call a | ||||||||||||||||||||||||||||
| 228 | method on an undefined value or unblessed ref in a set operation will result in | ||||||||||||||||||||||||||||
| 229 | an exception. | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | Currently, undefined results from non-lvalue method calls are not vivified back | ||||||||||||||||||||||||||||
| 232 | to the object to support setting a further nested value. This may be supported | ||||||||||||||||||||||||||||
| 233 | in the future. | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | All functions are exported individually. | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =head2 deep_exists | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | my $bool = deep_exists($structure, @keys); | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | Returns a true value if the value exists in the referenced structure located by | ||||||||||||||||||||||||||||
| 244 | the given keys. No intermediate structures will be altered or vivified; a | ||||||||||||||||||||||||||||
| 245 | missing structure will result in a false return value. | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | Array indexes are tested for existence with L<perlfunc/exists>, like hash | ||||||||||||||||||||||||||||
| 248 | keys, which may have surprising results in sparse arrays. Avoid this situation. | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | Object methods are tested for existence with C<< $object->can($method) >>. | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | If no keys are passed, returns true. | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =head2 deep_get | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | my $value = deep_get($structure, @keys); | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | Retrieves the value from the referenced structure located by the given keys. No | ||||||||||||||||||||||||||||
| 259 | intermediate structures will be altered or vivified; a missing structure will | ||||||||||||||||||||||||||||
| 260 | result in C<undef>. | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | If no keys are passed, returns the original structure. | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | =head2 deep_set | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | $new_value = deep_set($structure, @keys, $new_value); | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | Sets the value in the referenced structure located by the given keys. Missing | ||||||||||||||||||||||||||||
| 269 | intermediate structures will be vivified to hashrefs by default. If the | ||||||||||||||||||||||||||||
| 270 | structure is undefined, it must be an assignable lvalue to be vivified. | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | If no keys are passed, the structure must be an assignable lvalue and will be | ||||||||||||||||||||||||||||
| 273 | assigned the value directly. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =head2 deep_val | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | my $value = deep_val($structure, @keys); | ||||||||||||||||||||||||||||
| 278 | deep_val($structure, @keys) = $new_value; | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | L<Lvalue|perlsub/"Lvalue subroutines"> accessor that is equivalent to | ||||||||||||||||||||||||||||
| 281 | L</"deep_get"> when a value is retrieved from it, or L</"deep_set"> when a | ||||||||||||||||||||||||||||
| 282 | value is assigned to it (passing the assigned value as the final argument). | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | The passed keys are used to traverse the structure at the time of retrieval or | ||||||||||||||||||||||||||||
| 285 | assignment, not when the function is called. This subtle difference may matter | ||||||||||||||||||||||||||||
| 286 | if the lvalue is preserved for later access, such as via reference. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =head1 BUGS | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | Report any issues on the public bugtracker. | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | Dan Book <dbook@cpan.org> | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =head1 CONTRIBUTORS | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | =over | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =item Matt S Trout (mst) | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =back | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | This software is Copyright (c) 2019 by Dan Book. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | Many prior modules for accessing specific items within a deep structure, which | ||||||||||||||||||||||||||||
| 315 | the author determined were either insufficient or overcomplex. | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | =over | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =item * L<Hash::DeepAccess> | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | =item * L<Data::Deep> | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =item * L<Data::Diver> | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | =item * L<Data::DPath> | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =item * L<JSON::Pointer> | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | =back | ||||||||||||||||||||||||||||