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   18177 use strict;
  1         3  
  1         25  
4 1     1   3 use warnings;
  1         1  
  1         32  
5              
6             our $VERSION = '0.14';
7              
8 1     1   4 use Scalar::Util qw(blessed);
  1         1  
  1         93  
9              
10 1     1   3 use base qw(Tree::Simple::Visitor);
  1         2  
  1         457  
11              
12             sub new {
13 1     1 1 1798 my ($_class) = @_;
14 1   33     6 my $class = ref($_class) || $_class;
15 1         2 my $visitor = {};
16 1         2 bless($visitor, $class);
17 1         13 $visitor->_init();
18 1         8 return $visitor;
19             }
20              
21             sub visit {
22 6     6 1 4162 my ($self, $tree) = @_;
23 6 100 100     62 (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         7 my $filter_function = $self->getNodeFilter();
27             # use an inner subroutine to accomplish
28             # this traversal using recursion
29             my $_postOrderTraversal = sub {
30 32     32   28 my ($current_tree, $traversal_function) = @_;
31             # get a temporary results container
32 32         16 my @results;
33             # process each child
34 32         42 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         67 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     90 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       162 push @results => (($filter_function) ?
47             $filter_function->($current_tree)
48             :
49             $current_tree->getNodeValue());
50             # and then return the results
51 31         102 return @results;
52 2         17 };
53             # now store the results in our object
54 2         4 $self->setResults($_postOrderTraversal->($tree, $_postOrderTraversal));
55             }
56              
57             1;
58              
59             __END__