http://qs321.pair.com?node_id=522838


in reply to Self creating OO Module field accessors...

There are a gazillion Class:: modules that will do that for you. What you also can do is:
for my $prop (qw [field1 field2 field3]) { eval sprintf <<'END', ($prop) x 3; sub %s { my $self = shift; if (@_) {$self->{%s} = shift}; return $self->{%s}; } } END }
Then you won't have any runtime penalty due to AUTOLOAD, or whatever runtime solution your Class:: choice uses.

Or you could use a smart editor that allows you to create an accessor using a macro.

Personally, if I find myself making enough accessors in a class that I start contemplating automating it, I wonder whether I really need an object, or whether I'm just creating a glorified struct. It's usually the latter - and Perl already has an excellent data structure to use as a struct: it's called a hash.

Perl --((8:>*