1Data::Visitor(3)      User Contributed Perl Documentation     Data::Visitor(3)
2
3
4

NAME

6       Data::Visitor - Visitor style traversal of Perl data structures
7

SYNOPSIS

9               # NOTE
10               # You probably want to use Data::Visitor::Callback for trivial things
11
12               package FooCounter;
13               use Moose;
14
15               extends qw(Data::Visitor);
16
17               has number_of_foos => (
18                       isa => "Int",
19                       is  => "rw",
20                       default => 0,
21               );
22
23               sub visit_value {
24                       my ( $self, $data ) = @_;
25
26                       if ( defined $data and $data eq "foo" ) {
27                               $self->number_of_foos( $self->number_of_foos + 1 );
28                       }
29
30                       return $data;
31               }
32
33               my $counter = FooCounter->new;
34
35               $counter->visit( {
36                       this => "that",
37                       some_foos => [ qw/foo foo bar foo/ ],
38                       the_other => "foo",
39               });
40
41               $counter->number_of_foos; # this is now 4
42

DESCRIPTION

44       This module is a simple visitor implementation for Perl values.
45
46       It has a main dispatcher method, "visit", which takes a single perl
47       value and then calls the methods appropriate for that value.
48
49       It can recursively map (cloning as necessary) or just traverse most
50       structures, with support for per object behavior, circular structures,
51       visiting tied structures, and all ref types (hashes, arrays, scalars,
52       code, globs).
53
54       Data::Visitor is meant to be subclassed, but also ships with a callback
55       driven subclass, Data::Visitor::Callback.
56

METHODS

58       visit $data
59           This method takes any Perl value as it's only argument, and
60           dispatches to the various other visiting methods using
61           "visit_no_rec_check", based on the data's type.
62
63           If the value is a reference and has already been seen then
64           "visit_seen" is called.
65
66       visit_seen $data, $first_result
67           When an already seen value is encountered again it's typically
68           replaced with the result of the first visitation of that value. The
69           value and the result of the first visitation are passed as
70           arguments.
71
72           Returns $first_result.
73
74       visit_no_rec_check $data
75           Called for any value that has not yet been seen. Does the actual
76           type based dispatch for "visit".
77
78           Should not be called directly unless forcing a circular structure
79           to be unfolded. Use with caution as this may cause infinite
80           recursion.
81
82       visit_object $object
83           If the value is a blessed object, "visit" calls this method. The
84           base implementation will just forward to "visit_value".
85
86       visit_ref $value
87           Generic recursive visitor. All non blessed values are given to
88           this.
89
90           "visit_object" can delegate to this method in order to visit the
91           object anyway.
92
93           This will check if the visitor can handle "visit_$reftype"
94           (lowercase), and if not delegate to "visit_value" instead.
95
96       visit_array $array_ref
97       visit_hash $hash_ref
98       visit_glob $glob_ref
99       visit_code $code_ref
100       visit_scalar $scalar_ref
101           These methods are called for the corresponding container type.
102
103       visit_value $value
104           If the value is anything else, this method is called. The base
105           implementation will return $value.
106
107       visit_hash_entries $hash
108       visit_hash_entry $key, $value, $hash
109           Delegates to "visit_hash_key" and "visit_hash_value". The value is
110           passed as $_[2] so that it is aliased.
111
112       visit_hash_key $key, $value, $hash
113           Calls "visit" on the key and returns it.
114
115       visit_hash_value $value, $key, $hash
116           The value will be aliased (passed as $_[1]).
117
118       visit_array_entries $array
119       visit_array_entry $value, $index, $array
120           Delegates to "visit" on value. The value is passed as $_[1] to
121           retain aliasing.
122
123       visit_tied $object, $var
124           When "tied_as_objects" is enabled and a tied variable (hash, array,
125           glob or scalar) is encountered this method will be called on the
126           tied object. If a valid mapped value is returned, the newly
127           constructed result container will be tied to the return value and
128           no iteration of the contents of the data will be made (since all
129           storage is delegated to the tied object).
130
131           If a non blessed value is returned from "visit_tied" then the
132           structure will be iterated normally, and the result container will
133           not be tied at all.
134
135           This is because tying to the same class and performing the tie
136           operations will not yield the same results in many cases.
137
138       retain_magic $orig, $copy
139           Copies over magic from $orig to $copy.
140
141           Currently only handles "bless". In the future this might be
142           expanded using Variable::Magic but it isn't clear what the correct
143           semantics for magic copying should be.
144
145       trace
146           Called if the "DEBUG" constant is set with a trace message.
147

RETURN VALUE

149       This object can be used as an "fmap" of sorts - providing an ad-hoc
150       functor interface for Perl data structures.
151
152       In void context this functionality is ignored, but in any other context
153       the default methods will all try to return a value of similar
154       structure, with it's children also fmapped.
155

SUBCLASSING

157       Create instance data using the Class::Accessor interface. Data::Visitor
158       inherits Class::Accessor to get a sane "new".
159
160       Then override the callback methods in any way you like. To retain
161       visitor behavior, make sure to retain the functionality of
162       "visit_array" and "visit_hash".
163

TODO

165       ·   Add support for "natural" visiting of trees.
166
167       ·   Expand "retain_magic" to support tying at the very least, or even
168           more with Variable::Magic if possible.
169

SEE ALSO

171       Data::Rmap, Tree::Simple::VisitorFactory, Data::Traverse
172
173       <http://en.wikipedia.org/wiki/Visitor_pattern>,
174       http://www.ninebynine.org/Software/Learning-Haskell-Notes.html#functors
175       <http://www.ninebynine.org/Software/Learning-Haskell-
176       Notes.html#functors>, <http://en.wikipedia.org/wiki/Functor>
177

AUTHOR

179       Yuval Kogman "<nothingmuch@woobling.org>"
180
181       Marcel Gruenauer, "<marcel@cpan.org>"
182
184               Copyright (c) 2006-2008 Yuval Kogman. All rights reserved
185               This program is free software; you can redistribute
186               it and/or modify it under the same terms as Perl itself.
187
188
189
190perl v5.12.0                      2010-02-03                  Data::Visitor(3)
Impressum