line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tree::Simple::Visitor::FromNestedHash; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
69662
|
use strict; |
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
29
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.16'; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
22
|
use Scalar::Util qw(blessed); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
72
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
5
|
use base qw(Tree::Simple::Visitor); |
|
1
|
|
|
|
|
9
|
|
|
1
|
|
|
|
|
511
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
4
|
|
|
4
|
1
|
11747
|
my ($_class) = @_; |
14
|
4
|
|
33
|
|
|
21
|
my $class = ref($_class) || $_class; |
15
|
4
|
|
|
|
|
9
|
my $visitor = {}; |
16
|
4
|
|
|
|
|
7
|
bless($visitor, $class); |
17
|
4
|
|
|
|
|
12
|
$visitor->_init(); |
18
|
4
|
|
|
|
|
33
|
return $visitor; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub _init { |
22
|
4
|
|
|
4
|
|
7
|
my ($self) = @_; |
23
|
4
|
|
|
|
|
9
|
$self->{hash_tree} = undef; |
24
|
4
|
|
|
|
|
15
|
$self->SUPER::_init(); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub setHashTree { |
28
|
8
|
|
|
8
|
1
|
3913
|
my ($self, $hash_tree) = @_; |
29
|
8
|
100
|
100
|
|
|
56
|
(defined($hash_tree) && ref($hash_tree) eq 'HASH') |
30
|
|
|
|
|
|
|
|| die "Insufficient Arguments : You must supply a valid HASH reference"; |
31
|
|
|
|
|
|
|
# validate the tree ... |
32
|
|
|
|
|
|
|
# it must not be empty |
33
|
5
|
100
|
|
|
|
7
|
(scalar keys %{$hash_tree} == 1) |
|
5
|
|
|
|
|
28
|
|
34
|
|
|
|
|
|
|
|| die "Insufficient Arguments : The hash tree provided must be a single rooted tree"; |
35
|
3
|
|
|
|
|
7
|
$self->{hash_tree} = $hash_tree; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub visit { |
39
|
7
|
|
|
7
|
1
|
3032
|
my ($self, $tree) = @_; |
40
|
7
|
100
|
100
|
|
|
76
|
(blessed($tree) && $tree->isa("Tree::Simple")) |
41
|
|
|
|
|
|
|
|| die "Insufficient Arguments : You must supply a valid Tree::Simple object"; |
42
|
|
|
|
|
|
|
$self->_buildTree( |
43
|
|
|
|
|
|
|
$tree, |
44
|
|
|
|
|
|
|
$self->{hash_tree}, |
45
|
3
|
|
|
|
|
12
|
$self->getNodeFilter(), |
46
|
|
|
|
|
|
|
$self->includeTrunk() |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _buildTree { |
51
|
18
|
|
|
18
|
|
58
|
my ($self, $tree, $hash, $node_filter, $include_trunk) = @_; |
52
|
18
|
|
|
|
|
22
|
foreach my $key (sort keys %{$hash}) { |
|
18
|
|
|
|
|
58
|
|
53
|
15
|
|
|
|
|
22
|
my $node = $key; |
54
|
15
|
100
|
|
|
|
26
|
$node = $node_filter->($node) if $node_filter; |
55
|
15
|
|
|
|
|
33
|
my $new_tree; |
56
|
15
|
100
|
|
|
|
48
|
if ($include_trunk) { |
57
|
1
|
|
|
|
|
4
|
$tree->setNodeValue($node); |
58
|
1
|
|
|
|
|
7
|
$new_tree = $tree; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
else { |
61
|
14
|
|
|
|
|
26
|
$new_tree = Tree::Simple->new($node); |
62
|
14
|
|
|
|
|
344
|
$tree->addChild($new_tree); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
$self->_buildTree($new_tree, $hash->{$key}, $node_filter) |
65
|
15
|
50
|
|
|
|
1064
|
if ref($hash->{$key}) eq 'HASH'; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
1; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
__END__ |