A data::visitor with callbacks.
version 0.30
use Data::Visitor::Callback; my $v = Data::Visitor::Callback->new( # you can provide callbacks # $_ will contain the visited value value => sub { ... }, array => sub { ... }, # you can also delegate to method names # this specific example will force traversal on objects, by using the # 'visit_ref' callback which normally traverse unblessed references object => "visit_ref", # you can also use class names as callbacks # the callback will be invoked on all objects which inherit that class 'Some::Class' => sub { my ( $v, $obj ) = @_; # $v is the visitor ... }, ); $v->visit( $some_perl_value );
This is a Data::Visitor subclass that lets you invoke callbacks instead of needing to subclass yourself.
Construct a new visitor. The options supported are:
When this is true (off by default) the return values from the callbacks are ignored, thus disabling the fmapping behavior as documented in Data::Visitor. This is useful when you want to modify $_ directly
Whether ot not to visit the \*(L"tied\*(R" in perlfunc of a tied structure instead of pretending the structure is just a normal one. See \*(L"visit_tied\*(R" in Data::Visitor.
Use these keys for the corresponding callbacks.
The callback is in the form:
sub { my ( $visitor, $data ) = @_;
# or you can use $_, it's aliased
return $data; # or modified data }
Within the callback $_ is aliased to the data, and this is also passed in the parameter list.
Any method can also be used as a callback:
object => "visit_ref", # visit objects anyway
Called for all values
Called for non objects, non container (hash, array, glob or scalar ref) values.
Called after \*(C`value\*(C', for references to regexes, globs and code.
Called after \*(C`value\*(C' for non references.
Called for blessed objects. Since \*(L"visit_object\*(R" in Data::Visitor will not recurse downwards unless you delegate to \*(C`visit_ref\*(C', you can specify \*(C`visit_ref\*(C' as the callback for \*(C`object\*(C' in order to enter objects. It is reccomended that you specify the classes (or base classes) you want though, instead of just visiting any object forcefully.
You can use any class name as a callback. This is colled only after the \*(C`object\*(C' callback. If the object \*(C`isa\*(C' the class then the callback will fire. These callbacks are called from least derived to most derived by comparing the classes' \*(C`isa\*(C' at construction time.
Called for every object that did not have a class callback.
The last callback called for objects, useful if you want to post process the output of any class callbacks.
Called for array references.
Called for hash references.
Called for glob references.
Called for scalar references.
Called on the return value of \*(C`tied\*(C' for all tied containers. Also passes in the variable as the second argument.
Called for a reference value encountered a second time. Passes in the result mapping as the second argument.
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.