Visitor style traversal of perl data structures
version 0.30
# NOTE # You probably want to use Data::Visitor::Callback for trivial things package FooCounter; use Moose; extends qw(Data::Visitor); has number_of_foos => ( isa => "Int", is => "rw", default => 0, ); sub visit_value { my ( $self, $data ) = @_; if ( defined $data and $data eq "foo" ) { $self->number_of_foos( $self->number_of_foos + 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, \*(C`visit\*(C', which takes a single perl value and then calls the methods appropriate for that value.
It can recursively map (cloning as necessary) or just traverse most structures, with support for per object behavior, circular structures, visiting tied structures, and all ref types (hashes, arrays, scalars, code, globs).
Data::Visitor is meant to be subclassed, but also ships with a callback driven subclass, Data::Visitor::Callback.
This method takes any Perl value as it's only argument, and dispatches to the various other visiting methods using \*(C`visit_no_rec_check\*(C', based on the data's type. If the value is a reference and has already been seen then \*(C`visit_seen\*(C' is called. When an already seen value is encountered again it's typically replaced with the result of the first visitation of that value. The value and the result of the first visitation are passed as arguments. Returns $first_result. Called for any value that has not yet been seen. Does the actual type based dispatch for \*(C`visit\*(C'. Should not be called directly unless forcing a circular structure to be unfolded. Use with caution as this may cause infinite recursion. If the value is a blessed object, \*(C`visit\*(C' calls this method. The base implementation will just forward to \*(C`visit_value\*(C'. Generic recursive visitor. All non blessed values are given to this. \*(C`visit_object\*(C' can delegate to this method in order to visit the object anyway. This will check if the visitor can handle \*(C`visit_$reftype\*(C' (lowercase), and if not delegate to \*(C`visit_value\*(C' instead. 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 \*(C`visit_hash_key\*(C' and \*(C`visit_hash_value\*(C'. The value is passed as $_[2] so that it is aliased. Calls \*(C`visit\*(C' on the key and returns it. The value will be aliased (passed as $_[1]). Delegates to \*(C`visit\*(C' on value. The value is passed as $_[1] to retain aliasing. When \*(C`tied_as_objects\*(C' 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 \*(C`visit_tied\*(C' 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. Copies over magic from $orig to $copy. Currently only handles \*(C`bless\*(C'. In the future this might be expanded using Variable::Magic but it isn't clear what the correct semantics for magic copying should be.
Called if the \*(C`DEBUG\*(C' constant is set with a trace message.
This object can be used as an \*(C`fmap\*(C' 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.
Data::Visitor is a Moose class, so it should be subclassed using Moose.
Then override the callback methods in any way you like. To retain visitor behavior, make sure to retain the functionality of \*(C`visit_array\*(C' and \*(C`visit_hash\*(C'.
Add support for \*(L"natural\*(R" visiting of trees.
Expand \*(C`retain_magic\*(C' to support tying at the very least, or even more with Variable::Magic if possible.
Data::Rmap, Tree::Simple::VisitorFactory, Data::Traverse
<http://en.wikipedia.org/wiki/Visitor_pattern>, http://www.ninebynine.org/Software/Learning-Haskell-Notes.html#functors <http://www.ninebynine.org/Software/Learning-Haskell-Notes.html#functors>, <http://en.wikipedia.org/wiki/Functor>
Yuval Kogman <[email protected]>
Marcel Gru\*:nauer <[email protected]>
This software is copyright (c) 2013 by Yuval Kogman.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.