| 1 | 2 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Data::Visitor::Tiny; | ||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Recursively walk data structures | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Exporter 5.57 qw/import/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | our @EXPORT = qw/visit/; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | #pod =func visit | ||||||||||||||||||||||||||||
| 16 | #pod | ||||||||||||||||||||||||||||
| 17 | #pod visit( $ref, sub { ... } ); | ||||||||||||||||||||||||||||
| 18 | #pod | ||||||||||||||||||||||||||||
| 19 | #pod The C<visit> function takes a hashref or arrayref and recursively visits | ||||||||||||||||||||||||||||
| 20 | #pod all values via pre-order traversal, calling the provided callback for each | ||||||||||||||||||||||||||||
| 21 | #pod value. Only hashrefs and arrayrefs are traversed; objects, even if they | ||||||||||||||||||||||||||||
| 22 | #pod override hash or array dereference, are only ever treated as values. Hash | ||||||||||||||||||||||||||||
| 23 | #pod keys are sorted lexicographically before iteration, ensuring consistent | ||||||||||||||||||||||||||||
| 24 | #pod visitation order in the face of Perl's hash order randomization. | ||||||||||||||||||||||||||||
| 25 | #pod | ||||||||||||||||||||||||||||
| 26 | #pod Within the callback, the C<$_> variable is set to the value of the node. | ||||||||||||||||||||||||||||
| 27 | #pod The callback also receives three arguments: C<$key>, C<$valueref>, and | ||||||||||||||||||||||||||||
| 28 | #pod C<$context>. The C<$key> is the hash key or array index of the value. The | ||||||||||||||||||||||||||||
| 29 | #pod C<$valueref> is a scalar reference to the value; use it to modify the value | ||||||||||||||||||||||||||||
| 30 | #pod in place. The C<$context> is a hashref for tracking state throughout the | ||||||||||||||||||||||||||||
| 31 | #pod visiting process. Context keys beginning with '_' are reserved for | ||||||||||||||||||||||||||||
| 32 | #pod C<Data::Visitor::Tiny>; you may store whatever other keys/values you need. | ||||||||||||||||||||||||||||
| 33 | #pod The only key provided currently is C<_depth>, which starts at 0 and | ||||||||||||||||||||||||||||
| 34 | #pod reflects how deep the visitor has recursed. | ||||||||||||||||||||||||||||
| 35 | #pod | ||||||||||||||||||||||||||||
| 36 | #pod The C<visit> function returns the context object. | ||||||||||||||||||||||||||||
| 37 | #pod | ||||||||||||||||||||||||||||
| 38 | #pod =cut | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 6 | ▶ | sub visit { | ||||||||||||||||||||||||||
|
Subroutine
visit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 6 | my ( $ref, $fcn ) = @_; | |||||||||||||||||||||||||||
| 42 | 6 | my $ctx = { _depth => 0 }; | |||||||||||||||||||||||||||
| 43 | 6 | _visit( $ref, $fcn, $ctx ); | |||||||||||||||||||||||||||
| 44 | 6 | return $ctx; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 21 | ▶ | sub _visit { | ||||||||||||||||||||||||||
|
Subroutine
_visit: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||
| 48 | 21 | my ( $ref, $fcn, $ctx ) = @_; | |||||||||||||||||||||||||||
| 49 | 21 | my $type = ref($ref); | |||||||||||||||||||||||||||
| 50 | 21 | ▶ | croak("'$ref' is not an ARRAY or HASH") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$type eq "ARRAY" or $type eq "HASH"
MC/DC0%0/2
$type eq "ARRAY" or $type eq "HASH"
$type eq "ARRAY"
$type eq "HASH"
Truth table66%2/3
A or B
A= $type eq "ARRAY"B= $type eq "HASH"
| |||||||||||||||||||||||||||||
| 51 | unless $type eq 'ARRAY' || $type eq 'HASH'; | ||||||||||||||||||||||||||||
| 52 | 21 | ▶ | my @elems = $type eq 'ARRAY' ? ( 0 .. $#$ref ) : ( sort keys %$ref ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 21 | for my $idx (@elems) { | |||||||||||||||||||||||||||
| 54 | 54 | my ( $v, $vr ); | |||||||||||||||||||||||||||
| 55 | 54 | ▶ | $v = $type eq 'ARRAY' ? $ref->[$idx] : $ref->{$idx}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 54 | ▶ | $vr = $type eq 'ARRAY' ? \( $ref->[$idx] ) : \( $ref->{$idx} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 54 | local $_ = $v; | |||||||||||||||||||||||||||
| 58 | # Wrap $fcn in dummy for loop to guard against bare 'next' in $fcn | ||||||||||||||||||||||||||||
| 59 | 54 | for my $dummy (0) { $fcn->( $idx, $vr, $ctx ) } | |||||||||||||||||||||||||||
| 60 | 54 | ▶ | if ( ref($v) eq 'ARRAY' || ref($v) eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $v eq "ARRAY" or ref $v eq "HASH"
MC/DC100%2/2
ref $v eq "ARRAY" or ref $v eq "HASH"
ref $v eq "ARRAY"
ref $v eq "HASH"
Truth table100%3/3
A or B
A= ref $v eq "ARRAY"B= ref $v eq "HASH"
| |||||||||||||||||||||||||||||
| 61 | 15 | $ctx->{_depth}++; | |||||||||||||||||||||||||||
| 62 | 15 | _visit( $v, $fcn, $ctx ); | |||||||||||||||||||||||||||
| 63 | 15 | $ctx->{_depth}--; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 2 | 1; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # vim: ts=4 sts=4 sw=4 et tw=75: | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | __END__ | ||||||||||||||||||||||||||||