NAME
Memoize::Attrs - Add memoization with subroutine attributes
SYNOPSIS
use Memoize::Attrs;
sub slow_function :MEMOIZE {
...
}
# slow_function is memoized and faster
DESCRIPTION
Memoization is a wonderful thing when appropriate. And memoization is a nice optimization trick that may be applied after some thought and experimentation. With the Memoize
module, that means to invoke memoize
after declaring the subroutine in question.
But that said, memoization looks like a trait and should be added without much fuss. That trivial module makes is possible by annotating subroutine definitions with an attribute MEMOIZE
. So that code that looked like
use Memoize qw(memoize);
sub slow_function { ... }
memoize('slow_function');
turns into
use Memoize::Attrs;
sub slow_function :MEMOIZE { ... }
which is quite short and happens very early (namely, CHECK time).
This module is not a big deal, but it may help keep the code clean when you have a lot of memoized functions or when you want to add memoization without calling much attention to it.
OPTIONS
INSTALL => NAME
If NAME does not match /::/, this is installed in the caller package.
NORMALIZER => CODE | NAME
If NAME does not match /::/, it is resolved in the caller package.
SCALAR_CACHE
LIST_CACHE
Just like the options in Memoize
module.
SEE ALSO
Memoize
Attribute::Handlers
After the release of this module to CPAN, I found
Attribute::Memoize
There are some subtle differences between the two modules (besides using Memoize x MEMOIZE). Namely,
* this module resolves non-qualified subnames
into the caller's package, while
Attribute::Memoize requires the use of fully
qualified names
* this uses attribute handlers applied at CHECK
time while the former uses BEGIN-enabled
handlers.
BUGS
Documentation needs improvement. And so did tests as well.
We actually install an attribute handler for MEMOIZE
at UNIVERSAL
-- oh, horror! -- polluting much more than we should. Instead the attribute handler should be installed into the caller of Memoize::Attrs
' import
subroutine, but it looks like the current exporter modules aren't ready to export subroutines together with their attributes and made their attribute handlers run. I've not digged well enough in this issue and it may be only oversight actually. Time will tell and fixes will come eventually.
The handling of options via the data associated to the attribute MEMOIZE may be faulty yet. But it works
sub scalar_function :MEMOIZE( LIST_CACHE=>'FAULT' );
for a function which should be called only in scalar context.
sub slow_function :MEMOIZE( INSTALL => quick_function );
to install a memoized version at quick_function
.
Please report bugs via CPAN RT http://rt.cpan.org/NoAuth/Bugs.html?Dist=Memoize-Attrs.
AUTHOR
Adriano Ferreira, <ferreira@cpan.org>
COPYRIGHT AND LICENSE
Copyright (C) 2007 by Adriano R. Ferreira
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.