http://qs1969.pair.com?node_id=342922


in reply to Why breaking can() is acceptable

I'd prefer that people stop using AUTOLOAD when they don't need to. Specifically:

sub AUTOLOAD { my $self = shift; $self->{$AUTOLOAD} = shift if @_; $self->{$AUTOLOAD}; }

The actual code could be more complex, such as checking $AUTOLOAD against a list of allowed attributes, but the idea of runtime handling of accessors/mutators is the same. If you must have autogenerated accessors/mutators, please use symbol table manipulation instead if at all possible:

# Could be wrapped in a BEGIN block if you wanted to foreach my $field (qw/ foo bar baz /) { *$field = sub { my $self = shift; $self->{$field} = shift if @_; $self->{$field}; }; }

This is faster, is as memory-efficient as things get around Perl, and (IIRC) doesn't break can. Overall, it's even better to avoid spreading accessors/mutators all over your class design in the first place, but that's a seperate issue.

I understand the above isn't the only use of AUTOLOAD, but it's by far the most common. So if you're doing that, please stop and look for alternatives. Or better, fix your class design.

----
: () { :|:& };:

Note: All code is untested, unless otherwise stated