| 1 | package Tree::Simple::Visitor::ToNestedHash; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.16'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use base qw(Tree::Simple::Visitor); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 5 | my ($_class) = @_; | |||||||||||||||||||||||||||
| 14 | 5 | ▶ | my $class = ref($_class) || $_class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $_class || $_class
MC/DC0%0/2
ref $_class || $_class
ref $_class
$_class
Truth table33%1/3
A || B
A= ref $_classB= $_class
| |||||||||||||||||||||||||||||
| 15 | 5 | my $visitor = {}; | |||||||||||||||||||||||||||
| 16 | 5 | bless($visitor, $class); | |||||||||||||||||||||||||||
| 17 | 5 | $visitor->_init(); | |||||||||||||||||||||||||||
| 18 | 5 | return $visitor; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 8 | ▶ | sub visit { | ||||||||||||||||||||||||||
|
Subroutine
visit: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 8 | my ($self, $tree) = @_; | |||||||||||||||||||||||||||
| 23 | 8 | ▶ | (blessed($tree) && $tree->isa("Tree::Simple")) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($tree) and $tree->isa("Tree::Simple")
MC/DC100%2/2
builtin::blessed($tree) and $tree->isa("Tree::Simple")
builtin::blessed($tree)
$tree->isa("Tree::Simple")
Truth table100%3/3
A and B
A= builtin::blessed($tree)B= $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 | my $filter = $self->getNodeFilter(); | |||||||||||||||||||||||||||
| 27 | 4 | my %results; | |||||||||||||||||||||||||||
| 28 | # get the array | ||||||||||||||||||||||||||||
| 29 | 4 | $self->_buildHash($tree, \%results, $filter); | |||||||||||||||||||||||||||
| 30 | # add the trunk if we need to | ||||||||||||||||||||||||||||
| 31 | 4 | ▶ | %results = ( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 32 | ((defined($filter)) ? | ||||||||||||||||||||||||||||
| 33 | $filter->($tree) | ||||||||||||||||||||||||||||
| 34 | : | ||||||||||||||||||||||||||||
| 35 | $tree->getNodeValue()) => { %results } | ||||||||||||||||||||||||||||
| 36 | ) if $self->includeTrunk(); | ||||||||||||||||||||||||||||
| 37 | # set results | ||||||||||||||||||||||||||||
| 38 | 4 | $self->setResults(\%results); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 8 | ▶ | sub _buildHash { | ||||||||||||||||||||||||||
|
Subroutine
_buildHash: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 42 | 8 | my ($self, $tree, $accumulator, $filter) = @_; | |||||||||||||||||||||||||||
| 43 | 8 | foreach my $child ($tree->getAllChildren()) { | |||||||||||||||||||||||||||
| 44 | 16 | my $node_value = {}; | |||||||||||||||||||||||||||
| 45 | 16 | ▶ | my $node_key = (defined($filter) ? $filter->($child) : $child->getNodeValue()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 16 | ▶ | $self->_buildHash($child, $node_value, $filter) unless $child->isLeaf(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 16 | $accumulator->{$node_key} = $node_value; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 8 | return $accumulator; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | 1; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | __END__ | ||||||||||||||||||||||||||||