line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tree::Simple::Visitor::FromNestedHash; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
16507
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
31
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.15'; |
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
|
|
|
|
|
415
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
4
|
|
|
4
|
1
|
6999
|
my ($_class) = @_; |
14
|
4
|
|
33
|
|
|
18
|
my $class = ref($_class) || $_class; |
15
|
4
|
|
|
|
|
4
|
my $visitor = {}; |
16
|
4
|
|
|
|
|
5
|
bless($visitor, $class); |
17
|
4
|
|
|
|
|
9
|
$visitor->_init(); |
18
|
4
|
|
|
|
|
19
|
return $visitor; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub _init { |
22
|
4
|
|
|
4
|
|
2
|
my ($self) = @_; |
23
|
4
|
|
|
|
|
9
|
$self->{hash_tree} = undef; |
24
|
4
|
|
|
|
|
12
|
$self->SUPER::_init(); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub setHashTree { |
28
|
8
|
|
|
8
|
1
|
2739
|
my ($self, $hash_tree) = @_; |
29
|
8
|
100
|
100
|
|
|
49
|
(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
|
|
|
|
|
22
|
|
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
|
2123
|
my ($self, $tree) = @_; |
40
|
7
|
100
|
100
|
|
|
64
|
(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
|
|
|
|
|
10
|
$self->getNodeFilter(), |
46
|
|
|
|
|
|
|
$self->includeTrunk() |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _buildTree { |
51
|
18
|
|
|
18
|
|
32
|
my ($self, $tree, $hash, $node_filter, $include_trunk) = @_; |
52
|
18
|
|
|
|
|
15
|
foreach my $key (sort keys %{$hash}) { |
|
18
|
|
|
|
|
48
|
|
53
|
15
|
|
|
|
|
11
|
my $node = $key; |
54
|
15
|
100
|
|
|
|
28
|
$node = $node_filter->($node) if $node_filter; |
55
|
15
|
|
|
|
|
23
|
my $new_tree; |
56
|
15
|
100
|
|
|
|
17
|
if ($include_trunk) { |
57
|
1
|
|
|
|
|
3
|
$tree->setNodeValue($node); |
58
|
1
|
|
|
|
|
5
|
$new_tree = $tree; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
else { |
61
|
14
|
|
|
|
|
19
|
$new_tree = Tree::Simple->new($node); |
62
|
14
|
|
|
|
|
226
|
$tree->addChild($new_tree); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
$self->_buildTree($new_tree, $hash->{$key}, $node_filter) |
65
|
15
|
50
|
|
|
|
645
|
if ref($hash->{$key}) eq 'HASH'; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
1; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
__END__ |