File Coverage

lib/Data/Hash/DotNotation.pm
Criterion Covered Total %
statement 58 63 92.0
branch 10 14 71.4
condition 7 13 53.8
subroutine 10 10 100.0
pod 0 5 0.0
total 85 105 80.9


line stmt bran cond sub pod time code
1             package Data::Hash::DotNotation;
2 2     2   3832 use 5.010;
  2         6  
  2         84  
3 2     2   9 use strict;
  2         4  
  2         61  
4 2     2   16 use warnings;
  2         2  
  2         1548  
5              
6             our $VERSION = '1.00';
7              
8             =head1 NAME
9              
10             Data::Hash::DotNotation - Convenient representation for nested Hash structures
11              
12             =head1 VERSION
13              
14             1.00
15              
16             =head1 SYNOPSYS
17              
18             use Data::Hash::DotNotation;
19              
20             my $dn = Data::Hash::DotNotation->new({
21             name => 'Gurgeh',
22             planet => 'earth',
23             score => {
24             contact => 10,
25             scrabble => 20,
26             },
27             });
28              
29             print $dn->get('score.contact');
30             $dn->set('score.scrabble', 50);
31              
32             # return the complete modified hashref
33             my $gamer_info = $dn->data;
34              
35             =cut
36              
37             sub new {
38 1     1 0 15 my ($class, $args) = @_;
39 1   50     7 my $data = $args || {};
40              
41 1         4 my $self->{data} = $data;
42 1         3 bless $self, $class;
43              
44 1         4 return $self;
45             }
46              
47             sub data {
48 23     23 0 28 my ($self) = @_;
49 23         58 return $self->{data};
50             }
51              
52             sub get {
53 4     4 0 38 my $self = shift;
54 4 100       20 my $name = shift or die "No name given";
55 3         8 return $self->_get($name);
56             }
57              
58             sub set {
59 5     5 0 977 my $self = shift;
60 5 100       20 my $name = shift or die 'No name given';
61 4         4 my $value = shift;
62              
63 4         11 $self->_set($name, $value);
64              
65 4         14 return $value;
66             }
67              
68             sub key_exists {
69 2     2 0 245 my $self = shift;
70 2         3 my $name = shift;
71 2         4 my $data = $self->data;
72              
73 2         6 my @parts = split(/\./, $name);
74 2         3 my $node = pop @parts;
75 2         2 my $parent_node;
76              
77 2   33     13 while ($data and (my $section = shift @parts)) {
78 0 0       0 if (ref $data->{$section} eq 'HASH') {
79 0         0 $data = $data->{$section};
80             } else {
81 0         0 return;
82             }
83             }
84              
85 2         7 return exists $data->{$node};
86             }
87              
88             sub _get {
89 3     3   3 my $self = shift;
90 3         20 my $name = shift;
91 3         6 my $data = $self->data;
92              
93 3         12 my @parts = split(/\./, $name);
94 3         6 my $node = pop @parts;
95 3         2 my $parent_node;
96              
97 3   66     16 while ($data and (my $section = shift @parts)) {
98 2 50       8 if (ref $data->{$section} eq 'HASH') {
99 2         11 $data = $data->{$section};
100             } else {
101 0         0 return;
102             }
103             }
104              
105 3 100 66     648 if ($data and exists $data->{$node}) {
106 2         12 return $data->{$node};
107             }
108              
109 1         6 return;
110             }
111              
112             sub _set {
113 4     4   5 my $self = shift;
114 4         4 my $name = shift;
115 4         4 my $value = shift;
116              
117 4 50       6 unless ($self->data) {
118 0         0 $self->data({});
119             }
120              
121 4         17 my @tarts = split(/\./, $name);
122 4         6 my $node = pop @tarts;
123              
124 4         8 my $current_location = $self->data;
125 4         8 foreach my $section (@tarts) {
126 2   50     8 $current_location->{$section} //= {};
127 2         4 $current_location = $current_location->{$section};
128             }
129              
130 4 100       8 if (defined($value)) {
131 3         7 $current_location->{$node} = $value;
132             } else {
133 1         3 delete $current_location->{$node};
134             }
135              
136 4         8 return $self->data;
137             }
138              
139             1;
140              
141             =head1 SOURCE CODE
142              
143             L
144              
145             =head1 AUTHOR
146              
147             binary.com, C<< >>
148              
149             =head1 BUGS
150              
151             Please report any bugs or feature requests to
152             C, or through the web
153             interface at
154             L.
155             We will be notified, and then you'll automatically be notified of progress on
156             your bug as we make changes.
157              
158             =head1 SUPPORT
159              
160             You can find documentation for this module with the perldoc command.
161              
162             perldoc Data::Hash::DotNotation
163              
164             You can also look for information at:
165              
166             =over 4
167              
168             =item * RT: CPAN's request tracker (report bugs here)
169              
170             L
171              
172             =item * AnnoCPAN: Annotated CPAN documentation
173              
174             L
175              
176             =item * CPAN Ratings
177              
178             L
179              
180             =item * Search CPAN
181              
182             L
183              
184             =back
185              
186             =head1 LICENSE AND COPYRIGHT
187              
188             Copyright (C) 2015 binary.com
189              
190             This program is free software; you can redistribute it and/or modify it
191             under the terms of the the Artistic License (2.0). You may obtain a
192             copy of the full license at:
193              
194             L
195              
196             Any use, modification, and distribution of the Standard or Modified
197             Versions is governed by this Artistic License. By using, modifying or
198             distributing the Package, you accept this license. Do not use, modify,
199             or distribute the Package, if you do not accept this license.
200              
201             If your Modified Version has been derived from a Modified Version made
202             by someone other than you, you are nevertheless required to ensure that
203             your Modified Version complies with the requirements of this license.
204              
205             This license does not grant you the right to use any trademark, service
206             mark, tradename, or logo of the Copyright Holder.
207              
208             This license includes the non-exclusive, worldwide, free-of-charge
209             patent license to make, have made, use, offer to sell, sell, import and
210             otherwise transfer the Package with respect to any patent claims
211             licensable by the Copyright Holder that are necessarily infringed by the
212             Package. If you institute patent litigation (including a cross-claim or
213             counterclaim) against any party alleging that the Package constitutes
214             direct or contributory patent infringement, then this Artistic License
215             to you shall terminate on the date that such litigation is filed.
216              
217             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
218             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
219             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
220             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
221             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
222             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
223             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
224             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
225              
226             =cut
227