Attribute-Handlers
view release on metacpan or search on metacpan
Note that, because the "Cycle" attribute receives its arguments in the
$data variable, if the attribute is given a list of arguments, $data
will consist of a single array reference; otherwise, it will consist of
the single argument directly. Since Tie::Cycle requires its cycling
values to be passed as an array reference, this means that we need to
wrap non-array-reference arguments in an array constructor:
$data = [ $data ] unless ref $data eq 'ARRAY';
Typically, however, things are the other way around: the tieable class
expects its arguments as a flattened list, so the attribute looks like:
sub UNIVERSAL::Cycle : ATTR(SCALAR) {
my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
my @data = ref $data eq 'ARRAY' ? @$data : $data;
tie $$referent, 'Tie::Whatever', @data;
}
This software pattern is so widely applicable that Attribute::Handlers
provides a way to automate it: specifying 'autotie' in the "use
Attribute::Handlers" statement. So, the cycling example, could also be
sub foo : Purpose(to foo all data before barring it) { }
# etc.
UTILITY FUNCTIONS
This module offers a single utility function, "findsym()".
findsym
my $symbol = Attribute::Handlers::findsym($package, $referent);
The function looks in the symbol table of $package for the typeglob
for $referent, which is a reference to a variable or subroutine
(SCALAR, ARRAY, HASH, or CODE). If it finds the typeglob, it returns
it. Otherwise, it returns undef. Note that "findsym" memoizes the
typeglobs it has previously successfully found, so subsequent calls
with the same arguments should be much faster.
DIAGNOSTICS
"Bad attribute type: ATTR(%s)"
An attribute handler was specified with an ":ATTR(*ref_type*)", but
the type of referent it was defined to handle wasn't one of the five
lib/Attribute/Handlers.pm view on Meta::CPAN
Note that, because the C<Cycle> attribute receives its arguments in the
C<$data> variable, if the attribute is given a list of arguments, C<$data>
will consist of a single array reference; otherwise, it will consist of the
single argument directly. Since Tie::Cycle requires its cycling values to
be passed as an array reference, this means that we need to wrap
non-array-reference arguments in an array constructor:
$data = [ $data ] unless ref $data eq 'ARRAY';
Typically, however, things are the other way around: the tieable class expects
its arguments as a flattened list, so the attribute looks like:
sub UNIVERSAL::Cycle : ATTR(SCALAR) {
my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
my @data = ref $data eq 'ARRAY' ? @$data : $data;
tie $$referent, 'Tie::Whatever', @data;
}
This software pattern is so widely applicable that Attribute::Handlers
provides a way to automate it: specifying C<'autotie'> in the
lib/Attribute/Handlers.pm view on Meta::CPAN
=head1 UTILITY FUNCTIONS
This module offers a single utility function, C<findsym()>.
=over 4
=item findsym
my $symbol = Attribute::Handlers::findsym($package, $referent);
The function looks in the symbol table of C<$package> for the typeglob for
C<$referent>, which is a reference to a variable or subroutine (SCALAR, ARRAY,
HASH, or CODE). If it finds the typeglob, it returns it. Otherwise, it returns
undef. Note that C<findsym> memoizes the typeglobs it has previously
successfully found, so subsequent calls with the same arguments should be
much faster.
=back
=head1 DIAGNOSTICS
( run in 0.628 second using v1.01-cache-2.11-cpan-607d282f910 )