File Coverage

blib/lib/Tree/Simple/Visitor/PostOrderTraversal.pm
Criterion Covered Total %
statement 30 30 100.0
branch 6 6 100.0
condition 7 9 77.7
subroutine 7 7 100.0
pod 2 2 100.0
total 52 54 96.3


line stmt bran cond sub pod time code
1             package Tree::Simple::Visitor::PostOrderTraversal;
2              
3 1     1   60031 use strict;
  1         10  
  1         25  
4 1     1   4 use warnings;
  1         2  
  1         33  
5              
6             our $VERSION = '0.16';
7              
8 1     1   5 use Scalar::Util qw(blessed);
  1         2  
  1         58  
9              
10 1     1   6 use base qw(Tree::Simple::Visitor);
  1         2  
  1         438  
11              
12             sub new {
13 1     1 1 2170 my ($_class) = @_;
14 1   33     22 my $class = ref($_class) || $_class;
15 1         3 my $visitor = {};
16 1         2 bless($visitor, $class);
17 1         7 $visitor->_init();
18 1         11 return $visitor;
19             }
20              
21             sub visit {
22 6     6 1 3680 my ($self, $tree) = @_;
23 6 100 100     70 (blessed($tree) && $tree->isa("Tree::Simple"))
24             || die "Insufficient Arguments : You must supply a valid Tree::Simple object";
25             # get our filter function
26 2         21 my $filter_function = $self->getNodeFilter();
27             # use an inner subroutine to accomplish
28             # this traversal using recursion
29             my $_postOrderTraversal = sub {
30 32     32   47 my ($current_tree, $traversal_function) = @_;
31             # get a temporary results container
32 32         34 my @results;
33             # process each child
34 32         46 foreach my $child ($current_tree->getAllChildren()) {
35             # recurse our inner subroutine by passing itself
36             # to itself, and then collect the results of this
37             # recursion
38 30         112 push @results => $traversal_function->($child, $traversal_function);
39             }
40             # if we are root and we are not including the trunk then
41             # we can return our results now
42 32 100 100     124 return @results if $current_tree->isRoot() && !$self->includeTrunk();
43             # however, if we don't meet those conditions, then we
44             # need to process the current tree and add it to our
45             # results
46 31 100       192 push @results => (($filter_function) ?
47             $filter_function->($current_tree)
48             :
49             $current_tree->getNodeValue());
50             # and then return the results
51 31         195 return @results;
52 2         16 };
53             # now store the results in our object
54 2         5 $self->setResults($_postOrderTraversal->($tree, $_postOrderTraversal));
55             }
56              
57             1;
58              
59             __END__