NAME
Tree::Simple::Visitor - Visitor object for Tree::Simple objects
SYNOPSIS
use Tree::Simple::Visitor;
use Tree::Simple;
# create an array here, it will valid within the
# the closure of the subroutine below
my @accumulator;
# create a visitor with a subroutine and tell it
# what depth we want to do too (RECURSIVE)
my $visitor = Tree::Simple::Visitor->new(sub {
my ($tree) = @_;
push @accumlator, $tree->getNodeValue();
},
Tree::Simple::Visitor->RECURSIVE);
# 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")
);
# now pass the visitor to the tree
$tree->accept($visitor);
# now the @accumulator will have all the nodes in it
print join ", ", @accumulator; # prints "1.0, 2.0, 2.1.0, 3.0"
DESCRIPTION
This is a very basic Visitor object for Tree::Simple objects. It is really just an OO wrapper around the traverse
method of the Tree::Simple object.
I consider this module to be production stable, it is based on a module which has been in use on a few production systems for approx. 2 years now with no issue. The only difference is that the code has been cleaned up a bit, comments added and thorough tests written for its public release. I comment on this more in the DESCRIPTION section in Tree::Simple.
CONSTANTS
- RECURSIVE
-
If passed this constant in the constructor, the function will be applied recursively down the heirarchy of Tree::Simple objects.
- CHILDREN_ONLY
-
If passed this constant in the constructor, the function will be applied to the immediate children of the Tree::Simple object.
NOTE:
If not constant is passed to the constructor, then the function will only be applied to the current Tree::Simple object and none of its children.
METHODS
- new (CODE, $depth)
-
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 toCHILDREN_ONLY
, then the function will be applied to the current Tree::Simple instance and all its immediate children. If$depth
is set toRECURSIVE
, 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. - visit ($tree)
-
The
visit
method accepts a Tree::Simple and applies the function set innew
appropriately.
BUGS
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.
SEE ALSO
Tree::Simple
Design Patterns by the Gang Of Four. Specifically the Visitor Pattern.
AUTHOR
stevan little, <stevan@iinteractive.com>
COPYRIGHT AND LICENSE
Copyright 2004 by Infinity Interactive, Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.