Data::Visitor(3)
π NAME π
Okay bestie, this is Data::Visitor(3) from the User Contributed Perl Documentation, and it is literally about Visitor style traversal of Perl data structures. If you have a nested Perl data blob and you need to walk it like a main character, this is your girl. π«Ά
πΈ VERSION πΈ
version 0.32 β yes, that's the version, no cap. β¨
β¨ SYNOPSIS β¨
Heads-up from the original: you probably want Data::Visitor::Callback for trivial things. The code below is exactly the example, bestie. π
# 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
π¦ DESCRIPTION π¦
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. The way that works is honestly iconic. π
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). So yes, it's giving full coverage. πβ¨
Data::Visitor is meant to be subclassed, but also ships with a callback driven subclass, Data::Visitor::Callback. That's your low-effort bestie for simpler stuff. π«Ά
π METHODS π
π visit $data
This method takes any Perl value as its only argument, and dispatches to the various other visiting methods using visit_no_rec_check, based on the data's type. β¨
If the value is a reference and has already been seen then visit_seen is called. No infinite-loop chaos unless you ask for it, bestie. π
π visit_seen $data, $first_result
When an already seen value is encountered again, it is 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. Periodt. π
π₯ visit_no_rec_check $data
Called for any value that has not yet been seen. Does the actual type based dispatch for visit. π
Should not be called directly unless forcing a circular structure to be unfolded. Use with caution as this may cause infinite recursion. Like, genuinely be careful. π
π¦ visit_object $object
If the value is a blessed object, visit calls this method. The base implementation will just forward to visit_value. Cute and simple. β¨
π visit_ref $value
Generic recursive visitor. All non blessed values are given to this. π«Ά
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. iykyk. π
πΈ visit_array $array_ref, visit_hash $hash_ref, visit_glob $glob_ref, visit_code $code_ref, visit_scalar $scalar_ref
These methods are called for the corresponding container type. All the ref types get their little moment. β¨
π visit_value $value
If the value is anything else, this method is called. The base implementation will return $value. Simple, iconic, no notes. π
π visit_hash_entries $hash, visit_hash_entry $key, $value, $hash
Delegates to visit_hash_key and visit_hash_value. The value is passed as $_[2] so that it is aliased. π«Ά
π visit_hash_key $key, $value, $hash
Calls visit on the key and returns it. Slay. β¨
π visit_hash_value $value, $key, $hash
The value will be aliased (passed as $_[1]). That aliasing detail matters, bestie. π
π visit_array_entries $array, visit_array_entry $value, $index, $array
Delegates to visit on value. The value is passed as $_[1] to retain aliasing. π«Ά
π₯ visit_tied $object, $var
When 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. So yeah, that's the why. π
πΈ retain_magic $orig, $copy
Copies over magic from $orig to $copy. β¨
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. That's a future-bestie problem. π«Ά
π trace
Called if the DEBUG constant is set with a trace message. π
π RETURN VALUE π
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. That's the vibe. β¨
π SUBCLASSING π
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. No cap, those two are doing heavy lifting. π
π TODO π
- Add support for
"natural"visiting of trees. πΈ - Expand
retain_magicto support tying at the very least, or even more withVariable::Magicif possible. β¨
π SEE ALSO π
Data::RmapTree::Simple::VisitorFactoryData::Traverse<http://en.wikipedia.org/wiki/Visitor_pattern>π¦<http://www.ninebynine.org/Software/Learning-Haskell-Notes.html#functors>π<http://en.wikipedia.org/wiki/Functor>π
π SUPPORT π
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>). π«Ά
π₯ AUTHORS π₯
- Yuval Kogman
<nothingmuch@woobling.org> - Marcel GrΓΌnauer
<marcel@cpan.org>
πΈ CONTRIBUTORS πΈ
- Jesse Luehrs
<doy@tozt.net> - Karen Etheridge
<ether@cpan.org> - Florian Ragwitz
<rafl@debian.org> - David Steinbrunner
<dsteinbrunner@pobox.com> - Graham Knop
<haarg@haarg.org> - Robin SmidsrΓΈd
<robin@smidsrod.no>
β¨ COPYRIGHT AND LICENCE β¨
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. Slay. π
Cute sign-off time, bestie: this page comes from perl v5.42.0, dated 2023-04-30, and it is Data::Visitor(3). Byeee! πβ¨π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Data-Visitor 0.32-bp160.1.11 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms