2 package Tree::Simple::Visitor::ToNestedArray;
9 use Scalar::Util qw(blessed);
11 use base qw(Tree::Simple::Visitor);
15 my $class = ref($_class) || $_class;
17 bless($visitor, $class);
23 my ($self, $tree) = @_;
24 (blessed($tree) && $tree->isa("Tree::Simple"))
25 || die "Insufficient Arguments : You must supply a valid Tree::Simple object";
26 # grab our filter (if we have one)
27 my $filter = $self->getNodeFilter();
30 $self->_buildArray($tree, \@results, $filter);
31 # add the trunk if we need to
36 $tree->getNodeValue()),
38 ) if $self->includeTrunk();
40 $self->setResults(\@results);
44 my ($self, $tree, $accumulator, $filter) = @_;
45 foreach my $child ($tree->getAllChildren()) {
46 push @{$accumulator} => (defined($filter) ? $filter->($child) : $child->getNodeValue());
47 push @{$accumulator} => $self->_buildArray($child, [], $filter) unless $child->isLeaf();
58 Tree::Simple::Visitor::ToNestedArray - A Visitor for creating nested array trees from Tree::Simple objects.
62 use Tree::Simple::Visitor::ToNestedArray;
64 my $visitor = Tree::Simple::Visitor::ToNestedArray->new();
66 # given this Tree::Simple tree
67 my $tree = Tree::Simple->new("Root")
69 Tree::Simple->new("Child1")
71 Tree::Simple->new("GrandChild1"),
72 Tree::Simple->new("GrandChild2")
74 Tree::Simple->new("Child2"),
77 # include the trunk (Root)
78 $visitor->includeTrunk(1);
81 $tree->accept($visitor);
83 my $array_tree = $visitor->getResults();
84 # this then creates the equivalent nested array tree:
95 # if you don't include the trunk (Root) then ...
96 $tree->accept($visitor);
98 my $array_tree = $visitor->getResults();
99 # this then creates the following nested array tree:
110 Given a tree constructed from a Tree::Simple heirarchy, this Visitor will create the equivalent tree of nested arrays.
118 There are no arguments to the constructor the object will be in its default state. You can use the C<setNodeFilter> and C<includTrunk> methods to customize its behavior.
120 =item B<includTrunk ($boolean)>
122 Setting the C<$boolean> value to true (C<1>) will cause the node value of the tree's root to be included in the nested array output, setting it to false will do the opposite.
124 =item B<setNodeFilter ($filter_function)>
126 This method accepts a CODE reference as its C<$filter_function> argument and throws an exception if it is not a code reference. This code reference is used to filter the tree nodes as they are placed into the array tree. The C<$filter_function> is passed a Tree::Simple object, and is expected to return the value desired for inclusion into the array tree.
128 =item B<visit ($tree)>
130 This is the method that is used by Tree::Simple's C<accept> method. It can also be used on its own, it requires the C<$tree> argument to be a Tree::Simple object (or derived from a Tree::Simple object), and will throw and exception otherwise.
134 This method will return the array tree constructed.
140 None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it.
144 See the B<CODE COVERAGE> section in L<Tree::Simple::VisitorFactory> for more inforamtion.
148 These Visitor classes are all subclasses of B<Tree::Simple::Visitor>, which can be found in the B<Tree::Simple> module, you should refer to that module for more information.
152 stevan little, E<lt>stevan@iinteractive.comE<gt>
154 =head1 COPYRIGHT AND LICENSE
156 Copyright 2004, 2005 by Infinity Interactive, Inc.
158 L<http://www.iinteractive.com>
160 This library is free software; you can redistribute it and/or modify
161 it under the same terms as Perl itself.