| 1 | package Tree::Simple::Visitor::FindByPath; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.16'; | |||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use base qw(Tree::Simple::Visitor); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | my ($_class) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | 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 | 2 | my $visitor = {}; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | bless($visitor, $class); | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | $visitor->_init(); | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | return $visitor; | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | ▶ | sub _init { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | $self->{search_path} = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | $self->{success} = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | $self->SUPER::_init(); | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 8 | ▶ | sub setSearchPath { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
setSearchPath: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 8 | my ($self, @path) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | 8 | ▶ | (@path) || die "Insufficient Arguments : You must specify a path"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 7 | $self->{search_path} = \@path; | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 11 | ▶ | sub visit { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 11 | my ($self, $tree) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | 11 | ▶ | (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")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | || die "Insufficient Arguments : You must supply a valid Tree::Simple object"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # reset our success flag | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 7 | $self->{success} = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # get our filter function | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 7 | my $func; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | 7 | ▶ | if ($self->{_filter_function}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 12 | ▶ | $func = sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 12 | my ($tree, $test) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | 12 | return (($self->{_filter_function}->($tree) . "") eq $test); | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 18 | ▶ | $func = sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 18 | my ($tree, $test) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | 18 | return (($tree->getNodeValue() . "") eq $test); | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # get ready with our results | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 7 | my @results; | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # get our path | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 7 | my @path = @{$self->{search_path}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # get our variables ready | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 7 | my $current_path; | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | 7 | my $current_tree = $tree; | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # check to see if we have been | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # asked to include the trunk | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 7 | ▶ | if ($self->includeTrunk()) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # if we don't match the root of the path | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # then we have failed already and so return | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | ▶ | $self->setResults(()) && return | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
&$func($current_tree, $path[0]) || $self->setResults(()) && (return)
$self->setResults(()) and return
MC/DC0%0/2
&$func($current_tree, $path[0]) || $self->setResults(()) && (return)
&$func($current_tree, $path[0])
$self->setResults(())
Truth table66%2/3
A || B
A= &$func($current_tree, $path[0])B= $self->setResults(())
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | unless $func->($current_tree, $path[0]); | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # if we do match, then remove it off the path | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | shift @path; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | TOP: { | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # if we have no more @path we have found it | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 16 | ▶ | unless (@path) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # store the current tree as | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # our last result | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | $self->setResults(@results, $current_tree); | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | # and set the success flag | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | $self->{success} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # otherwise we need to keep looking ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # get the next element in the path | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 14 | $current_path = shift @path; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | # now check all the current tree's children | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # for a match | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 14 | foreach my $child ($current_tree->getAllChildren()) { | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | 28 | ▶ | if ($func->($child, $current_path)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # if we find a match, then | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # we store the current tree | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # in our results, and | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 10 | push @results => $current_tree; | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | # we change our current tree | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 10 | $current_tree = $child; | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | # and go back to the TOP | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 10 | goto TOP; | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | # if we do not find a match, then we can fall off | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # this block and the whole subroutine for that matter | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | # since we know the match has failed. | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | push @results => $current_tree | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | ▶ | if (@path || $self->{success} == 0) && $current_tree != $tree; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
@path || $self->{"success"} == 0 and $current_tree != $tree
@path or $self->{"success"} == 0
MC/DC33%1/3
@path || $self->{"success"} == 0 and $current_tree != $tree
@path
$self->{"success"} == 0
$current_tree != $tree
Truth table60%3/5
A or B and C
A= @pathB= $self->{"success"} == 0C= $current_tree != $tree
| |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # we do however, store the | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | # results as far as we got, | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # so that the user can maybe | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # do something else to recover | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 4 | $self->setResults(@results); | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 7 | ▶ | sub getResult { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
getResult: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | # if we did not succeed, then | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | # we return undef, ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 7 | ▶ | return undef unless $self->{success}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | # otherwise we return the | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # last in the results | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | return $self->getResults()->[-1]; | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||