Data::Visitor - Visitor style traversal of Perl data structures |
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
calls this method. The base
implementation will just forward to visit_value
.
visit_object
can delegate to this method in order to visit the object
anyway.
This will check if the visitor can handle visit_$reftype
(lowercase), and if
not delegate to visit_value
instead.
visit_hash_key
and visit_hash_value
. The value is passed as
$_[2]
so that it is aliased.
visit
on the key and returns it.
$_[1]
).
visit
on value. The value is passed as $_[1]
to retain
aliasing.
tied_as_objects
is enabled and a tied variable (hash, array, glob or
scalar) is encountered this method will be called on the tied object. If a
valid mapped value is returned, the newly constructed result container will be
tied to the return value and no iteration of the contents of the data will be
made (since all storage is delegated to the tied object).
If a non blessed value is returned from visit_tied
then the structure will
be iterated normally, and the result container will not be tied at all.
This is because tying to the same class and performing the tie operations will not yield the same results in many cases.
This object can be used as an fmap
of sorts - providing an ad-hoc functor
interface for Perl data structures.
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 the Class::Accessor manpage interface. the Data::Visitor manpage
inherits the Class::Accessor manpage to get a sane 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
.
retain_magic
to support tying at the very least, or even more with
the Variable::Magic manpage if possible.
the Data::Rmap manpage, the Tree::Simple::VisitorFactory manpage, the Data::Traverse manpage
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.
Data::Visitor - Visitor style traversal of Perl data structures |