1 2package Tree::Simple::Visitor; 3 4use strict; 5use warnings; 6 7our $VERSION = '1.11'; 8 9use Scalar::Util qw(blessed); 10 11## class constants 12 13use constant RECURSIVE => 0x01; 14use constant CHILDREN_ONLY => 0x10; 15 16### constructor 17 18sub new { 19 my ($_class, $func, $depth) = @_; 20 if (defined($depth)){ 21 ($depth =~ /\d+/ && ($depth == RECURSIVE || $depth == CHILDREN_ONLY)) 22 || die "Insufficient Arguments : Depth arguement must be either RECURSIVE or CHILDREN_ONLY"; 23 } 24 my $class = ref($_class) || $_class; 25 # if we have not supplied a $func 26 # it is automatically RECURSIVE 27 $depth = RECURSIVE unless defined $func; 28 my $visitor = { 29 depth => $depth || 0 30 }; 31 bless($visitor, $class); 32 $visitor->_init(); 33 if (defined $func) { 34 $visitor->setNodeFilter($func); 35 $visitor->includeTrunk(1); 36 } 37 return $visitor; 38} 39 40### methods 41 42sub _init { 43 my ($self) = @_; 44 $self->{_include_trunk} = 0; 45 $self->{_filter_function} = undef; 46 $self->{_results} = []; 47} 48 49sub includeTrunk { 50 my ($self, $boolean) = @_; 51 $self->{_include_trunk} = ($boolean ? 1 : 0) if defined $boolean; 52 return $self->{_include_trunk}; 53} 54 55# node filter methods 56 57sub getNodeFilter { 58 my ($self) = @_; 59 return $self->{_filter_function}; 60} 61 62sub clearNodeFilter { 63 my ($self) = @_; 64 $self->{_filter_function} = undef; 65} 66 67sub setNodeFilter { 68 my ($self, $filter_function) = @_; 69 (defined($filter_function) && ref($filter_function) eq "CODE") 70 || die "Insufficient Arguments : filter function argument must be a subroutine reference"; 71 $self->{_filter_function} = $filter_function; 72} 73 74# results methods 75 76sub setResults { 77 my ($self, @results) = @_; 78 $self->{results} = \@results; 79} 80 81sub getResults { 82 my ($self) = @_; 83 return wantarray ? 84 @{$self->{results}} 85 : 86 $self->{results}; 87} 88 89# visit routine 90sub visit { 91 my ($self, $tree) = @_; 92 (blessed($tree) && $tree->isa("Tree::Simple")) 93 || die "Insufficient Arguments : You must supply a valid Tree::Simple object"; 94 # get all things set up 95 my @results; 96 my $func; 97 if ($self->{_filter_function}) { 98 $func = sub { push @results => $self->{_filter_function}->(@_) }; 99 } 100 else { 101 $func = sub { push @results => $_[0]->getNodeValue() }; 102 } 103 # always apply the function 104 # to the tree's node 105 $func->($tree) unless defined $self->{_include_trunk}; 106 # then recursively to all its children 107 # if the object is configured that way 108 $tree->traverse($func) if ($self->{depth} == RECURSIVE); 109 # or just visit its immediate children 110 # if the object is configured that way 111 if ($self->{depth} == CHILDREN_ONLY) { 112 $func->($_) foreach $tree->getAllChildren(); 113 } 114 # now store the results we got 115 $self->setResults(@results); 116} 117 118 1191; 120 121__END__ 122 123=head1 NAME 124 125Tree::Simple::Visitor - Visitor object for Tree::Simple objects 126 127=head1 SYNOPSIS 128 129 use Tree::Simple; 130 use Tree::Simple::Visitor; 131 132 # create a visitor instance 133 my $visitor = Tree::Simple::Visitor->new(); 134 135 # create a tree to visit 136 my $tree = Tree::Simple->new(Tree::Simple->ROOT) 137 ->addChildren( 138 Tree::Simple->new("1.0"), 139 Tree::Simple->new("2.0") 140 ->addChild( 141 Tree::Simple->new("2.1.0") 142 ), 143 Tree::Simple->new("3.0") 144 ); 145 146 # by default this will collect all the 147 # node values in depth-first order into 148 # our results 149 $tree->accept($visitor); 150 151 # get our results and print them 152 print join ", ", $visitor->getResults(); # prints "1.0, 2.0, 2.1.0, 3.0" 153 154 # for more complex node objects, you can specify 155 # a node filter which will be used to extract the 156 # information desired from each node 157 $visitor->setNodeFilter(sub { 158 my ($t) = @_; 159 return $t->getNodeValue()->description(); 160 }); 161 162 # NOTE: this object has changed, but it still remains 163 # backwards compatible to the older version, see the 164 # DESCRIPTION section below for more details 165 166=head1 DESCRIPTION 167 168This 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 B<Tree::Simple::VisitorFactory> distribution, which includes a number of useful pre-built Visitors. 169 170While 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: 171 172 my @accumulator; 173 my $visitor = Tree::Simple::Visitor->new(sub { 174 my ($tree) = @_; 175 push @accumlator, $tree->getNodeValue(); 176 }, 177 Tree::Simple::Visitor->RECURSIVE); 178 179 $tree->accept($visitor); 180 181 print join ", ", @accumulator; # prints "1.0, 2.0, 2.1.0, 3.0" 182 183But is better expressed as this: 184 185 my $visitor = Tree::Simple::Visitor->new(); 186 $tree->accept($visitor); 187 print join ", ", $visitor->getResults(); # prints "1.0, 2.0, 2.1.0, 3.0" 188 189This object is still pretty much a wrapper around the Tree::Simple C<traverse> method, and can be thought of as a depth-first traversal Visitor object. 190 191=head1 METHODS 192 193=over 4 194 195=item B<new ($func, $depth)> 196 197The 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 C<$depth> is always assumed to be equivalent to C<RECURSIVE> and the C<$func> argument can be set with C<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. 198 199The old style constructor documentation is retained her for reference: 200 201The first argument to the constructor is a code reference to a function which expects a B<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 B<Tree::Simple> instance. If C<$depth> is set to C<CHILDREN_ONLY>, then the function will be applied to the current B<Tree::Simple> instance and all its immediate children. If C<$depth> is set to C<RECURSIVE>, then the function will be applied to the current B<Tree::Simple> instance and all its immediate children, and all of their children recursively on down the tree. If no C<$depth> is passed to the constructor, then the function will only be applied to the current B<Tree::Simple> object and none of its children. 202 203=item B<includeTrunk ($boolean)> 204 205Based upon the value of C<$boolean>, this will tell the visitor to collect the trunk of the tree as well. It is defaulted to false (C<0>) in the new style interface, but is defaulted to true (C<1>) in the old style interface. 206 207=item B<getNodeFilter> 208 209This method returns the CODE reference set with C<setNodeFilter> argument. 210 211=item B<clearNodeFilter> 212 213This method clears node filter field. 214 215=item B<setNodeFilter ($filter_function)> 216 217This method accepts a CODE reference as its C<$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. 218 219=item B<getResults> 220 221This method returns the accumulated results of the application of the node filter to the tree. 222 223=item B<setResults> 224 225This 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 C<setResults> with no argument. 226 227=item B<visit ($tree)> 228 229The C<visit> method accepts a B<Tree::Simple> and applies the function set in C<new> or C<setNodeFilter> appropriately. The results of this application can be retrieved with C<getResults> 230 231=back 232 233=head1 CONSTANTS 234 235These constants are part of the old-style interface, and therefore will eventually be deprecated. 236 237=over 4 238 239=item B<RECURSIVE> 240 241If passed this constant in the constructor, the function will be applied recursively down the hierarchy of B<Tree::Simple> objects. 242 243=item B<CHILDREN_ONLY> 244 245If passed this constant in the constructor, the function will be applied to the immediate children of the B<Tree::Simple> object. 246 247=back 248 249=head1 BUGS 250 251None that I am aware of. The code is pretty thoroughly tested (see B<CODE COVERAGE> section in B<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. 252 253=head1 SEE ALSO 254 255I have written a set of pre-built Visitor objects, available on CPAN as B<Tree::Simple::VisitorFactory>. 256 257=head1 AUTHOR 258 259stevan little, E<lt>stevan@iinteractive.comE<gt> 260 261=head1 COPYRIGHT AND LICENSE 262 263Copyright 2004-2006 by Infinity Interactive, Inc. 264 265L<http://www.iinteractive.com> 266 267This library is free software; you can redistribute it and/or modify 268it under the same terms as Perl itself. 269 270=cut