Tree::Simple::Visitor(User Contributed Perl DocumentatTree::Simple::Visitor(3)



NAME
       Tree::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

DESCRIPTION
       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.

METHODS
       new ($func, $depth)
           The new style interface means that all arguments to the constructor
           are now optional. As a means of defining the usage of the old and
           new, when no arguments are sent to the constructor, it is assumed
           that the new style interface is being used. In the new style, the
           $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.

       includeTrunk ($boolean)
           Based upon the value of $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.

       getNodeFilter
           This method returns the CODE reference set with "setNodeFilter"
           argument.

       clearNodeFilter
           This method clears node filter field.

       setNodeFilter ($filter_function)
           This method accepts a CODE reference as its $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.

       getResults
           This method returns the accumulated results of the application of
           the node filter to the tree.

       setResults
           This method should not really be used outside of this class, as it
           just would not make any sense to. It is included in this class and
           in this documenation to facilitate subclassing of this class for
           your own needs. If you desire to clear the results, then you can
           simply call "setResults" with no argument.

       visit ($tree)
           The "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"

CONSTANTS
       These constants are part of the old-style interface, and therefore will
       eventually be deprecated.

       RECURSIVE
           If passed this constant in the constructor, the function will be
           applied recursively down the hierarchy 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.

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
       I have written a set of pre-built Visitor objects, available on CPAN as
       Tree::Simple::VisitorFactory.

AUTHOR
       stevan little, <stevan@iinteractive.com>

COPYRIGHT AND LICENSE
       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.



perl v5.12.2                      2012-08-05          Tree::Simple::Visitor(3)
