Data::Visitor - A visitor for Perl data structures
use base qw/Data::Visitor/; sub visit_value { my ( $self, $data ) = @_; return $whatever; } sub visit_array { my ( $self, $data ) = @_; # ... return $self->SUPER::visit_array( $whatever ); }
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
The visitor pattern is
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
This method is called when the value is an array reference.
If the value is anything else, this method is called. The base implementation will return $value.
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::Validator 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
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.