in reply to Re^5: Modules for autogenerating accessor/mutator methods
in thread Modules for autogenerating accessor/mutator methods

It goes something like this:

sub AUTOLOAD { my $self = $_[ 0 ]; ( my $name = our $AUTOLOAD ) =~ s/.*:://; return if $name eq 'DESTROTY'; die "Unknown field: $name" unless exists $OK_FIELDS{ $name }; return @_ ? $self->{ $name } = shift : $self->{ $name }; }
As you can infer from the above, this version assumes that the instances of this class are implemented as hash refs. However, I don't like using AUTOLOAD when I have a lot of inheritance going on (but then, I don't use inheritance much).

I've seen many descriptions of this technique. You can find one in this section of perltoot. Also, The Perl Cookbook, the Alpaca book, Object Oriented Perl discuss the use of AUTOLOAD to generate accessors. I'm sure this list is far from exhaustive.

the lowliest monk

Replies are listed 'Best First'.
Re^7: Modules for autogenerating accessor/mutator methods
by wazoox (Prior) on Jun 05, 2005 at 07:34 UTC
    Thanks, I'll look into the docs. However I'm too young in OOP to try anything like this at the moment :)