Attribute::Util - A selection of general-utility attributes |
Attribute::Util - A selection of general-utility attributes
use Attribute::Util;
# Alias
sub color : Alias(colour) { return 'red' }
# Abstract
package MyObj; sub new { ... } sub somesub: Abstract;
package MyObj::Better; use base 'MyObj'; sub somesub { return "I'm implemented!" }
# Memoize
sub fib :Memoize { my $n = shift; return $n if $n < 2; fib($n-1) + fib($n-2); }
$|++; print fib($_),"\n" for 1..50;
# SigHandler
sub myalrm : SigHandler(ALRM, VTALRM) { ... } sub mywarn : SigHandler(__WARN__) { ... }
This module provides four universally accessible attributes of general interest:
:Memoize
that makes it
unnecessary for you to explicitly call Memoize::memoize()
.
Options can be passed via the attribute per usual (see the
Attribute::Handlers
manpage for details, and the Memoize
manpage for information on memoizing options):
sub f :Memoize(NORMALIZER => 'main::normalize_f') { ... }
However, since the call to memoize()
is now done in a different
package, it is necessary to include the package name in any function
names passed as options to the attribute, as shown above.
Using the attribute makes it visually distinctive that a method is abstract, as opposed to declaring it without any attribute or method body, or providing a method body that might make it look as though it was implemented after all.
Alias
attribute provides
a layer of abstraction. If the underlying mechanism changes in a
future version of Perl (say, one that might not have the concept
of typeglobs anymore :), a new version of this module will take
care of that, but your Alias
declarations are going to stay the
same.
Note that assigning typeglobs means that you can't specify a synonym for one element of the glob and use the same synonym for a different target name in a different slot. I.e.,
sub color :Alias(colour) { ... } my $farbe :Alias(colour);
doesn't make sense, since the sub declaration aliases the whole
colour
glob to color
, but then the scalar declaration aliases
the whole colour
glob to farbe
, so the first alias is lost.
signal(s)
given as options for this
attribute. It thereby frees you from the implementation details of
defining sig handlers and keeps the handler definitions where they
belong, namely with the handler subroutine.
None known so far. If you find any bugs or oddities, please do inform the author.
Marcel Grunauer, <marcel@codewerk.com>
Copyright 2001 Marcel Grunauer. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
perl(1), Attribute::Handlers(3pm), Memoize(3pm).
Attribute::Util - A selection of general-utility attributes |