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

As I already made clear, I use AUTOLOAD for very simple modules. In fact, I have never needed AUTOLOAD for anything else, so I have not attained the point of wanting to "save it". (What can I say? I'm a lowly script writer, not a heavy-hitting module meister.) I'm aware of the technique you describe, though I am sorry to say that I use it with AUTOLOAD:

sub AUTOLOAD { my $self = $_[ 0 ]; ( my $name = our $AUTOLOAD ) =~ s/.*:://; return if $name eq 'DESTROTY'; die "Unknown field: $name" unless exists $self->{ $name }; no strict 'refs'; *$AUTOLOAD = sub { my $self = shift; return @_ ? $self->{ $name } = shift : $self->{ $name + }; }; goto &$AUTOLOAD; }
(Yes, I posted a different version in another the reply, because I think it is more standard and easier to follow.)

So, please tell me, what are the things one should be saving AUTOLOAD for instead of wasting it on accessors?

Update: Bug in the original version fixed.

the lowliest monk

Replies are listed 'Best First'.
Re^7: Modules for autogenerating accessor/mutator methods
by diotalevi (Canon) on Jun 05, 2005 at 00:42 UTC
    Off the top of my head, delegation.

      That's an instructive example. I now see tilly's point much more clearly. Thanks.

      And in light of this, I think it is somewhat unfortunate that in the Perl literature accessor generation is so often presented as a prime example of the utility of AUTOLOAD.

      the lowliest monk

        In general, I would expect that almost no one should ever use AUTOLOAD. That's a feature for use when you don't know until call-time whether a given method is going to be valid or not. If you know this at compile time, do your sub generation then. If you know it at db connection time, do it then. Whatever. In general, the time to generate subs is not during the statement you're using it in.

        Consider what happens to the maintenance programmer (or just some debugger program) when a method can't be detected as existing until it has been called at least once. Its ugly and it isn't being nice.