|
NAMETree::Simple::Visitor - Visitor object for Tree::Simple objects SYNOPSIS 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
DESCRIPTIONThis 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. METHODS
CONSTANTSThese constants are part of the old-style interface, and therefore will eventually be deprecated.
BUGSNone 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> SEE ALSOI have written a set of pre-built Visitor objects, available on CPAN as Tree::Simple::VisitorFactory. AUTHORstevan little, <stevan@iinteractive.com> REPOSITORY<https://github.com/ronsavage/Tree-Simple>. COPYRIGHT AND LICENSECopyright 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.
|