Class::Visitor - Visitor and Iterator extensions to Class::Template
use Class::Visitor; visitor_class 'CLASS', 'SUPER', { TEMPLATE }; visitor_class 'CLASS', 'SUPER', [ TEMPLATE ]; $obj = CLASS->new (); $iter = $obj->iter; $iter = $obj->iter ($parent, $array, $index); $obj->accept($visitor, ...); $obj->children_accept($visitor, ...); $obj->children_accept_ARRAYMEMBER ($visitor, ...); $obj->push_ARRAYMEMBER($value[, ...]); $value = $obj->pop_ARRAYMEMBER; $obj->as_string ([$context[, ...]]); $obj->ARRAYMEMBER_as_string ([$context[, ...]]); $iter inherits the following from Class::Iter: $iter->parent; $iter->is_iter; $iter->root; $iter->rootpath; $iter->next; $iter->at_end; $iter->delegate; $iter->is_same ($obj);
Class::Visitor extends the getter/setter functions provided by Class::Template for CLASS by defining methods for using the Visitor and Iterator design patterns. All of the Iterator methods are inherited from Class::Iter except iter.
Class::Visitor
Class::Template
Class::Iter
iter
CLASS is the name of the new class, SUPER the superclass of this class (will define @ISA), and TEMPLATE is as defined in Class::Template.
@ISA
$obj-iter> returns a new iterator for this object. If parent, array, and index are not defined, then the new iterator is treated as the root object. Except as inherited from Class::Iter or as defined below, methods for $iter and $obj work the same.
$obj-
parent
array
index
$iter
$obj
The accept methods cause a callback to $visitor with $self as the first argument plus the rest of the arguments passed to accept. This is implemented like:
accept
$visitor
$self
sub accept { my $self = shift; my $visitor = shift; $visitor->visit_MyClass ($self, @_); }
children_accept calls accept on each object in the array field named contents. children_accept_ARRAYMEMBER does the same for ARRAYMEMBER.
children_accept
contents
children_accept_ARRAYMEMBER
Calling accept methods on iterators always calls back using iterators. Calling accept on non-iterators calls back using non-iterators. The latter is significantly faster.
push and pop act like their respective array functions.
push
pop
as_string returns the concatenated scalar values of the array field named contents, possibly modified by $context. ARRAYMEMBER_as_string does the same for ARRAYMEMBER.
as_string
$context
ARRAYMEMBER_as_string
Visitor handles scalars specially for children_accept and as_string. In the case of children_accept, Visitor will create an iterator in the class Class::Scalar::Iter with the scalar as the delegate.
Class::Scalar::Iter
In the case of as_string, Visitor will use the string unless $context->{cdata_mapper} is defined, in which case it returns the result of calling the cdata_mapper subroutine with the scalar and the remaining arguments. The actual implementation is:
$context->{cdata_mapper}
cdata_mapper
&{$context->{cdata_mapper}} ($scalar, @_);
Ken MacLeod, ken@bitsko.slc.ut.us
perl(1), Class::Template(3), Class::Iter(3).
The package SGML::SPGrove uses Class::Visitor extensively.
SGML::SPGrove
To install Class::Visitor, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::Visitor
CPAN shell
perl -MCPAN -e shell install Class::Visitor
For more information on module installation, please visit the detailed CPAN module installation guide.