blib/lib/Hash/Digger.pm

filter: Statementstmt 97.8% 46 / 47 Branchbran 92.8% 13 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 93.9% 77 / 82 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.3
15  
16 =cut
17  
18 2 our $VERSION = '0.0.3';
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 my $value = ( _traverse_hash( $root, @path ) )[0];
97 6 ▶ return defined $value ? $value : $default;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 2 4 6
98 }
99  
100 ## no critic (ValuesAndExpressions::ProhibitConstantPragma)
101 2 ▶use constant E_NO_ROOT => 'Root node is undefined';
Subroutine BEGIN: called CC=1 SCAR=0.0
102 2 ▶use constant E_NO_ROOT_HASH => 'Root node is not a hash reference';
Subroutine BEGIN: called CC=1 SCAR=0.0
103 2 ▶use constant E_NO_PATH => 'No path to exhume';
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 # Traverse hash for the given path and return the data.
106 # Last item could be `undef` as in `$hash{'foo'}{'bar'} = undef`,
107 # so we also need to return if the element exists or not
108 sub _traverse_hash {
109 13 ▶ my ( $root, @path ) = @_;
Subroutine _traverse_hash: called CC=8 SCAR=20.9
110 13 my $exists = 0;
111  
112 13 ▶ croak E_NO_ROOT if !defined $root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root 1 12 13
113 12 ▶ croak E_NO_ROOT_HASH if !_is_hash_reference($root);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hash_reference($root) 1 11 12
114 11 ▶ croak E_NO_PATH if @path == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @path == 0 1 10 11
115  
116 10 ▶ while ( my $element = shift @path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $element = shift @path) 28 5 33
117 28 ▶ if ( !exists $root->{$element} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $root->{$element}) 5 23 28
118 5 return ( undef, q() );
119 }
120  
121 23 $root = $root->{$element};
122  
123 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
124 0 return ( undef, q() );
125 }
126 }
127  
128 5 return ( $root, 1 );
129 }
130  
131 sub _is_hash_reference {
132 35 ▶ my $item = shift;
Subroutine _is_hash_reference: called CC=1 SCAR=0.0
133 35 return ref $item eq ref {};
134 }
135  
136 =head1 REPOSITORY
137  
138 L<https://github.com/juliodcs/Hash-Digger>
139  
140 =head1 AUTHOR
141  
142 Julio de Castro, C<< <julio.dcs at gmail.com> >>
143  
144 =head1 BUGS
145  
146 Please report any bugs or feature requests to C<bug-hash-digger at rt.cpan.org>, or through
147 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hash-Digger>. I will be notified, and then you'll
148 automatically be notified of progress on your bug as I make changes.
149  
150 =head1 SUPPORT
151  
152 You can find documentation for this module with the perldoc command.
153  
154 perldoc Hash::Digger
155  
156  
157 You can also look for information at:
158  
159 =over 4
160  
161 =item * RT: CPAN's request tracker (report bugs here)
162  
163 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Hash-Digger>
164  
165 =item * CPAN Ratings
166  
167 L<https://cpanratings.perl.org/d/Hash-Digger>
168  
169 =item * Search CPAN
170  
171 L<https://metacpan.org/release/Hash-Digger>
172  
173 =back
174  
175 =head1 ACKNOWLEDGEMENTS
176  
177  
178 =head1 LICENSE AND COPYRIGHT
179  
180 This software is Copyright (c) 2020 by Julio de Castro.
181  
182 This is free software, licensed under:
183  
184 The Artistic License 2.0 (GPL Compatible)
185  
186  
187 =cut
188  
189 2 1; # End of Hash::Digger
↑ Summary