blib/lib/Data/Leaf/Walker.pm

filter: Statementstmt 96.3% 133 / 138 Branchbran 85.1% 46 / 54 Conditioncond 100% 14 / 14 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 14 / 14 Podpod 100% 10 / 10 totaltotal 94.5% 225 / 238 CC 37 SCAR 17.5
CC
37
Cov
94%
_iterate
12.0
exists
6.1
_each
5.0
CRAP 5.8
↑ Summary
1 package Data::Leaf::Walker;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Data::Leaf::Walker - Walk the leaves of arbitrarily deep nested data structures.
9  
10 =head1 VERSION
11  
12 Version 0.21
13  
14 =cut
15  
16 7 our $VERSION = '0.21';
17  
18 =head1 SYNOPSIS
19  
20 $data = {
21 a => 'hash',
22 or => [ 'array', 'ref' ],
23 with => { arbitrary => 'nesting' },
24 };
25  
26 $walker = Data::Leaf::Walker->new( $data );
27
28 while ( my ( $k, $v ) = $walker->each )
29 {
30 print "@{ $k } : $v\n";
31 }
32
33 ## output might be
34 ## a : hash
35 ## or 0 : array
36 ## or 1 : ref
37 ## with arbitrary : nesting
38  
39 =head1 DESCRIPTION
40  
41 C<Data::Leaf::Walker> provides simplified access to nested data structures. It
42 operates on key paths in place of keys. A key path is a list of HASH and ARRAY
43 indexes which define a path through your data structure. For example, in the
44 following data structure, the value corresponding to key path C<[ 0, 'foo' ]> is
45 C<bar>:
46  
47 $aoh = [ { foo => 'bar' } ];
48  
49 You can get and set that value like so:
50  
51 $walker = Data::Leaf::Walker->new( $aoh ); ## create the walker
52 $bar = $walker->fetch( [ 0, 'foo' ] ); ## get the value 'bar'
53 $walker->store( [ 0, 'foo'], 'baz' ); ## change value to 'baz'
54  
55 =head1 FUNCTIONS
56  
57 =head2 new( $data )
58  
59 Construct a new C<Data::Leaf::Walker> instance.
60  
61 $data = {
62 a => 'hash',
63 or => [ 'array', 'ref' ],
64 with => { arbitrary => 'nesting' },
65 };
66  
67 $walker = Data::Leaf::Walker->new( $data );
68
69 =head3 Options
70  
71 =over 3
72  
73 =item * max_depth: the C<each>, C<keys> and C<values> methods iterate no deeper
74 than C<max_depth> keys deep.
75  
76 =item * min_depth: the C<each>, C<keys> and C<values> methods iterate no shallower
77 than C<min_depth> keys deep.
78  
79 =back
80  
81 =cut
82  
83 sub new
84 31 ▶ {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
85 31 my ( $class, $data, %opts ) = @_;
86 31 my $self = bless
87 {
88 _data => $data,
89 _data_stack => [],
90 _key_path => [],
91 _array_tracker => {},
92 _opts => {},
93 }, $class;
94 31 $self->opts( %opts );
95 31 return $self;
96 }
97  
98 =head2 each()
99  
100 Iterates over the leaf values of the nested HASH or ARRAY structures. Much like
101 the built-in C<each %hash> function, the iterators for individual structures are
102 global and the caller should be careful about what state they are in. Invoking
103 the C<keys()> or C<values()> methods will reset the iterators. In scalar
104 context it returns the key path only.
105  
106 while ( my ( $key_path, $value ) = $walker->each )
107 {
108 ## do something
109 }
110  
111 =cut
112  
113 sub each
114 282 ▶ {
Subroutine each: called CC=2 SCAR=6.9
Pod documented
115 282 my ( $self ) = @_;
116
117 282 ▶ if ( ! @{ $self->{_data_stack} } )
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"_data_stack"};}) 49 233 282
118 {
119 49 push @{ $self->{_data_stack} }, $self->{_data};
120 }
121
122 282 return $self->_iterate;
123 }
124  
125 =head2 keys()
126  
127 Returns the list of all key paths.
128  
129 @key_paths = $walker->keys;
130  
131 =cut
132  
133 sub keys
134 28 ▶ {
Subroutine keys: called CC=2 SCAR=6.9
Pod documented
135 28 my ( $self ) = @_;
136  
137 28 my @keys;
138  
139 28 ▶ while ( defined( my $key = $self->each ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = $self->each)) 61 28 89
140 {
141 61 push @keys, $key;
142 }
143
144 28 return @keys;
145 }
146
147 =head2 values()
148  
149 Returns the list of all leaf values.
150  
151 @leaf_values = $walker->values;
152  
153 =cut
154  
155 sub values
156 3 ▶ {
Subroutine values: called CC=2 SCAR=6.9
Pod documented
157 3 my ( $self ) = @_;
158  
159 3 my @values;
160  
161 3 ▶ while ( my ($key, $value) = $self->each )
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->each) 57 3 60
162 {
163 57 push @values, $value;
164 }
165  
166 3 return @values;
167 }
168  
169 =head2 fetch( $key_path )
170  
171 Lookup the value corresponding to the given key path. If an individual key
172 attempts to fetch from an invalid the fetch method dies.
173  
174 $key_path = [ $key1, $index1, $index2, $key2 ];
175 $leaf = $walker->fetch( $key_path );
176  
177 =cut
178  
179 sub fetch
180 178 ▶ {
Subroutine fetch: called CC=4 SCAR=13.9
Pod documented
181 178 my ( $self, $key_path ) = @_;
182  
183 178 my $data = $self->{_data};
184
185 178 for my $key ( @{ $key_path } )
186 {
187  
188 431 my $type = ref $data;
189
190 431 ▶ if ( $type eq 'ARRAY' )
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 282 149 431
elsif ($type eq 'HASH') { } 146 3 149
191 {
192 282 $data = $data->[$key];
193 }
194 elsif ( $type eq 'HASH' )
195 {
196 146 $data = $data->{$key};
197 }
198 else
199 {
200 3 die "Error: cannot lookup key ($key) in invalid ref type ($type)";
201 }
202
203 }
204
205 175 return $data;
206 }
207  
208 =head2 store( $key_path, $value )
209  
210 Set the value for the corresponding key path.
211  
212 $key_path = [ $key1, $index1, $index2, $key2 ];
213 $walker->store( $key_path, $value );
214  
215 =cut
216  
217 sub store
218 54 ▶ {
Subroutine store: called CC=4 SCAR=14.1
Pod documented
219 54 my ( $self, $key_path, $value ) = @_;
220
221 54 my @store_path = @{ $key_path };
222
223 54 my $twig_key = pop @store_path;
224
225 54 my $twig = $self->fetch( \@store_path );
226
227 54 ▶ if ( ! defined $twig )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $twig) 0 54 54
228 {
229 0 die "Error: cannot autovivify arbitrarily";
230 }
231
232 54 my $type = ref $twig;
233
234 54 ▶ if ( $type eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 21 33 54
elsif ($type eq 'ARRAY') { } 33 0 33
235 {
236 21 return $twig->{ $twig_key } = $value;
237 }
238 elsif ( $type eq 'ARRAY' )
239 {
240 33 return $twig->[ $twig_key ] = $value;
241 }
242
243 }
244  
245 =head2 delete( $key_path )
246  
247 Delete the leaf key in the corresponding key path. Only works for a HASH leaf,
248 dies otherwise. Returns the deleted value.
249  
250 $key_path = [ $key1, $index1, $index2, $key2 ];
251 $old_value = $walker->delete( $key_path );
252  
253 =cut
254  
255 sub delete
256 3 ▶ {
Subroutine delete: called CC=4 SCAR=15.0
Pod documented
257 3 my ( $self, $key_path ) = @_;
258  
259 3 my @delete_path = @{ $key_path };
260
261 3 my $twig_key = pop @delete_path;
262
263 3 my $twig = $self->fetch( \@delete_path );
264
265 3 ▶ defined $twig || return;
Branch: 1 branch
BranchTrueFalseTotal
defined $twig or return 3 0 3
266
267 3 my $type = ref $twig;
268
269 3 ▶ if ( $type eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 3 0 3
elsif ($type eq 'ARRAY') { } 0 0 0
270 {
271 3 return delete $twig->{ $twig_key };
272 }
273 elsif ( $type eq 'ARRAY' )
274 {
275 0 die "Error: cannot delete() from an ARRAY leaf";
276 }
277
278 }
279  
280 =head2 exists( $key_path )
281  
282 Returns true if the corresponding key path exists.
283  
284 $key_path = [ $key1, $index1, $index2, $key2 ];
285 if ( $walker->exists( $key_path ) )
286 {
287 ## do something
288 }
289  
290 =cut
291  
292 sub exists
293 15 ▶ {
Subroutine exists: called CC=6 SCAR=18.1
Pod documented
294 15 my ( $self, $key_path ) = @_;
295  
296 15 my $data = $self->{_data};
297
298 15 for my $key ( @{ $key_path } )
299 {
300  
301 51 my $type = ref $data;
302
303 51 ▶ if ( $type eq 'ARRAY' )
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 39 12 51
elsif ($type eq 'HASH') { } 9 3 12
304 {
305 39 ▶ if ( exists $data->[$key] )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->[$key]) { } 36 3 39
306 {
307 36 $data = $data->[$key];
308 }
309 else
310 {
311 3 return;
312 }
313 }
314 elsif ( $type eq 'HASH' )
315 {
316 9 ▶ if ( exists $data->{$key} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{$key}) { } 0 9 9
317 {
318 0 $data = $data->{$key};
319 }
320 else
321 {
322 9 return;
323 }
324 }
325 else
326 {
327 3 return;
328 }
329
330 }
331
332 0 return 1;
333 }
334  
335 =head2 reset()
336  
337 Resets the current iterators. This is faster than using the C<keys()> or
338 C<values()> methods to do an iterator reset.
339  
340 ## set the max depth one above the bottom, to get the twig structures
341 $key_path = $walker->each;
342 $walker->opts( max_depth => @{ $key_path } - 1 );
343 $walker->reset;
344 @twigs = $walker->values;
345  
346 =cut
347  
348 sub reset
349 7 ▶ {
Subroutine reset: called CC=3 SCAR=11.0
Pod documented
350 7 my ( $self ) = @_;
351
352 7 for my $data ( @{ $self->{_data_stack} } )
353 {
354 23 ▶ if ( ref $data eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH") 13 10 23
355 {
356 13 CORE::keys %{ $data };
357 }
358 }
359  
360 7 %{ $self->{_array_tracker} } = ();
361 7 @{ $self->{_data_stack} } = ();
362 7 @{ $self->{_key_path} } = ();
363
364 7 return;
365 }
366  
367 =head2 opts()
368  
369 Change the values of the constructor options. Only given options are affected.
370 See C<new()> for a description of the options. Returns the current option hash
371 after changes are applied.
372  
373 ## change the max_depth
374 $walker->opts( max_depth => 3 );
375
376 ## get the current options
377 %opts = $walker->opts;
378  
379 =cut
380  
381 sub opts
382 38 ▶ {
Subroutine opts: called CC=3 SCAR=11.0
Pod documented
383 38 my ( $self, %opts ) = @_;
384  
385 38 ▶ if ( CORE::keys %opts )
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::keys %opts) 5 33 38
386 {
387  
388 5 for my $key ( CORE::keys %opts )
389 {
390 6 $self->{_opts}{$key} = $opts{$key};
391 }
392  
393 }
394  
395 38 return %{ $self->{_opts} };
396 }
397  
398 sub _iterate
399 638 ▶ {
Subroutine _iterate: called CC=12 SCAR=24.8
400 638 my ( $self ) = @_;
401  
402 ## find the top of the stack
403 638 my $data = ${ $self->{_data_stack} }[-1];
404
405 ## iterate on the stack top
406 638 my ( $key, $val ) = $self->_each($data);
407  
408 ## if we're at the end of the stack top
409 638 ▶ if ( ! defined $key )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 203 435 638
410 {
411 ## remove the stack top
412 203 pop @{ $self->{_data_stack} };
413 203 pop @{ $self->{_key_path} };
414  
415 ## iterate on the new stack top if available
416 203 ▶ if ( @{ $self->{_data_stack} } )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'_data_stack'};}) { } 161 42 203
417 {
418 161 return $self->_iterate;
419 }
420 ## mark the stack as empty
421 ## return empty/undef
422 else
423 {
424 42 return;
425 }
426  
427 }
428
429 ## _each() succeeded
430  
431 ## return right away if we're at max_depth
432 435 my $max_depth = $self->{_opts}{max_depth};
433 435 ▶ if ( defined $max_depth && @{ $self->{_key_path} } + 1 >= $max_depth )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_depth and @{$self->{"_key_path"};} + 1 >= $max_depth) 84 351 435
Condition100%3/3
defined $max_depth and @{$self->{"_key_path"};} + 1 >= $max_depth
!ll&&!rl&&r
23211984
MC/DC100%2/2
defined $max_depth and @{$self->{"_key_path"};} + 1 >= $max_depth
defined $max_depth @{$self->{"_key_path"};} + 1 >= $max_depth
Truth table100%3/3
A and B
A= defined $max_depthB= @{$self->{"_key_path"};} + 1 >= $max_depth
ABresult
0X0
100
111
434 {
435 84 my $key_path = [ @{ $self->{_key_path} }, $key ];
436 84 ▶ return wantarray ? ( $key_path, $val ) : $key_path;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 56 28 84
437 }
438  
439 ## if the value is a HASH/ARRAY, add it to the stack and iterate
440 351 ▶ if ( defined $val && ( ref $val eq 'HASH' || ref $val eq 'ARRAY' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and ref $val eq "HASH" || ref $val eq "ARRAY") 177 174 351
Condition100%6/6
defined $val and ref $val eq "HASH" || ref $val eq "ARRAY"
!ll&&!rl&&r
3171177
ref $val eq "HASH" or ref $val eq "ARRAY"
l!l&&r!l&&!r
72105171
MC/DC100%3/3
defined $val and ref $val eq "HASH" || ref $val eq "ARRAY"
defined $val ref $val eq "HASH" ref $val eq "ARRAY"
Truth table100%4/4
A and B or C
A= defined $valB= ref $val eq "HASH"C= ref $val eq "ARRAY"
ABCresult
0XX0
1000
11X1
1011
441 {
442 177 push @{ $self->{_data_stack} }, $val;
443 177 push @{ $self->{_key_path} }, $key;
444 177 return $self->_iterate;
445 }
446
447 ## continue iterating if we are less than min_depth
448 174 my $min_depth = $self->{_opts}{min_depth};
449 174 ▶ if ( defined $min_depth && @{ $self->{_key_path} } + 1 < $min_depth )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min_depth and @{$self->{"_key_path"};} + 1 < $min_depth) 18 156 174
Condition100%3/3
defined $min_depth and @{$self->{"_key_path"};} + 1 < $min_depth
!ll&&!rl&&r
1253118
MC/DC100%2/2
defined $min_depth and @{$self->{"_key_path"};} + 1 < $min_depth
defined $min_depth @{$self->{"_key_path"};} + 1 < $min_depth
Truth table100%3/3
A and B
A= defined $min_depthB= @{$self->{"_key_path"};} + 1 < $min_depth
ABresult
0X0
100
111
450 {
451 18 return $self->_iterate;
452 }
453  
454 156 my $key_path = [ @{ $self->{_key_path} }, $key ];
455  
456 156 ▶ return wantarray ? ( $key_path, $val ) : $key_path;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 106 50 156
457 }
458  
459 sub _each
460 638 ▶ {
Subroutine _each: called CC=5 SCAR=16.1
461 638 my ( $self, $data ) = @_;
462
463 638 ▶ if ( ref $data eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 259 379 638
elsif (ref $data eq 'ARRAY') { } 379 0 379
464 {
465 259 return CORE::each %{ $data };
466 }
467 elsif ( ref $data eq 'ARRAY' )
468 {
469 379 my $array_tracker = $self->{_array_tracker};
470 379 ▶ $array_tracker->{ $data } ||= 0;
Condition100%2/2
$array_tracker->{$data} ||= 0
l!l
251128
MC/DC100%1/1
$array_tracker->{$data} ||= 0
$array_tracker->{$data}
Truth table100%2/2
A
A= $array_tracker->{$data}
Aresult
11
00
471 379 ▶ if ( $array_tracker->{ $data } <= $#{ $data } )
Branch: 1 branch
BranchTrueFalseTotal
if ($array_tracker->{$data} <= $#{$data;}) { } 261 118 379
472 {
473 261 my $index = $array_tracker->{ $data };
474 261 ++ $array_tracker->{ $data };
475 261 return( $index, $data->[ $index ] );
476 }
477 else
478 {
479 118 $array_tracker->{ $data } = 0;
480 118 return;
481 }
482
483 }
484 else
485 {
486 0 die "Error: cannot call _each() on non-HASH/non-ARRAY data record";
487 }
488
489 }
490  
491 =head1 AUTHOR
492  
493 Dan Boorstein, C<< <danboo at cpan.org> >>
494  
495 =head1 CAVEATS
496  
497 =head2 Global Iterators
498  
499 Because the iterators are global, data structures which contain cyclical
500 references or repeated sub structures are not handled correctly.
501  
502 =head2 Hash Iterators
503  
504 If you iterate directly over a hash which is also contained in your leaf walker
505 instance, be sure to leave it in a proper state. If that hash is a sub reference
506 within the leaf walker, calling the C<keys()> or C<values()> methods, for the
507 purpose of resetting the iterator, may not be able to reach the hash. A second
508 reset attempt should work as expected. If you consistently use the leaf walker
509 instance to access the data structure, you should be fine.
510  
511 =head1 PLANS
512  
513 =over 3
514  
515 =item * add type and twig limiters for C<each>, C<keys>, C<values>
516  
517 =item * optional autovivification (Data::Peek, Scalar::Util, String::Numeric)
518  
519 =back
520  
521 =head1 BUGS
522  
523 Please report any bugs or feature requests to C<bug-Data-Leaf-Walker at rt.cpan.org>, or through
524 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Leaf-Walker>. I will be notified, and then you'll
525 automatically be notified of progress on your bug as I make changes.
526  
527 =head1 SUPPORT
528  
529 You can find documentation for this module with the perldoc command.
530  
531 perldoc Data::Leaf::Walker
532  
533  
534 You can also look for information at:
535  
536 =over 4
537  
538 =item * RT: CPAN's request tracker
539  
540 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Leaf-Walker>
541  
542 =item * AnnoCPAN: Annotated CPAN documentation
543  
544 L<http://annocpan.org/dist/Data-Leaf-Walker>
545  
546 =item * CPAN Ratings
547  
548 L<http://cpanratings.perl.org/d/Data-Leaf-Walker>
549  
550 =item * Search CPAN
551  
552 L<http://search.cpan.org/dist/Data-Leaf-Walker/>
553  
554 =back
555  
556  
557 =head1 ACKNOWLEDGEMENTS
558  
559  
560 =head1 COPYRIGHT & LICENSE
561  
562 Copyright 2009 Dan Boorstein.
563  
564 This program is free software; you can redistribute it and/or modify it
565 under the terms of either: the GNU General Public License as published
566 by the Free Software Foundation; or the Artistic License.
567  
568 See http://dev.perl.org/licenses/ for more information.
569  
570  
571 =cut
572  
573 7 1; # End of Data::Leaf::Walker
↑ Summary