Data::Visitor(3pm) | User Contributed Perl Documentation | Data::Visitor(3pm) |
Data::Visitor - Visitor style traversal of Perl data structures
version 0.32
# 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, "visit", 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.
If the value is a reference and has already been seen then "visit_seen" is called.
Returns $first_result.
Should not be called directly unless forcing a circular structure to be unfolded. Use with caution as this may cause infinite recursion.
"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.
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.
Currently only handles "bless". In the future this might be expanded using Variable::Magic but it isn't clear what the correct semantics for magic copying should be.
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 its 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 "visit_array" and "visit_hash".
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://en.wikipedia.org/wiki/Functor>
Bugs may be submitted through the RT bug tracker <https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Visitor> (or bug-Data-Visitor@rt.cpan.org <mailto:bug-Data-Visitor@rt.cpan.org>).
This software is copyright (c) 2023 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.
2023-09-28 | perl v5.36.0 |