line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tree::Simple::Visitor::ToNestedHash; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
16807
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
4
|
1
|
|
|
1
|
|
2
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.14'; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
3
|
use Scalar::Util qw(blessed); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
88
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
4
|
use base qw(Tree::Simple::Visitor); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
410
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
5
|
|
|
5
|
1
|
5326
|
my ($_class) = @_; |
14
|
5
|
|
33
|
|
|
24
|
my $class = ref($_class) || $_class; |
15
|
5
|
|
|
|
|
7
|
my $visitor = {}; |
16
|
5
|
|
|
|
|
7
|
bless($visitor, $class); |
17
|
5
|
|
|
|
|
20
|
$visitor->_init(); |
18
|
5
|
|
|
|
|
34
|
return $visitor; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub visit { |
22
|
8
|
|
|
8
|
1
|
8872
|
my ($self, $tree) = @_; |
23
|
8
|
100
|
100
|
|
|
87
|
(blessed($tree) && $tree->isa("Tree::Simple")) |
24
|
|
|
|
|
|
|
|| die "Insufficient Arguments : You must supply a valid Tree::Simple object"; |
25
|
|
|
|
|
|
|
# grab our filter (if we have one) |
26
|
4
|
|
|
|
|
17
|
my $filter = $self->getNodeFilter(); |
27
|
4
|
|
|
|
|
17
|
my %results; |
28
|
|
|
|
|
|
|
# get the array |
29
|
4
|
|
|
|
|
10
|
$self->_buildHash($tree, \%results, $filter); |
30
|
|
|
|
|
|
|
# add the trunk if we need to |
31
|
4
|
100
|
|
|
|
12
|
%results = ( |
|
|
100
|
|
|
|
|
|
32
|
|
|
|
|
|
|
((defined($filter)) ? |
33
|
|
|
|
|
|
|
$filter->($tree) |
34
|
|
|
|
|
|
|
: |
35
|
|
|
|
|
|
|
$tree->getNodeValue()) => { %results } |
36
|
|
|
|
|
|
|
) if $self->includeTrunk(); |
37
|
|
|
|
|
|
|
# set results |
38
|
4
|
|
|
|
|
52
|
$self->setResults(\%results); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _buildHash { |
42
|
8
|
|
|
8
|
|
52
|
my ($self, $tree, $accumulator, $filter) = @_; |
43
|
8
|
|
|
|
|
18
|
foreach my $child ($tree->getAllChildren()) { |
44
|
16
|
|
|
|
|
48
|
my $node_value = {}; |
45
|
16
|
100
|
|
|
|
40
|
my $node_key = (defined($filter) ? $filter->($child) : $child->getNodeValue()); |
46
|
16
|
100
|
|
|
|
75
|
$self->_buildHash($child, $node_value, $filter) unless $child->isLeaf(); |
47
|
16
|
|
|
|
|
81
|
$accumulator->{$node_key} = $node_value; |
48
|
|
|
|
|
|
|
} |
49
|
8
|
|
|
|
|
10
|
return $accumulator; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
1; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
__END__ |