Christopher Grau wrote:
I may be veering off-topic, but I've started doing similar things in my
own code (generating accessor methods via AUTOLOAD).  I ended up writing
`Class::Autoload,' which I intend to upload to CPAN when I'm done with
documentation and testing.
Mine was very simple and didn't need to handle any odd cases, which was why I didn't bother using a CPAN module. I would probably check Class::MethodMaker before doing it this way again.

I had this in a shared class:

Package Util;

sub build_accessors {
my ($class, $pkg, $attr_names) = @_;
no strict 'refs';
foreach my $attr_name (@{$attr_names}) {
*{$pkg . "::$attr_name"} = sub { return $_[0]->{$attr_name}; };
}
}


And then I would put something like this in the classes that needed it:

BEGIN {
my @attr_names;
@attr_names = qw(
foo
bar
baz
);
Util->build_accessors(__PACKAGE__, \@attr_names);
}

- Perrin



Reply via email to