Data::Visitor - Visitor style traversal of Perl data structures
# NOTE # You probably want to use Data::Visitor::Callback for trivial things package FooCounter; use base qw/Data::Visitor/; BEGIN { __PACKAGE__->mk_accessors( "number_of_foos" ) }; sub visit_value { my ( $self, $data ) = @_; if ( defined $data and $data eq "foo" ) { $self->number_of_foos( ($self->number_of_foos || 0) + 1 ); } return $data; } my $counter = FooCounter->new; $counter->visit( { this => "that", some_foos => [ qw/foo foo bar foo/ ], the_other => "foo", }); $counter->number_of_foos; # this is now 4
This module is a simple visitor implementation for Perl values.
It has a main dispatcher method, visit, which takes a single perl value and then calls the methods appropriate for that value.
visit
This method takes any Perl value as it's only argument, and dispatches to the various other visiting methods, based on the data's type.
If the value is a blessed object, visit calls this method. The base implementation will just forward to visit_value.
visit_value
Generic recursive visitor. All non blessed values are given to this.
visit_object can delegate to this method in order to visit the object anyway.
visit_object
This will check if the visitor can handle visit_$reftype (lowercase), and if not delegate to visit_value instead.
visit_$reftype
These methods are called for the corresponding container type.
If the value is anything else, this method is called. The base implementation will return $value.
Delegates to visit_hash_key and visit_hash_value. The value is passed as $_[2] so that it is aliased.
visit_hash_key
visit_hash_value
$_[2]
Calls visit on the key and returns it.
The value will be aliased (passed as $_[1]).
$_[1]
Delegates to visit on value. The value is passed as $_[1] to retain aliasing.
This object can be used as an fmap of sorts - providing an ad-hoc functor interface for Perl data structures.
fmap
In void context this functionality is ignored, but in any other context the default methods will all try to return a value of similar structure, with it's children also fmapped.
Create instance data using the Class::Accessor interface. Data::Visitor inherits Class::Accessor to get a sane new.
new
Then override the callback methods in any way you like. To retain visitor behavior, make sure to retain the functionality of visit_array and visit_hash.
visit_array
visit_hash
Add support for "natural" visiting of trees.
Expand retain_magic to support tying at the very least, or even more with Variable::Magic if possible.
retain_magic
Tied values might be redirected to an alternate handler that builds a new empty value, and ties it to a visited clone of the object the original is tied to using a trampoline class. Look into this.
Tree::Simple::VisitorFactory, Data::Traverse
http://en.wikipedia.org/wiki/Visitor_pattern, http://www.ninebynine.org/Software/Learning-Haskell-Notes.html#functors, http://en.wikipedia.org/wiki/Functor
Yuval Kogman <nothingmuch@woobling.org>
Copyright (c) 2006-2008 Yuval Kogman. All rights reserved This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Data::Visitor, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Visitor
CPAN shell
perl -MCPAN -e shell install Data::Visitor
For more information on module installation, please visit the detailed CPAN module installation guide.