2 package Tree::Simple::Visitor::PathToRoot;
9 use Scalar::Util qw(blessed);
11 use base qw(Tree::Simple::Visitor);
15 my $class = ref($_class) || $_class;
17 bless($visitor, $class);
23 my ($self, $tree) = @_;
24 (blessed($tree) && $tree->isa("Tree::Simple"))
25 || die "Insufficient Arguments : You must supply a valid Tree::Simple object";
26 # create an array for our path
28 # we need to climb up the tree and
30 my $filter_function = $self->getNodeFilter();
31 my $current_tree = $tree;
32 until ($current_tree->isRoot()) {
33 unshift @path => ($filter_function ?
34 $filter_function->($current_tree)
36 $current_tree->getNodeValue());
37 $current_tree = $current_tree->getParent();
39 # now grab the trunk if specified
40 unshift @path => ($filter_function ?
41 $filter_function->($current_tree)
43 $current_tree->getNodeValue()) if $self->includeTrunk();
44 # now store our path in results
45 $self->setResults(@path);
50 return $self->getResults();
54 my ($self, $delimiter) = @_;
56 return join $delimiter => $self->getResults();
65 Tree::Simple::Visitor::PathToRoot - A Visitor for finding the path back a Tree::Simple object's root
69 use Tree::Simple::Visitor::PathToRoot;
71 # create an instance of our visitor
72 my $visitor = Tree::Simple::Visitor::PathToRoot->new();
74 # pass the visitor to a Tree::Simple object
75 $tree->accept($visitor);
77 # now get the accumulated path as a string
78 # with the '/' character as the delimiter
79 print $visitor->getPathAsString("/");
81 # include the tree's trunk in your
83 $visitor->includeTrunk();
85 # for more complex node objects, you can specify
86 # a node filter which will be used to extract the
87 # information desired from each node
88 $visitor->setNodeFilter(sub {
90 return $t->getNodeValue()->description();
93 # you can also get the path back as an array
94 my @path = $visitor->getPath();
98 Given a Tree::Simple object, this Visitor will find the path back to the tree's root node.
106 There are no arguments to the constructor the object will be in its default state. You can use the C<includeTrunk> and C<setNodeFilter> methods to customize its behavior.
108 =item B<includeTrunk ($boolean)>
110 Based upon the value of C<$boolean>, this will tell the visitor to collect the trunk of the tree as well.
112 =item B<setNodeFilter ($filter_function)>
114 This method accepts a CODE reference as its C<$filter_function> argument and throws an exception if it is not a code reference. This code reference is used to filter the tree nodes as they are collected. This can be used to customize output, or to gather specific information from a more complex tree node. The filter function should accept a single argument, which is the current Tree::Simple object.
116 =item B<visit ($tree)>
118 This is the method that is used by Tree::Simple's C<accept> method. It can also be used on its own, it requires the C<$tree> argument to be a Tree::Simple object (or derived from a Tree::Simple object), and will throw and exception otherwise.
122 This will return the collected path as an array, or in scalar context, as an array reference.
124 =item B<getPathAsString ($delimiter)>
126 This will return the collected path as a string with the path elements joined by a C<$delimiter>. If no C<$delimiter> is specified, the default (', ') will be used.
132 None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it.
136 See the B<CODE COVERAGE> section in L<Tree::Simple::VisitorFactory> for more inforamtion.
140 These Visitor classes are all subclasses of B<Tree::Simple::Visitor>, which can be found in the B<Tree::Simple> module, you should refer to that module for more information.
144 stevan little, E<lt>stevan@iinteractive.comE<gt>
146 =head1 COPYRIGHT AND LICENSE
148 Copyright 2004, 2005 by Infinity Interactive, Inc.
150 L<http://www.iinteractive.com>
152 This library is free software; you can redistribute it and/or modify
153 it under the same terms as Perl itself.