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
# be in 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.
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.
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.
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.