Attribute::Memoize - A Memoize attribute


Attribute-Util documentation Contained in the Attribute-Util distribution.

Index


Code Index:

NAME

Top

Attribute::Memoize - A Memoize attribute

SYNOPSIS

Top

  use Attribute::Memoize;

  sub fib :Memoize {
          my $n = shift;
          return $n if $n < 2;
          fib($n-1) + fib($n-2);
  }
  $|++;
  print fib($_),"\n" for 1..50;

DESCRIPTION

Top

This attribute makes it slightly easier (and modern) to memoize a function by providing an attribute, :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.

BUGS

Top

None known so far. If you find any bugs or oddities, please do inform the author.

AUTHOR

Top

Marcel Grunauer, <marcel@codewerk.com>

Dan Kogai, <dankogai@dan.co.jp>

COPYRIGHT

Top

SEE ALSO

Top

perl(1), Attribute::Handlers, Memoize


Attribute-Util documentation Contained in the Attribute-Util distribution.

package Attribute::Memoize;

use warnings;
use strict;
use Attribute::Handlers;

our $VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)/g;

sub UNIVERSAL::Memoize :ATTR(CODE) {
	my ($pkg, $symbol, $options) = @_[0,1,4];
	$options = [ $options || () ] unless ref $options eq 'ARRAY';
	require Memoize;
	Memoize::memoize($pkg . '::' . *{$symbol}{NAME}, @$options);
}

"Rosebud"; # for MARCEL's sake, not 1 -- dankogai

__END__