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

VERSION

9       version 0.30
10

SYNOPSIS

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

DESCRIPTION

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

METHODS

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

RETURN VALUE

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

SUBCLASSING

160       Data::Visitor is a Moose class, so it should be subclassed using Moose.
161
162       Then override the callback methods in any way you like. To retain
163       visitor behavior, make sure to retain the functionality of
164       "visit_array" and "visit_hash".
165

TODO

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

SEE ALSO

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

AUTHORS

180       ·   Yuval Kogman <nothingmuch@woobling.org>
181
182       ·   Marcel Grünauer <marcel@cpan.org>
183
185       This software is copyright (c) 2013 by Yuval Kogman.
186
187       This is free software; you can redistribute it and/or modify it under
188       the same terms as the Perl 5 programming language system itself.
189
190
191
192perl v5.28.1                      2013-06-24                  Data::Visitor(3)
Impressum