The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::Visitor - A visitor for Perl data structures

SYNOPSIS

        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 );
        }

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 visitor pattern is

METHODS

visit $data

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.

visit_object $object

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

visit_array $array_ref

This method is called when the value is an array reference.

visit_value $value

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

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 it's children also fmapped.

SUBCLASSING

Create instance data using the Class::Accessor interface. Data::Validator inherits Class::Accessor to get a sane 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.