blib/lib/Data/Tree.pm

filter: Statementstmt 46.5% 54 / 116 Branchbran 16.6% 7 / 42 Conditioncond 10.0% 4 / 40 MC/DCmcdc 4.5% 1 / 22 Subroutinesub 64.7% 11 / 17 Podpod 100% 7 / 7 totaltotal 34.4% 84 / 244 CC 38 SCAR 38.7
CC
38
Cov
33%
get_array
156.0
get_scalar
56.0
increment
20.0
CRAP 47.8
↑ Summary
1 package Data::Tree;
2 {
3 4 $Data::Tree::VERSION = '0.16';
4 }
5 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 $Data::Tree::AUTHORITY = 'cpan:TEX';
7 }
8 # ABSTRACT: a hash-based tree-like data structure
9  
10 4 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # use IO::Handle;
18 # use autodie;
19 # use MooseX::Params::Validate;
20 # use Carp;
21 # use English qw( -no_match_vars );
22 # use Try::Tiny;
23  
24 # extends ...
25 # has ...
26 4 has 'data' => (
27 'is' => 'rw',
28 'isa' => 'HashRef',
29 'lazy' => 1,
30 'builder' => '_init_data',
31 );
32  
33 4 has 'debug' => (
34 'is' => 'rw',
35 'isa' => 'Bool',
36 'lazy' => 1,
37 'builder' => '_init_debug',
38 );
39 # with ...
40 # initializers ...
41 1 ▶sub _init_data {
Subroutine _init_data: called CC=1 SCAR=0.0
42 1 return {};
43 }
44  
45 0 ▶sub _init_debug {
Subroutine _init_debug: not called CC=2 SCAR=17.9
46 0 my $self = shift;
47  
48 0 ▶ if($ENV{'DATA_TREE_DEBUG'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DATA_TREE_DEBUG"}) 0 0 0
49 0 return 1;
50 }
51  
52 0 return 0;
53 }
54  
55 # your code here ...
56 ############################################
57 # Usage : $C->set('Path::To::Key','Value');
58 # Purpose : Set a value to the given key.
59 # Returns :
60 # Parameters :
61 # Throws : no exceptions
62 # Comments : none
63 # See Also : n/a
64 ## no critic (ProhibitAmbiguousNames)
65 3 ▶sub set {
Subroutine set: called CC=4 SCAR=15.0
Pod documented
66 3 my $self = shift;
67 3 my $key = shift;
68 3 my $value = shift;
69 3 ▶ my $force = shift || 0;
Condition50%1/2
shift() || 0
l!l
03
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
70  
71 3 my ( $ref, $last_key ) = $self->_find_leaf($key);
72 3 ▶ if ( ref( $ref->{$last_key} ) eq 'HASH' && !$force ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref->{$last_key} eq "HASH" and not $force) 0 3 3
Condition33%1/3
ref $ref->{$last_key} eq "HASH" and not $force
!ll&&!rl&&r
300
MC/DC0%0/2
ref $ref->{$last_key} eq "HASH" and not $force
ref $ref->{$last_key} eq "HASH" not $force
Truth table33%1/3
A and not(B)
A= ref $ref->{$last_key} eq "HASH"B= not $force
ABresult
0X0
100
111
73 0 return;
74 }
75 3 $ref->{$last_key} = $value;
76 3 return $value;
77 }
78 ## use critic
79  
80 0 ▶sub increment {
Subroutine increment: not called CC=4 SCAR=30.0
Pod documented
81 0 my $self = shift;
82 0 my $key = shift;
83 0 ▶ my $increment = shift // 1;
Condition0%0/2
shift() // 1
l!l
00
MC/DC0%0/1
shift() // 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
84  
85 0 ▶ my $value = $self->get($key) || 0;
Condition0%0/2
$self->get($key) || 0
l!l
00
MC/DC0%0/1
$self->get($key) || 0
$self->get($key)
Truth table0%0/2
A
A= $self->get($key)
Aresult
11
00
86  
87 # bail out if value != numeric
88 0 ▶ if($value !~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^\d+$/) 0 0 0
89 0 return $value;
90 }
91  
92 0 $value += $increment;
93 0 $self->set( $key, $value );
94  
95 0 return $value;
96 }
97  
98 0 ▶sub decrement {
Subroutine decrement: not called CC=4 SCAR=30.0
Pod documented
99 0 my $self = shift;
100 0 my $key = shift;
101 0 ▶ my $decrement = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
102  
103 0 ▶ my $value = $self->get($key) || 0;
Condition0%0/2
$self->get($key) || 0
l!l
00
MC/DC0%0/1
$self->get($key) || 0
$self->get($key)
Truth table0%0/2
A
A= $self->get($key)
Aresult
11
00
104  
105 # bail out if value != numeric
106 0 ▶ if($value !~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^\d+$/) 0 0 0
107 0 return $value;
108 }
109  
110 0 $value -= $decrement;
111 0 $self->set( $key, $value );
112  
113 0 return $value;
114 }
115  
116 ############################################
117 # THIS METHOD IS NOT PART OF OUR PUBLIC API!
118 # Usage :
119 # Purpose :
120 # Returns :
121 # Parameters :
122 # Throws : no exceptions
123 # Comments : none
124 # See Also : n/a
125 # THIS METHOD IS NOT PART OF OUR PUBLIC API!
126 10 ▶sub _find_leaf {
Subroutine _find_leaf: called CC=7 SCAR=25.7
127 10 my $self = shift;
128 10 my $key = shift;
129  
130 10 my @path = ();
131 10 ▶ if ( ref($key) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key eq 'ARRAY') { } 0 10 10
132 0 @path = map { lc($_); } @{$key};
133 }
134 else {
135 10 $key = lc($key);
136 10 @path = split /::/, $key;
137 }
138  
139 10 my $ref = $self->data();
140 10 my $last_step = undef;
141 10 ▶ while ( my $step = shift @path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $step = shift @path) 10 0 10
142 10 $last_step = $step;
143 10 ▶ if ( @path < 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@path < 1) { } 10 0 10
elsif (ref $ref->{$step} eq 'HASH') { } 0 0 0
elsif (@path >= 1) { } 0 0 0
144 10 last;
145 }
146 elsif ( ref( $ref->{$step} ) eq 'HASH' ) {
147 0 $ref = $ref->{$step};
148 }
149 elsif ( @path >= 1 ) {
150 0 $ref->{$step} = {};
151 0 $ref = $ref->{$step};
152 }
153 else {
154 0 ▶ warn "Unhandled condition in _find_leaf w/ key $key in step $step in Data::Tree::_find_leaf().\n" if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
155 }
156 }
157  
158 # ref contains the hash ref one step above the wanted entry,
159 # last_step is the key in this hash to access the wanted
160 # entry.
161 # this is necessary or
162 10 return ( $ref, $last_step );
163 }
164  
165 ############################################
166 # Usage : my $value = $C->get('Path::To::Key');
167 # Purpose : Retrieve a value from the config.
168 # Returns : The value.
169 # Parameters : The name of the key.
170 # Throws : no exceptions
171 # Comments : none
172 # See Also : n/a
173 7 ▶sub get {
Subroutine get: called CC=4 SCAR=14.0
Pod documented
174 7 my $self = shift;
175 7 my $key = shift;
176 7 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
43
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
177  
178 7 my ( $ref, $last_key ) = $self->_find_leaf($key);
179  
180 7 ▶ if ( exists( $ref->{$last_key} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ref->{$last_key}) { } 6 1 7
181 6 return $ref->{$last_key};
182 }
183 else {
184 1 ▶ if ( exists( $opts->{'Default'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{'Default'}) { } 1 0 1
185 1 return $opts->{'Default'};
186 }
187 else {
188 0 return;
189 }
190 }
191 }
192  
193 # return a single value out of an array
194 0 ▶sub get_scalar {
Subroutine get_scalar: not called CC=7 SCAR=40.3
Pod documented
195 0 my $self = shift;
196 0 my $key = shift;
197  
198 0 my $value = $self->get($key);
199  
200 0 ▶ if ( $value && ref($value) && ref($value) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value and ref $value and ref $value eq 'ARRAY') { } 0 0 0
elsif ($value and ref $value and ref $value eq 'HASH') { } 0 0 0
Condition0%0/6
$value and ref $value and ref $value eq "ARRAY"
!ll&&!rl&&r
000
$value and ref $value
!ll&&!rl&&r
000
MC/DC0%0/3
$value and ref $value and ref $value eq "ARRAY"
$value ref $value ref $value eq "ARRAY"
Truth table0%0/4
A and B and C
A= $valueB= ref $valueC= ref $value eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
201 0 return $value->[0];
202 }
203 ▶ elsif ( $value && ref($value) && ref($value) eq 'HASH' ) {
Condition0%0/6
$value and ref $value and ref $value eq "HASH"
!ll&&!rl&&r
000
$value and ref $value
!ll&&!rl&&r
000
MC/DC0%0/3
$value and ref $value and ref $value eq "HASH"
$value ref $value ref $value eq "HASH"
Truth table0%0/4
A and B and C
A= $valueB= ref $valueC= ref $value eq "HASH"
ABCresult
0XX0
10X0
1100
1111
204 0 return ( keys %{$value} )[0];
205 }
206 else {
207 0 return $value;
208 }
209 }
210  
211 ############################################
212 # Usage : my @values = $C->get_array('Path::To::Key');
213 # Purpose : Retrieve an array of values from config.
214 # Returns : The values as an array.
215 # Parameters : The name of the key.
216 # Throws : no exceptions
217 # Comments : none
218 # See Also : n/a
219 0 ▶sub get_array {
Subroutine get_array: not called CC=12 SCAR=50.5
Pod documented
220 0 my $self = shift;
221 0 my $key = shift;
222 0 ▶ my $opts = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
223  
224 0 my $ref = $self->get($key);
225  
226 0 ▶ if ( $ref && ref($ref) eq 'HASH' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($ref and ref $ref eq 'HASH') { } 0 0 0
elsif ($ref and ref $ref eq 'ARRAY') { } 0 0 0
elsif ($ref) { } 0 0 0
elsif (defined $opts->{'Default'} and ref $opts->{'Default'} eq 'ARRAY') { } 0 0 0
Condition0%0/3
$ref and ref $ref eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$ref and ref $ref eq "HASH"
$ref ref $ref eq "HASH"
Truth table0%0/3
A and B
A= $refB= ref $ref eq "HASH"
ABresult
0X0
100
111
227 0 ▶ warn "Returning only the keys of a hashref in Data::Tree::get_array($key).\n" if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
228 0 return ( keys %{$ref} );
229 }
230 ▶ elsif ( $ref && ref($ref) eq 'ARRAY' ) {
Condition0%0/3
$ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$ref and ref $ref eq "ARRAY"
$ref ref $ref eq "ARRAY"
Truth table0%0/3
A and B
A= $refB= ref $ref eq "ARRAY"
ABresult
0X0
100
111
231 0 return @{$ref};
232 }
233 elsif ($ref) {
234 0 return ($ref);
235 }
236 ▶ elsif ( defined( $opts->{'Default'} ) && ref($opts->{'Default'}) eq 'ARRAY' ) {
Condition0%0/3
defined $opts->{"Default"} and ref $opts->{"Default"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts->{"Default"} and ref $opts->{"Default"} eq "ARRAY"
defined $opts->{"Default"} ref $opts->{"Default"} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $opts->{"Default"}B= ref $opts->{"Default"} eq "ARRAY"
ABresult
0X0
100
111
237 0 return @{$opts->{'Default'}};
238 }
239 else {
240 ## no critic (ProhibitMagicNumbers)
241 0 ▶ my $caller = ( caller(1) )[3] || 'n/a';
Condition0%0/2
(caller 1)[3] || 'n/a'
l!l
00
MC/DC0%0/1
(caller 1)[3] || 'n/a'
(caller 1)[3]
Truth table0%0/2
A
A= (caller 1)[3]
Aresult
11
00
242 ## use critic
243 0 ▶ warn "Returning empty array in Data::Tree::get_array($key) to $caller.\n" if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
244 0 return ();
245 }
246 }
247 ## no critic (ProhibitBuiltinHomonyms)
248 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
249 ## use critic
250 0 my $self = shift;
251 0 my $key = shift;
252  
253 0 my ( $ref, $last_key ) = $self->_find_leaf($key);
254  
255 0 ▶ if ( ref($ref) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref eq 'HASH') { } 0 0 0
256 0 delete $ref->{$last_key};
257 0 return 1;
258 }
259 else {
260  
261 # don't know how to handle non hash refs
262 0 return;
263 }
264 }
265  
266 4 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
267 4 __PACKAGE__->meta->make_immutable;
268  
269 4 1;
270  
271 =pod
272  
273 =encoding utf-8
274  
275 =head1 NAME
276  
277 Data::Tree - a hash-based tree-like data structure
278  
279 =head1 SYNOPSIS
280  
281 use Data::Tree;
282 my $DT = Data::Tree::->new();
283  
284 $DT->set('First::Key',[qw(a b c]);
285 $DT->get('First::Key'); # should return [a b c]
286 $DT->get_scalar('First::Key'); # should return a
287 $DT->get_array('First::Key'); # should return (a, b, c)
288  
289 =head1 DESCRIPTION
290  
291 A simple hash-based nested tree.
292  
293 =head1 METHODS
294  
295 =head2 decrement
296  
297 Decrement the numeric value of the given key by one.
298  
299 =head2 delete
300  
301 Remove the given key and all subordinate keys.
302  
303 =head2 get
304  
305 Return the value associated with the given key. May be an SCALAR, HASH or ARRAY.
306  
307 =head2 get_array
308  
309 Return the values associated with the given key as a list.
310  
311 =head2 get_scalar
312  
313 Return the value associated with the given key as an SCALAR.
314  
315 =head2 increment
316  
317 Increment the numeric value of the given key by one.
318  
319 =head2 set
320  
321 Set the value of the given key to the given value.
322  
323 =head1 NAME
324  
325 Data::Tree - A simple hash-based tree.
326  
327 =head1 AUTHOR
328  
329 Dominik Schulz <dominik.schulz@gauner.org>
330  
331 =head1 COPYRIGHT AND LICENSE
332  
333 This software is copyright (c) 2012 by Dominik Schulz.
334  
335 This is free software; you can redistribute it and/or modify it under
336 the same terms as the Perl 5 programming language system itself.
337  
338 =cut
339  
340 __END__
↑ Summary