2 package Tree::Simple::Visitor;
9 use Scalar::Util qw(blessed);
13 use constant RECURSIVE => 0x01;
14 use constant CHILDREN_ONLY => 0x10;
19 my ($_class, $func, $depth) = @_;
21 ($depth =~ /\d+/ && ($depth == RECURSIVE || $depth == CHILDREN_ONLY))
22 || die "Insufficient Arguments : Depth arguement must be either RECURSIVE or CHILDREN_ONLY";
24 my $class = ref($_class) || $_class;
25 # if we have not supplied a $func
26 # it is automatically RECURSIVE
27 $depth = RECURSIVE unless defined $func;
31 bless($visitor, $class);
34 $visitor->setNodeFilter($func);
35 $visitor->includeTrunk(1);
44 $self->{_include_trunk} = 0;
45 $self->{_filter_function} = undef;
46 $self->{_results} = [];
50 my ($self, $boolean) = @_;
51 $self->{_include_trunk} = ($boolean ? 1 : 0) if defined $boolean;
52 return $self->{_include_trunk};
59 return $self->{_filter_function};
64 $self->{_filter_function} = undef;
68 my ($self, $filter_function) = @_;
69 (defined($filter_function) && ref($filter_function) eq "CODE")
70 || die "Insufficient Arguments : filter function argument must be a subroutine reference";
71 $self->{_filter_function} = $filter_function;
77 my ($self, @results) = @_;
78 $self->{results} = \@results;
91 my ($self, $tree) = @_;
92 (blessed($tree) && $tree->isa("Tree::Simple"))
93 || die "Insufficient Arguments : You must supply a valid Tree::Simple object";
94 # get all things set up
97 if ($self->{_filter_function}) {
98 $func = sub { push @results => $self->{_filter_function}->(@_) };
101 $func = sub { push @results => $_[0]->getNodeValue() };
103 # always apply the function
105 $func->($tree) unless defined $self->{_include_trunk};
106 # then recursively to all its children
107 # if the object is configured that way
108 $tree->traverse($func) if ($self->{depth} == RECURSIVE);
109 # or just visit its immediate children
110 # if the object is configured that way
111 if ($self->{depth} == CHILDREN_ONLY) {
112 $func->($_) foreach $tree->getAllChildren();
114 # now store the results we got
115 $self->setResults(@results);
125 Tree::Simple::Visitor - Visitor object for Tree::Simple objects
130 use Tree::Simple::Visitor;
132 # create a visitor instance
133 my $visitor = Tree::Simple::Visitor->new();
135 # create a tree to visit
136 my $tree = Tree::Simple->new(Tree::Simple->ROOT)
138 Tree::Simple->new("1.0"),
139 Tree::Simple->new("2.0")
141 Tree::Simple->new("2.1.0")
143 Tree::Simple->new("3.0")
146 # by default this will collect all the
147 # node values in depth-first order into
149 $tree->accept($visitor);
151 # get our results and print them
152 print join ", ", $visitor->getResults(); # prints "1.0, 2.0, 2.1.0, 3.0"
154 # for more complex node objects, you can specify
155 # a node filter which will be used to extract the
156 # information desired from each node
157 $visitor->setNodeFilter(sub {
159 return $t->getNodeValue()->description();
162 # NOTE: this object has changed, but it still remains
163 # backwards compatible to the older version, see the
164 # DESCRIPTION section below for more details
168 This object has been revised into what I think is more intelligent approach to Visitor objects. This is now a more suitable base class for building your own Visitors. It is also the base class for the visitors found in the B<Tree::Simple::VisitorFactory> distribution, which includes a number of useful pre-built Visitors.
170 While I have changed a number of things about this module, I have kept it backwards compatible to the old way of using it. So the original example code still works:
173 my $visitor = Tree::Simple::Visitor->new(sub {
175 push @accumlator, $tree->getNodeValue();
177 Tree::Simple::Visitor->RECURSIVE);
179 $tree->accept($visitor);
181 print join ", ", @accumulator; # prints "1.0, 2.0, 2.1.0, 3.0"
183 But is better expressed as this:
185 my $visitor = Tree::Simple::Visitor->new();
186 $tree->accept($visitor);
187 print join ", ", $visitor->getResults(); # prints "1.0, 2.0, 2.1.0, 3.0"
189 This object is still pretty much a wrapper around the Tree::Simple C<traverse> method, and can be thought of as a depth-first traversal Visitor object.
195 =item B<new ($func, $depth)>
197 The new style interface means that all arguments to the constructor are now optional. As a means of defining the usage of the old and new, when no arguments are sent to the constructor, it is assumed that the new style interface is being used. In the new style, the C<$depth> is always assumed to be equivalent to C<RECURSIVE> and the C<$func> argument can be set with C<setNodeFilter> instead. This is the recommended way of doing things now. If you have been using the old way, it is still there, and I will maintain backwards compatability for a few more version before removing it entirely. If you are using this module (and I don't even know if anyone actually is) you have been warned. Please contact me if this will be a problem.
199 The old style constructor documentation is retained her for reference:
201 The first argument to the constructor is a code reference to a function which expects a B<Tree::Simple> object as its only argument. The second argument is optional, it can be used to set the depth to which the function is applied. If no depth is set, the function is applied to the current B<Tree::Simple> instance. If C<$depth> is set to C<CHILDREN_ONLY>, then the function will be applied to the current B<Tree::Simple> instance and all its immediate children. If C<$depth> is set to C<RECURSIVE>, then the function will be applied to the current B<Tree::Simple> instance and all its immediate children, and all of their children recursively on down the tree. If no C<$depth> is passed to the constructor, then the function will only be applied to the current B<Tree::Simple> object and none of its children.
203 =item B<includeTrunk ($boolean)>
205 Based upon the value of C<$boolean>, this will tell the visitor to collect the trunk of the tree as well. It is defaulted to false (C<0>) in the new style interface, but is defaulted to true (C<1>) in the old style interface.
207 =item B<getNodeFilter>
209 This method returns the CODE reference set with C<setNodeFilter> argument.
211 =item B<clearNodeFilter>
213 This method clears node filter field.
215 =item B<setNodeFilter ($filter_function)>
217 This method accepts a CODE reference as its C<$filter_function> argument. 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.
221 This method returns the accumulated results of the application of the node filter to the tree.
225 This method should not really be used outside of this class, as it just would not make any sense to. It is included in this class and in this documenation to facilitate subclassing of this class for your own needs. If you desire to clear the results, then you can simply call C<setResults> with no argument.
227 =item B<visit ($tree)>
229 The C<visit> method accepts a B<Tree::Simple> and applies the function set in C<new> or C<setNodeFilter> appropriately. The results of this application can be retrieved with C<getResults>
235 These constants are part of the old-style interface, and therefore will eventually be deprecated.
241 If passed this constant in the constructor, the function will be applied recursively down the hierarchy of B<Tree::Simple> objects.
243 =item B<CHILDREN_ONLY>
245 If passed this constant in the constructor, the function will be applied to the immediate children of the B<Tree::Simple> object.
251 None that I am aware of. The code is pretty thoroughly tested (see B<CODE COVERAGE> section in B<Tree::Simple>) and is based on an (non-publicly released) module which I had used in production systems for about 2 years without incident. Of course, if you find a bug, let me know, and I will be sure to fix it.
255 I have written a set of pre-built Visitor objects, available on CPAN as B<Tree::Simple::VisitorFactory>.
259 stevan little, E<lt>stevan@iinteractive.comE<gt>
261 =head1 COPYRIGHT AND LICENSE
263 Copyright 2004-2006 by Infinity Interactive, Inc.
265 L<http://www.iinteractive.com>
267 This library is free software; you can redistribute it and/or modify
268 it under the same terms as Perl itself.