1Tree::Simple::Visitor::UFsreormNCeosntterdiAbrurtaeydT(r3Pe)eer:l:SDiomcpulmee:n:tVaitsiiotnor::FromNestedArray(3)
2
3
4
6 Tree::Simple::Visitor::FromNestedArray - A Visitor for creating
7 Tree::Simple objects from nested array trees.
8
10 use Tree::Simple::Visitor::FromNestedArray;
11
12 my $visitor = Tree::Simple::Visitor::FromNestedArray->new();
13
14 # given this nested array tree
15 my $array_tree = [
16 'Root', [
17 'Child1', [
18 'GrandChild1',
19 'GrandChild2'
20 ],
21 'Child2'
22 ]
23 ];
24 # set the array tree we
25 # are going to convert
26 $visitor->setArrayTree($array_tree);
27
28 $tree->accept($visitor);
29
30 # this then creates the equivalent Tree::Simple object:
31 # Tree::Simple->new("Root")
32 # ->addChildren(
33 # Tree::Simple->new("Child1")
34 # ->addChildren(
35 # Tree::Simple->new("GrandChild1"),
36 # Tree::Simple->new("GrandChild2")
37 # ),
38 # Tree::Simple->new("Child2"),
39 # );
40
42 Given a tree constructed from nested arrays, this Visitor will create
43 the equivalent Tree::Simple hierarchy.
44
46 new There are no arguments to the constructor the object will be in its
47 default state. You can use the "setNodeFilter", "includTrunk" and
48 "setArrayTree" methods to customize its behavior.
49
50 includTrunk ($boolean)
51 Setting the $boolean value to true (1) will cause the node value of
52 the $tree object passed into "visit" to be set with the root value
53 found in the $array_tree. Setting it to false (0), or not setting
54 it, will result in the first value in the $array_tree creating a
55 new node level.
56
57 setNodeFilter ($filter_function)
58 This method accepts a CODE reference as its $filter_function
59 argument and throws an exception if it is not a code reference.
60 This code reference is used to filter the tree nodes as they are
61 created, the $filter_function is passed the node value extracted
62 from the array prior to it being inserted into the tree being
63 built. The $filter_function is expected to return the value desired
64 for inclusion into the tree.
65
66 setArrayTree ($array_tree)
67 This method is used to set the $array_tree that our Tree::Simple
68 hierarchy will be constructed from. It must be in the following
69 form:
70
71 [
72 'Root', [
73 'Child1', [
74 'GrandChild1',
75 'GrandChild2'
76 ],
77 'Child2'
78 ]
79 ]
80
81 Basically each element in the array is considered a node, unless it
82 is an array reference, in which case it is interpreted as
83 containing the children of the node created from the previous
84 element in the array.
85
86 The tree is validated prior being accepted, if it fails validation
87 an exception will be thrown. The rules are as follows;
88
89 The array tree must not be empty.
90 It makes not sense to create a tree out of nothing, so it is
91 assumed that this is a sign of something wrong.
92
93 All nodes of the array tree must not be array references.
94 The root node is validated against this in this function, but
95 all subsequent nodes are checked as the tree is built. Any
96 nodes found to be array references are rejected and an
97 exception is thrown. If you desire your node values to be array
98 references, you can use the node filtering mechanism to achieve
99 this as the node is filtered after it is validated.
100
101 The array tree must be a single rooted tree.
102 If there is a second element in the array tree, it is assumed
103 to be the children of the root, and therefore must be in the
104 form of an array reference.
105
106 visit ($tree)
107 This is the method that is used by the Tree::Simple "accept"
108 method. It can also be used on its own, it requires the $tree
109 argument to be a Tree::Simple object (or derived from a
110 Tree::Simple object), and will throw and exception otherwise.
111
113 None that I am aware of. Of course, if you find a bug, let me know, and
114 I will be sure to fix it.
115
117 See the CODE COVERAGE section in Tree::Simple::VisitorFactory for more
118 information.
119
121 These Visitor classes are all subclasses of Tree::Simple::Visitor,
122 which can be found in the Tree::Simple module, you should refer to that
123 module for more information.
124
126 stevan little, <stevan@iinteractive.com>
127
129 Copyright 2004, 2005 by Infinity Interactive, Inc.
130
131 <http://www.iinteractive.com>
132
133 This library is free software; you can redistribute it and/or modify it
134 under the same terms as Perl itself.
135
136
137
138perl v5.34.0 2022T-r0e1e-:2:1Simple::Visitor::FromNestedArray(3)