| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Tree::Simple::Visitor::PostOrderTraversal; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 16349 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 4 | 1 |  |  | 1 |  | 3 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 31 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = '0.15'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 3 | use Scalar::Util qw(blessed); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 87 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 1 |  |  | 1 |  | 3 | use base qw(Tree::Simple::Visitor); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 432 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub new { | 
| 13 | 1 |  |  | 1 | 1 | 1776 | my ($_class) = @_; | 
| 14 | 1 |  | 33 |  |  | 6 | my $class = ref($_class) || $_class; | 
| 15 | 1 |  |  |  |  | 2 | my $visitor = {}; | 
| 16 | 1 |  |  |  |  | 1 | bless($visitor, $class); | 
| 17 | 1 |  |  |  |  | 12 | $visitor->_init(); | 
| 18 | 1 |  |  |  |  | 8 | return $visitor; | 
| 19 |  |  |  |  |  |  | } | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | sub visit { | 
| 22 | 6 |  |  | 6 | 1 | 2857 | my ($self, $tree) = @_; | 
| 23 | 6 | 100 | 100 |  |  | 59 | (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 |  |  |  |  | 10 | my $filter_function = $self->getNodeFilter(); | 
| 27 |  |  |  |  |  |  | # use an inner subroutine to accomplish | 
| 28 |  |  |  |  |  |  | # this traversal using recursion | 
| 29 |  |  |  |  |  |  | my $_postOrderTraversal = sub { | 
| 30 | 32 |  |  | 32 |  | 17 | my ($current_tree, $traversal_function) = @_; | 
| 31 |  |  |  |  |  |  | # get a temporary results container | 
| 32 | 32 |  |  |  |  | 21 | my @results; | 
| 33 |  |  |  |  |  |  | # process each child | 
| 34 | 32 |  |  |  |  | 35 | 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 |  |  |  |  | 82 | 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 |  |  | 80 | 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 |  |  |  | 159 | push @results => (($filter_function) ? | 
| 47 |  |  |  |  |  |  | $filter_function->($current_tree) | 
| 48 |  |  |  |  |  |  | : | 
| 49 |  |  |  |  |  |  | $current_tree->getNodeValue()); | 
| 50 |  |  |  |  |  |  | # and then return the results | 
| 51 | 31 |  |  |  |  | 109 | return @results; | 
| 52 | 2 |  |  |  |  | 12 | }; | 
| 53 |  |  |  |  |  |  | # now store the results in our object | 
| 54 | 2 |  |  |  |  | 4 | $self->setResults($_postOrderTraversal->($tree, $_postOrderTraversal)); | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | 1; | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | __END__ |