Tree::Simple::Visitor(3pm) | User Contributed Perl Documentation | Tree::Simple::Visitor(3pm) |
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 @accumulator, $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.
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.
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.
Bugs should be reported via the CPAN bug tracker at
<https://github.com/ronsavage/Tree-Simple/issues>
I have written a set of pre-built Visitor objects, available on CPAN as Tree::Simple::VisitorFactory.
stevan little, <stevan@iinteractive.com>
<https://github.com/ronsavage/Tree-Simple>.
Copyright 2004-2006 by Infinity Interactive, Inc.
<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2022-10-13 | perl v5.34.0 |