1Tree::Simple::Visitor(3U)ser Contributed Perl DocumentatiTornee::Simple::Visitor(3)
2
3
4

NAME

6       Tree::Simple::Visitor - Visitor object for Tree::Simple objects
7

SYNOPSIS

9         use Tree::Simple;
10         use Tree::Simple::Visitor;
11
12         # create a visitor instance
13         my $visitor = Tree::Simple::Visitor->new();
14
15         # create a tree to visit
16         my $tree = Tree::Simple->new(Tree::Simple->ROOT)
17                                ->addChildren(
18                                    Tree::Simple->new("1.0"),
19                                    Tree::Simple->new("2.0")
20                                                ->addChild(
21                                                    Tree::Simple->new("2.1.0")
22                                                    ),
23                                    Tree::Simple->new("3.0")
24                                    );
25
26         # by default this will collect all the
27         # node values in depth-first order into
28         # our results
29         $tree->accept($visitor);
30
31         # get our results and print them
32         print join ", ", $visitor->getResults();  # prints "1.0, 2.0, 2.1.0, 3.0"
33
34         # for more complex node objects, you can specify
35         # a node filter which will be used to extract the
36         # information desired from each node
37         $visitor->setNodeFilter(sub {
38                       my ($t) = @_;
39                       return $t->getNodeValue()->description();
40                       });
41
42         # NOTE: this object has changed, but it still remains
43         # backwards compatible to the older version, see the
44         # DESCRIPTION section below for more details
45

DESCRIPTION

47       This object has been revised into what I think is more intelligent
48       approach to Visitor objects.  This is now a more suitable base class
49       for building your own Visitors. It is also the base class for the
50       visitors found in the Tree::Simple::VisitorFactory distribution, which
51       includes a number of useful pre-built Visitors.
52
53       While I have changed a number of things about this module, I have kept
54       it backwards compatible to the old way of using it. So the original
55       example code still works:
56
57         my @accumulator;
58         my $visitor = Tree::Simple::Visitor->new(sub {
59                               my ($tree) = @_;
60                               push @accumulator, $tree->getNodeValue();
61                               },
62                               Tree::Simple::Visitor->RECURSIVE);
63
64         $tree->accept($visitor);
65
66         print join ", ", @accumulator;  # prints "1.0, 2.0, 2.1.0, 3.0"
67
68       But is better expressed as this:
69
70         my $visitor = Tree::Simple::Visitor->new();
71         $tree->accept($visitor);
72         print join ", ", $visitor->getResults();  # prints "1.0, 2.0, 2.1.0, 3.0"
73
74       This object is still pretty much a wrapper around the Tree::Simple
75       "traverse" method, and can be thought of as a depth-first traversal
76       Visitor object.
77

METHODS

79       new ($func, $depth)
80           The new style interface means that all arguments to the constructor
81           are now optional.  As a means of defining the usage of the old and
82           new, when no arguments are sent to the constructor, it is assumed
83           that the new style interface is being used. In the new style, the
84           $depth is always assumed to be equivalent to "RECURSIVE" and the
85           $func argument can be set with "setNodeFilter" instead. This is the
86           recommended way of doing things now.  If you have been using the
87           old way, it is still there, and I will maintain backwards
88           compatibility for a few more version before removing it entirely.
89           If you are using this module (and I do not even know if anyone
90           actually is) you have been warned. Please contact me if this will
91           be a problem.
92
93           The old style constructor documentation is retained her for
94           reference:
95
96           The first argument to the constructor is a code reference to a
97           function which expects a Tree::Simple object as its only argument.
98           The second argument is optional, it can be used to set the depth to
99           which the function is applied. If no depth is set, the function is
100           applied to the current Tree::Simple instance. If $depth is set to
101           "CHILDREN_ONLY", then the function will be applied to the current
102           Tree::Simple instance and all its immediate children. If $depth is
103           set to "RECURSIVE", then the function will be applied to the
104           current Tree::Simple instance and all its immediate children, and
105           all of their children recursively on down the tree. If no $depth is
106           passed to the constructor, then the function will only be applied
107           to the current Tree::Simple object and none
108            of its children.
109
110       includeTrunk ($boolean)
111           Based upon the value of $boolean, this will tell the visitor to
112           collect the trunk of the tree as well. It is defaulted to false (0)
113           in the new style interface, but is defaulted to true (1) in the old
114           style interface.
115
116       getNodeFilter
117           This method returns the CODE reference set with "setNodeFilter"
118           argument.
119
120       clearNodeFilter
121           This method clears node filter field.
122
123       setNodeFilter ($filter_function)
124           This method accepts a CODE reference as its $filter_function
125           argument. This code reference is used to filter the tree nodes as
126           they are collected. This can be used to customize output, or to
127           gather specific information from a more complex tree node. The
128           filter function should accept a single argument, which is the
129           current Tree::Simple object.
130
131       getResults
132           This method returns the accumulated results of the application of
133           the node filter to the tree.
134
135       setResults
136           This method should not really be used outside of this class, as it
137           just would not make any sense to. It is included in this class and
138           in this documentation to facilitate subclassing of this class for
139           your own needs. If you desire to clear the results, then you can
140           simply call "setResults" with no argument.
141
142       visit ($tree)
143           The "visit" method accepts a Tree::Simple and applies the function
144           set in "new" or "setNodeFilter" appropriately. The results of this
145           application can be retrieved with "getResults"
146

CONSTANTS

148       These constants are part of the old-style interface, and therefore will
149       eventually be deprecated.
150
151       RECURSIVE
152           If passed this constant in the constructor, the function will be
153           applied recursively down the hierarchy of Tree::Simple objects.
154
155       CHILDREN_ONLY
156           If passed this constant in the constructor, the function will be
157           applied to the immediate children of the Tree::Simple object.
158

BUGS

160       None that I am aware of. The code is pretty thoroughly tested (see CODE
161       COVERAGE section in Tree::Simple) and is based on an (non-publicly
162       released) module which I had used in production systems for about 2
163       years without incident. Of course, if you find a bug, let me know, and
164       I will be sure to fix it.
165

SEE ALSO

167       I have written a set of pre-built Visitor objects, available on CPAN as
168       Tree::Simple::VisitorFactory.
169

AUTHOR

171       stevan little, <stevan@iinteractive.com>
172

REPOSITORY

174       <https://github.com/ronsavage/Tree-Simple>.
175
177       Copyright 2004-2006 by Infinity Interactive, Inc.
178
179       <http://www.iinteractive.com>
180
181       This library is free software; you can redistribute it and/or modify it
182       under the same terms as Perl itself.
183
184
185
186perl v5.28.1                      2018-01-06          Tree::Simple::Visitor(3)
Impressum