File Coverage

blib/lib/Hash/DeepAccess.pm
Criterion Covered Total %
statement 18 30 60.0
branch 0 6 0.0
condition 0 3 0.0
subroutine 6 7 85.7
pod 1 1 100.0
total 25 47 53.1


line stmt bran cond sub pod time code
1             # vim:expandtab tabstop=4 shiftwidth=4
2              
3             package Hash::DeepAccess;
4              
5 1     1   23546 use 5.006;
  1         4  
  1         39  
6 1     1   5 use strict;
  1         10  
  1         45  
7 1     1   6 use warnings FATAL => 'all';
  1         7  
  1         49  
8              
9 1     1   6 use Carp;
  1         2  
  1         81  
10 1     1   7 use Exporter 'import';
  1         1  
  1         33  
11 1     1   861 use Want;
  1         20136  
  1         412  
12              
13             our @EXPORT = qw( deep );
14              
15             =head1 NAME
16              
17             Hash::DeepAccess - The great new Hash::DeepAccess!
18              
19             =head1 VERSION
20              
21             Version 0.01
22              
23             =cut
24              
25             our $VERSION = '0.01';
26              
27              
28             =head1 SYNOPSIS
29              
30             Allows retrieving and changing values in a nested hash structure.
31              
32             use Hash::DeepAccess;
33              
34             my $hash = {
35             a => {
36             b => {
37             c => {
38             d => 5
39             },
40             },
41             },
42             };
43              
44             my $five = deep($hash, qw( a b c d ));
45              
46             deep($hash, qw( a b c d )) = 10;
47              
48             =head1 EXPORT
49              
50             =head2 deep(HASH, PATH, ...)
51              
52             Retrieve the value determined by the path elements in the given hash. It's an
53             lvalue function, so values can be assigned to it to insert elements deep into
54             hashes. The function tries to be smart about this and does not create empty
55             hashes for non-existent paths unless a value is actually assigned. However, if
56             a value is assigned and elements in the path reference non-hash values, those
57             are overwritten with hashes to create the requested structure.
58              
59             =cut
60              
61             sub deep : lvalue {
62 0     0 1   my ($hash, @path) = @_;
63              
64 0           my $lvalue = want(qw( LVALUE ASSIGN ));
65              
66 0           my $last = pop @path;
67              
68 0           while(@path) {
69 0           my $node = shift @path;
70              
71 0 0         croak("Expected a hash") if ref($hash) ne 'HASH';
72              
73 0 0 0       if(!defined($hash->{$node}) || ref($hash->{$node}) ne 'HASH') {
74 0 0         if($lvalue) {
75 0           $hash->{$node} = {};
76             }
77             else {
78 0           return undef;
79             }
80             }
81              
82 0           $hash = $hash->{$node};
83             }
84              
85 0           return $hash->{$last};
86             }
87              
88             =head1 AUTHOR
89              
90             Jonas Kramer, C<< >>
91              
92             =head1 BUGS
93              
94             Please report any bugs or feature requests to C, or through
95             the web interface at L. I will be notified, and then you'll
96             automatically be notified of progress on your bug as I make changes.
97              
98              
99              
100              
101             =head1 SUPPORT
102              
103             You can find documentation for this module with the perldoc command.
104              
105             perldoc Hash::DeepAccess
106              
107              
108             You can also look for information at:
109              
110             =over 4
111              
112             =item * RT: CPAN's request tracker (report bugs here)
113              
114             L
115              
116             =item * AnnoCPAN: Annotated CPAN documentation
117              
118             L
119              
120             =item * CPAN Ratings
121              
122             L
123              
124             =item * Search CPAN
125              
126             L
127              
128             =back
129              
130              
131             =head1 ACKNOWLEDGEMENTS
132              
133              
134             =head1 LICENSE AND COPYRIGHT
135              
136             Copyright 2014 Jonas Kramer.
137              
138             This program is free software; you can redistribute it and/or modify it
139             under the terms of the the Artistic License (2.0). You may obtain a
140             copy of the full license at:
141              
142             L
143              
144             Any use, modification, and distribution of the Standard or Modified
145             Versions is governed by this Artistic License. By using, modifying or
146             distributing the Package, you accept this license. Do not use, modify,
147             or distribute the Package, if you do not accept this license.
148              
149             If your Modified Version has been derived from a Modified Version made
150             by someone other than you, you are nevertheless required to ensure that
151             your Modified Version complies with the requirements of this license.
152              
153             This license does not grant you the right to use any trademark, service
154             mark, tradename, or logo of the Copyright Holder.
155              
156             This license includes the non-exclusive, worldwide, free-of-charge
157             patent license to make, have made, use, offer to sell, sell, import and
158             otherwise transfer the Package with respect to any patent claims
159             licensable by the Copyright Holder that are necessarily infringed by the
160             Package. If you institute patent litigation (including a cross-claim or
161             counterclaim) against any party alleging that the Package constitutes
162             direct or contributory patent infringement, then this Artistic License
163             to you shall terminate on the date that such litigation is filed.
164              
165             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
166             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
167             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
168             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
169             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
170             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
171             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
172             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
173              
174              
175             =cut
176              
177             1; # End of Hash::DeepAccess