phpman > perldoc > Data::Visitor(3pm)

Markdown | JSON | MCP    

NAME
    Data::Visitor - Visitor style traversal of Perl data structures

VERSION
    version 0.31

SYNOPSIS
            # 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.

    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.

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.

    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.

    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.

    visit_object $object
        If the value is a blessed object, "visit" calls this method. The base implementation will
        just forward to "visit_value".

    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.

    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.

    visit_value $value
        If the value is anything else, this method is called. The base implementation will return
        $value.

    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.

    visit_hash_value $value, $key, $hash
        The value will be aliased (passed as $_[1]).

    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.

    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.

    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.

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".

TODO
    *   Add support for "natural" visiting of trees.

    *   Expand "retain_magic" to support tying at the very least, or even more with Variable::Magic
        if possible.

SEE ALSO
    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>

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 AT rt.org <mailto:bug-Data-Visitor AT rt.org>).

AUTHORS
    *   Yuval Kogman <nothingmuch AT woobling.org>

    *   Marcel Grünauer <marcel AT cpan.org>

CONTRIBUTORS
    *   Jesse Luehrs <doy AT tozt.net>

    *   Florian Ragwitz <rafl AT debian.org>

    *   Karen Etheridge <ether AT cpan.org>

    *   David Steinbrunner <dsteinbrunner AT pobox.com>

    *   Robin Smidsrød <robin AT smidsrod.no>

COPYRIGHT AND LICENCE
    This software is copyright (c) 2020 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.

Data::Visitor(3pm)
NAME VERSION SYNOPSIS DESCRIPTION METHODS RETURN VALUE SUBCLASSING TODO SEE ALSO SUPPORT AUTHORS CONTRIBUTORS COPYRIGHT AND LICENCE

Generated by phpman v3.7.12 Author: Che Dong Under GNU General Public License
2026-06-13 14:43 @216.73.216.28
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 TransitionalValid CSS!

^_back to top