NAME
Attribute::Method - No more 'my $self = shift;'
SYNOPSIS
package Lazy;
use strict;
use warnings;
use Attribute::Method qw( $val );
# pass all parameter names here
# to make strict.pm happy
sub new : Method {
bless { @_ }, $self
}
sub set_foo : Method( $val ){
$self->{foo} = $val;
}
sub get_foo : Method {
$self->{foo};
}
#....
DESCRIPTION
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.
BUGS
None known so far. If you find any bugs or oddities, please do inform the author.
CAVEAT
The following does not work.
- foo.pl
-
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;
- bar.pm
-
package bar; use strict; use warnings; use Attribute::Memoize; sub func : Memoize { print "func runs\n"; return 123; } 1;
To use modules that use Attribute::Memoize, don't require
; use
it. That holds true for most Attribute::* modules.
AUTHOR
Dan Kogai, <dankogai+cpan at gmail.com>
COPYRIGHT
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.
SEE ALSO
perl(1), Attribute::Handlers
Perl Hacks, isbn:0596526741