| 1 | package Hash::Digger; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | Hash::Digger - Access nested hash structures without vivification | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Version 0.0.2 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | our $VERSION = '0.0.2'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Allows accessing hash structures without triggering autovivification. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | my %hash; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | $hash{'foo'}{'bar'} = 'baz'; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | diggable \%hash, 'foo', 'bar'; | ||||||||||||||||||||||||||||
| 29 | # Truthy | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | diggable \%hash, 'xxx', 'yyy'; | ||||||||||||||||||||||||||||
| 32 | # Falsey | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | dig \%hash, 'foo', 'bar'; | ||||||||||||||||||||||||||||
| 35 | # 'baz' | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | dig \%hash, 'foo', 'bar', 'xxx'; | ||||||||||||||||||||||||||||
| 38 | # undef | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | exhume 'some default', \%hash, 'foo', 'bar'; | ||||||||||||||||||||||||||||
| 41 | # 'baz' | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | exhume 'some default', \%hash, 'foo', 'xxx'; | ||||||||||||||||||||||||||||
| 44 | # 'some default' | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Hash structure has not changed: | ||||||||||||||||||||||||||||
| 47 | use Data::Dumper; | ||||||||||||||||||||||||||||
| 48 | Dumper \%hash; | ||||||||||||||||||||||||||||
| 49 | # $VAR1 = { | ||||||||||||||||||||||||||||
| 50 | # 'foo' => { | ||||||||||||||||||||||||||||
| 51 | # 'bar' => 'baz' | ||||||||||||||||||||||||||||
| 52 | # } | ||||||||||||||||||||||||||||
| 53 | # }; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head1 EXPORT | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | dig, diggable, exhume | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 2 | our @EXPORT_OK = qw(dig diggable exhume); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head2 diggable | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | Check if given path is diggable on the hash (`exists` equivalent) | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub diggable { | ||||||||||||||||||||||||||||
| 73 | 4 | ▶ | my ($root, @path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
diggable: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 4 | return (_traverse_hash($root, @path))[1]; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head2 dig | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | Dig the hash and return the value. If the path is not valid, it returns undef. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub dig { | ||||||||||||||||||||||||||||
| 84 | 8 | ▶ | my ($root, @path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
dig: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 8 | return exhume(undef, $root, @path); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head2 exhume | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Dig the hash and return the value. If the path is not valid, it returns a default value. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =cut | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub exhume { | ||||||||||||||||||||||||||||
| 95 | 9 | ▶ | my ($default, $root, @path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
exhume: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 9 | ▶ | return (_traverse_hash($root, @path))[0] // $default; | ||||||||||||||||||||||||||
|
Condition100%3/3
(_traverse_hash($root, @path))[0] // $default
MC/DC100%2/2
(_traverse_hash($root, @path))[0] // $default
(_traverse_hash($root, @path))[0]
$default
Truth table100%3/3
A // B
A= (_traverse_hash($root, @path))[0]B= $default
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | ## no critic (ValuesAndExpressions::ProhibitConstantPragma) | ||||||||||||||||||||||||||||
| 100 | 2 | ▶ | use constant E_NO_ROOT => 'Root node is undefined'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 101 | 2 | ▶ | use constant E_NO_ROOT_HASH => 'Root node is not a hash reference'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 102 | 2 | ▶ | use constant E_NO_PATH => 'No path to exhume'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # Traverse hash for the given path and return the data. | ||||||||||||||||||||||||||||
| 105 | # Last item could be `undef` as in `$hash{'foo'}{'bar'} = undef`, | ||||||||||||||||||||||||||||
| 106 | # so we also need to return if the element exists or not | ||||||||||||||||||||||||||||
| 107 | sub _traverse_hash { | ||||||||||||||||||||||||||||
| 108 | 13 | ▶ | my ($root, @path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_traverse_hash: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||
| 109 | 13 | my $exists = 0; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 13 | ▶ | croak E_NO_ROOT if ! defined $root; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 12 | ▶ | croak E_NO_ROOT_HASH if ! _is_hash_reference($root); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 11 | ▶ | croak E_NO_PATH if @path == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 10 | ▶ | while (my $element = shift @path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 28 | ▶ | if (!exists $root->{$element}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 5 | return (undef, q()); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 23 | $root = $root->{$element}; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 23 | ▶ | if (!_is_hash_reference($root) && @path > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not _is_hash_reference($root) and @path > 0
MC/DC0%0/2
not _is_hash_reference($root) and @path > 0
not _is_hash_reference($root)
@path > 0
Truth table66%2/3
not(A) and B
A= not _is_hash_reference($root)B= @path > 0
| |||||||||||||||||||||||||||||
| 123 | 0 | return (undef, q()); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 5 | return ($root, 1); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | sub _is_hash_reference { | ||||||||||||||||||||||||||||
| 131 | 35 | ▶ | my $item = shift; | ||||||||||||||||||||||||||
|
Subroutine
_is_hash_reference: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 132 | 35 | return ref $item eq ref {}; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | L<https://github.com/juliodcs/Hash-Digger> | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Julio de Castro, C<< <julio.dcs at gmail.com> >> | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head1 BUGS | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Please report any bugs or feature requests to C<bug-hash-digger at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 146 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hash-Digger>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 147 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | perldoc Hash::Digger | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | You can also look for information at: | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =over 4 | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Hash-Digger> | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | L<https://cpanratings.perl.org/d/Hash-Digger> | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | L<https://metacpan.org/release/Hash-Digger> | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =back | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | This software is Copyright (c) 2020 by Julio de Castro. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =cut | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 2 | 1; # End of Hash::Digger | |||||||||||||||||||||||||||