1Tree::Simple::Visitor::UTsoeNresCtoendtArrirbauyt(e3d)PTerrele:D:oSciummpelnet:a:tViiosnitor::ToNestedArray(3)
2
3
4
6 Tree::Simple::Visitor::ToNestedArray - A Visitor for creating nested
7 array trees from Tree::Simple objects.
8
10 use Tree::Simple::Visitor::ToNestedArray;
11
12 my $visitor = Tree::Simple::Visitor::ToNestedArray->new();
13
14 # given this Tree::Simple tree
15 my $tree = Tree::Simple->new("Root")
16 ->addChildren(
17 Tree::Simple->new("Child1")
18 ->addChildren(
19 Tree::Simple->new("GrandChild1"),
20 Tree::Simple->new("GrandChild2")
21 ),
22 Tree::Simple->new("Child2"),
23 );
24
25 # include the trunk (Root)
26 $visitor->includeTrunk(1);
27
28 # visit the tree
29 $tree->accept($visitor);
30
31 my $array_tree = $visitor->getResults();
32 # this then creates the equivalent nested array tree:
33 # [
34 # 'Root', [
35 # 'Child1', [
36 # 'GrandChild1',
37 # 'GrandChild2'
38 # ],
39 # 'Child2'
40 # ]
41 # ]
42
43 # if you don't include the trunk (Root) then ...
44 $tree->accept($visitor);
45
46 my $array_tree = $visitor->getResults();
47 # this then creates the following nested array tree:
48 # [
49 # 'Child1', [
50 # 'GrandChild1',
51 # 'GrandChild2'
52 # ],
53 # 'Child2'
54 # ]
55
57 Given a tree constructed from a Tree::Simple hierarchy, this Visitor
58 will create the equivalent tree of nested arrays.
59
61 new There are no arguments to the constructor the object will be in its
62 default state. You can use the "setNodeFilter" and "includTrunk"
63 methods to customize its behavior.
64
65 includTrunk ($boolean)
66 Setting the $boolean value to true (1) will cause the node value of
67 the tree's root to be included in the nested array output, setting
68 it to false will do the opposite.
69
70 setNodeFilter ($filter_function)
71 This method accepts a CODE reference as its $filter_function
72 argument and throws an exception if it is not a code reference.
73 This code reference is used to filter the tree nodes as they are
74 placed into the array tree. The $filter_function is passed a
75 Tree::Simple object, and is expected to return the value desired
76 for inclusion into the array tree.
77
78 visit ($tree)
79 This is the method that is used by Tree::Simple's "accept" method.
80 It can also be used on its own, it requires the $tree argument to
81 be a Tree::Simple object (or derived from a Tree::Simple object),
82 and will throw and exception otherwise.
83
84 getResults
85 This method will return the array tree constructed.
86
88 <https://github.com/ronsavage/Tree-Simple-VisitorFactory>
89
91 Bugs should be reported via the CPAN bug tracker at
92
93 <https://github.com/ronsavage/Tree-Simple-VisitorFactory/issues>
94
96 See the CODE COVERAGE section in Tree::Simple::VisitorFactory for more
97 information.
98
100 These Visitor classes are all subclasses of Tree::Simple::Visitor,
101 which can be found in the Tree::Simple module, you should refer to that
102 module for more information.
103
105 stevan little, <stevan@iinteractive.com>
106
108 Copyright 2004, 2005 by Infinity Interactive, Inc.
109
110 <http://www.iinteractive.com>
111
112 This library is free software; you can redistribute it and/or modify it
113 under the same terms as Perl itself.
114
115
116
117perl v5.38.0 2023-0T7r-e2e1::Simple::Visitor::ToNestedArray(3)