The Perl Advent Calendar needs more articles for 2022. Submit your idea today!


Attribute::Method - No more 'my $self = shift;'


  package Lazy;
  use strict;
  use warnings;
  use Attribute::Method qw( $val );
                        # pass all parameter names here
                        # to make happy
  sub new : Method { 
      bless { @_ }, $self 
  sub set_foo : Method( $val ){
      $self->{foo} = $val;
  sub get_foo : Method {


This Attribute makes your subroutine a method -- $self is automagically set and the parameter list is supported.

This trick is actually introduced in "Perl Hacks", hack #47. But the code sample therein is a little buggy so have a look at this module instead.


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


The following does not work.
  use Attribute::Memoize;
  use strict;
  use warnings;
  use lib '.';
  print "loading bar ...\n";
  require bar; # should have been 'use bar;'
  print "bar is loaded\n";
  print bar::func(),"\n";
  print bar::func(),"\n";
  exit 0;
  package bar;
  use strict;
  use warnings;
  use Attribute::Memoize;

  sub func : Memoize {
    print "func runs\n";
    return 123;

To use modules that use Attribute::Memoize, don't require; use it. That holds true for most Attribute::* modules.


Dan Kogai, <dankogai+cpan at>


Copyright 2008 Dan Kogai. 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

Perl Hacks, isbn:0596526741