Tree::Simple::Visitor - Visitor object for Tree::Simple objects |
Tree::Simple::Visitor - Visitor object for Tree::Simple objects
use Tree::Simple; use Tree::Simple::Visitor; # create a visitor instance my $visitor = Tree::Simple::Visitor->new(); # create a tree to visit my $tree = Tree::Simple->new(Tree::Simple->ROOT) ->addChildren( Tree::Simple->new("1.0"), Tree::Simple->new("2.0") ->addChild( Tree::Simple->new("2.1.0") ), Tree::Simple->new("3.0") );
# by default this will collect all the # node values in depth-first order into # our results $tree->accept($visitor); # get our results and print them print join ", ", $visitor->getResults(); # prints "1.0, 2.0, 2.1.0, 3.0" # for more complex node objects, you can specify # a node filter which will be used to extract the # information desired from each node $visitor->setNodeFilter(sub { my ($t) = @_; return $t->getNodeValue()->description(); }); # NOTE: this object has changed, but it still remains # backwards compatible to the older version, see the # DESCRIPTION section below for more details
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 Tree::Simple::VisitorFactory distribution, which includes a number of useful pre-built Visitors.
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:
my @accumulator; my $visitor = Tree::Simple::Visitor->new(sub { my ($tree) = @_; push @accumlator, $tree->getNodeValue(); }, Tree::Simple::Visitor->RECURSIVE); $tree->accept($visitor); print join ", ", @accumulator; # prints "1.0, 2.0, 2.1.0, 3.0" But is better expressed as this:
my $visitor = Tree::Simple::Visitor->new(); $tree->accept($visitor); print join ", ", $visitor->getResults(); # prints "1.0, 2.0, 2.1.0, 3.0"
This object is still pretty much a wrapper around the Tree::Simple traverse
method, and can be thought of as a depth-first traversal Visitor object.
$depth
is always assumed to be equivalent to RECURSIVE
and the $func
argument can be set with 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.
The old style constructor documentation is retained her for reference:
The first argument to the constructor is a code reference to a function which expects a 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 Tree::Simple instance. If $depth
is set to CHILDREN_ONLY
, then the function will be applied to the current Tree::Simple instance and all its immediate children. If $depth
is set to RECURSIVE
, then the function will be applied to the current Tree::Simple instance and all its immediate children, and all of their children recursively on down the tree. If no $depth
is passed to the constructor, then the function will only be applied to the current Tree::Simple object and none of its children.
$boolean
, this will tell the visitor to collect the trunk of the tree as well. It is defaulted to false (0
) in the new style interface, but is defaulted to true (1
) in the old style interface.
setNodeFilter
argument.
$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.
setResults
with no argument.
visit
method accepts a Tree::Simple and applies the function set in new
or setNodeFilter
appropriately. The results of this application can be retrieved with getResults
These constants are part of the old-style interface, and therefore will eventually be deprecated.
None that I am aware of. The code is pretty thoroughly tested (see CODE COVERAGE section in 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.
I have written a set of pre-built Visitor objects, available on CPAN as Tree::Simple::VisitorFactory.
stevan little, <stevan@iinteractive.com>
Copyright 2004-2006 by Infinity Interactive, Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Tree::Simple::Visitor - Visitor object for Tree::Simple objects |