line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tree::Simple::Visitor::FromNestedHash; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
26975
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
40
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
60
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.14'; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
7
|
use Scalar::Util qw(blessed); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
157
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
8
|
use base qw(Tree::Simple::Visitor); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
708
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
4
|
|
|
4
|
1
|
7368
|
my ($_class) = @_; |
14
|
4
|
|
33
|
|
|
27
|
my $class = ref($_class) || $_class; |
15
|
4
|
|
|
|
|
7
|
my $visitor = {}; |
16
|
4
|
|
|
|
|
7
|
bless($visitor, $class); |
17
|
4
|
|
|
|
|
12
|
$visitor->_init(); |
18
|
4
|
|
|
|
|
29
|
return $visitor; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub _init { |
22
|
4
|
|
|
4
|
|
6
|
my ($self) = @_; |
23
|
4
|
|
|
|
|
14
|
$self->{hash_tree} = undef; |
24
|
4
|
|
|
|
|
16
|
$self->SUPER::_init(); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub setHashTree { |
28
|
8
|
|
|
8
|
1
|
2805
|
my ($self, $hash_tree) = @_; |
29
|
8
|
100
|
100
|
|
|
60
|
(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
|
|
|
|
6
|
(scalar keys %{$hash_tree} == 1) |
|
5
|
|
|
|
|
31
|
|
34
|
|
|
|
|
|
|
|| die "Insufficient Arguments : The hash tree provided must be a single rooted tree"; |
35
|
3
|
|
|
|
|
8
|
$self->{hash_tree} = $hash_tree; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub visit { |
39
|
7
|
|
|
7
|
1
|
2254
|
my ($self, $tree) = @_; |
40
|
7
|
100
|
100
|
|
|
78
|
(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
|
|
|
|
|
15
|
$self->getNodeFilter(), |
46
|
|
|
|
|
|
|
$self->includeTrunk() |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _buildTree { |
51
|
18
|
|
|
18
|
|
44
|
my ($self, $tree, $hash, $node_filter, $include_trunk) = @_; |
52
|
18
|
|
|
|
|
14
|
foreach my $key (sort keys %{$hash}) { |
|
18
|
|
|
|
|
65
|
|
53
|
15
|
|
|
|
|
15
|
my $node = $key; |
54
|
15
|
100
|
|
|
|
36
|
$node = $node_filter->($node) if $node_filter; |
55
|
15
|
|
|
|
|
19
|
my $new_tree; |
56
|
15
|
100
|
|
|
|
21
|
if ($include_trunk) { |
57
|
1
|
|
|
|
|
5
|
$tree->setNodeValue($node); |
58
|
1
|
|
|
|
|
5
|
$new_tree = $tree; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
else { |
61
|
14
|
|
|
|
|
22
|
$new_tree = Tree::Simple->new($node); |
62
|
14
|
|
|
|
|
242
|
$tree->addChild($new_tree); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
$self->_buildTree($new_tree, $hash->{$key}, $node_filter) |
65
|
15
|
50
|
|
|
|
752
|
if ref($hash->{$key}) eq 'HASH'; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
1; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
__END__ |