| 1 | package Tree::Simple::Visitor::FindByNodeValue; | ||||||||||||||||||||||||||||
| 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 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 7 | my ($_class) = @_; | |||||||||||||||||||||||||||
| 14 | 7 | ▶ | 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 | 7 | my $visitor = {}; | |||||||||||||||||||||||||||
| 16 | 7 | bless($visitor, $class); | |||||||||||||||||||||||||||
| 17 | 7 | $visitor->_init(); | |||||||||||||||||||||||||||
| 18 | 7 | return $visitor; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 7 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 23 | 7 | $self->{success} = 0; | |||||||||||||||||||||||||||
| 24 | 7 | $self->{node_value_to_find} = undef; | |||||||||||||||||||||||||||
| 25 | 7 | $self->SUPER::_init(); | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 9 | ▶ | sub searchForNodeValue { | ||||||||||||||||||||||||||
|
Subroutine
searchForNodeValue: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 9 | my ($self, $node_value) = @_; | |||||||||||||||||||||||||||
| 30 | 9 | ▶ | (defined($node_value)) || die "Insufficient Arguments : You must provide a node value to search for"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 7 | $self->{node_value_to_find} = $node_value; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 6 | ▶ | sub setTraversalMethod { | ||||||||||||||||||||||||||
|
Subroutine
setTraversalMethod: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 6 | my ($self, $visitor) = @_; | |||||||||||||||||||||||||||
| 36 | 6 | ▶ | (blessed($visitor) && $visitor->isa("Tree::Simple::Visitor")) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($visitor) and $visitor->isa("Tree::Simple::Visitor")
MC/DC100%2/2
builtin::blessed($visitor) and $visitor->isa("Tree::Simple::Visitor")
builtin::blessed($visitor)
$visitor->isa("Tree::Simple::Visitor")
Truth table100%3/3
A and B
A= builtin::blessed($visitor)B= $visitor->isa("Tree::Simple::Visitor")
| |||||||||||||||||||||||||||||
| 37 | || die "Insufficient Arguments : You must supply a valid Tree::Simple::Visitor object"; | ||||||||||||||||||||||||||||
| 38 | 2 | $self->{traversal_method} = $visitor; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 13 | ▶ | sub visit { | ||||||||||||||||||||||||||
|
Subroutine
visit: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 13 | my ($self, $tree) = @_; | |||||||||||||||||||||||||||
| 43 | 13 | ▶ | (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")
| |||||||||||||||||||||||||||||
| 44 | || die "Insufficient Arguments : You must supply a valid Tree::Simple object"; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # reset our success flag | ||||||||||||||||||||||||||||
| 47 | 9 | $self->{success} = 0; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 9 | my $node_value = $self->{node_value_to_find}; | |||||||||||||||||||||||||||
| 50 | 9 | ▶ | (defined($node_value)) || die "Illegal Operation : You cannot search for a node_value without setting one first"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | # create our filter function | ||||||||||||||||||||||||||||
| 52 | # NOTE: | ||||||||||||||||||||||||||||
| 53 | # in order to get an immediate exit | ||||||||||||||||||||||||||||
| 54 | # from the traversal once a match is | ||||||||||||||||||||||||||||
| 55 | # found, we use 'die'. It is a somewhat | ||||||||||||||||||||||||||||
| 56 | # unorthodox way of using this, but it | ||||||||||||||||||||||||||||
| 57 | # works. The found tree is propagated | ||||||||||||||||||||||||||||
| 58 | # up the call chain and returned from | ||||||||||||||||||||||||||||
| 59 | # this function. | ||||||||||||||||||||||||||||
| 60 | 8 | my $func; | |||||||||||||||||||||||||||
| 61 | 8 | ▶ | if ($self->{_filter_function}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 39 | ▶ | $func = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 63 | 39 | my ($tree, $test) = @_; | |||||||||||||||||||||||||||
| 64 | 39 | ▶ | (($tree->getNodeValue() eq $node_value) && $self->{_filter_function}->($tree)) && die $tree; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$tree->getNodeValue eq $node_value and $self->{"_filter_function"}->($tree)
MC/DC100%2/2
$tree->getNodeValue eq $node_value and $self->{"_filter_function"}->($tree)
$tree->getNodeValue eq $node_value
$self->{"_filter_function"}->($tree)
Truth table100%3/3
A and B
A= $tree->getNodeValue eq $node_valueB= $self->{"_filter_function"}->($tree)
| |||||||||||||||||||||||||||||
| 65 | 4 | }; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | else { | ||||||||||||||||||||||||||||
| 68 | 22 | ▶ | $func = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 69 | 22 | my ($tree, $test) = @_; | |||||||||||||||||||||||||||
| 70 | 22 | ▶ | ($tree->getNodeValue() eq $node_value) && die $tree; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 4 | }; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # we eval this so we can catch the tree | ||||||||||||||||||||||||||||
| 75 | # match when it is thrown with 'die' | ||||||||||||||||||||||||||||
| 76 | 8 | eval { | |||||||||||||||||||||||||||
| 77 | 8 | ▶ | unless (defined($self->{traversal_method})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | # include the trunk in our | ||||||||||||||||||||||||||||
| 79 | # search if needed | ||||||||||||||||||||||||||||
| 80 | 6 | ▶ | $func->($tree) if $self->includeTrunk(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | # and traverse | ||||||||||||||||||||||||||||
| 82 | 5 | $tree->traverse($func); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | else { | ||||||||||||||||||||||||||||
| 85 | # include the trunk in our | ||||||||||||||||||||||||||||
| 86 | # search if needed | ||||||||||||||||||||||||||||
| 87 | 2 | ▶ | $self->{traversal_method}->includeTrunk(1) if $self->includeTrunk(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | # and visit | ||||||||||||||||||||||||||||
| 89 | 2 | $self->{traversal_method}->setNodeFilter($func); | |||||||||||||||||||||||||||
| 90 | 2 | $self->{traversal_method}->visit($tree); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | }; | ||||||||||||||||||||||||||||
| 93 | # now see what we have ... | ||||||||||||||||||||||||||||
| 94 | 8 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | # if we caught a Tree::Simple object | ||||||||||||||||||||||||||||
| 96 | # then we have found a match, and ... | ||||||||||||||||||||||||||||
| 97 | 7 | ▶ | if (blessed($@) && $@->isa('Tree::Simple')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($@) and $@->isa("Tree::Simple")
MC/DC100%2/2
builtin::blessed($@) and $@->isa("Tree::Simple")
builtin::blessed($@)
$@->isa("Tree::Simple")
Truth table100%3/3
A and B
A= builtin::blessed($@)B= $@->isa("Tree::Simple")
| |||||||||||||||||||||||||||||
| 98 | # we assign it to our results | ||||||||||||||||||||||||||||
| 99 | 5 | $self->setResults($@); | |||||||||||||||||||||||||||
| 100 | 5 | $self->{success} = 1; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | # however, if it is not a Tree::Simple | ||||||||||||||||||||||||||||
| 103 | # object then it is likely a real exception | ||||||||||||||||||||||||||||
| 104 | else { | ||||||||||||||||||||||||||||
| 105 | # so we re-throw it | ||||||||||||||||||||||||||||
| 106 | 2 | die $@; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | else { | ||||||||||||||||||||||||||||
| 110 | # if no exception is thrown though, | ||||||||||||||||||||||||||||
| 111 | # we failed in our search, and so we | ||||||||||||||||||||||||||||
| 112 | # set our success flag to false | ||||||||||||||||||||||||||||
| 113 | 1 | $self->{success} = 0; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 6 | ▶ | sub getResult { | ||||||||||||||||||||||||||
|
Subroutine
getResult: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 119 | # if we did not succeed, then | ||||||||||||||||||||||||||||
| 120 | # we return undef, ... | ||||||||||||||||||||||||||||
| 121 | 6 | ▶ | return undef unless $self->{success}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | # otherwise we return the results | ||||||||||||||||||||||||||||
| 123 | 5 | return $self->getResults()->[0]; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1 | 1; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||||||||||||