blib/lib/Hash/Digger.pm

filter: Statementstmt 97.8% 45 / 46 Branchbran 91.6% 11 / 12 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 94.0% 79 / 84 CC 9 SCAR 13.3
CC
9
Cov
95%
_traverse_hash
8.1
exhume
2.0
BEGIN
1.0
CRAP 3.8
↑ Summary
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
l!l&&r!l&&!r
213
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless defined $root 1 12 13
112 12 ▶ croak E_NO_ROOT_HASH if ! _is_hash_reference($root);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hash_reference($root) 1 11 12
113 11 ▶ croak E_NO_PATH if @path == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @path == 0 1 10 11
114  
115 10 ▶ while (my $element = shift @path) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $element = shift @path) 28 5 33
116 28 ▶ if (!exists $root->{$element}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $root->{$element}) 5 23 28
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
BranchTrueFalseTotal
if (not _is_hash_reference($root) and @path > 0) 0 23 23
Condition66%2/3
not _is_hash_reference($root) and @path > 0
!ll&&!rl&&r
1940
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
ABresult
0X0
100
111
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
↑ Summary