My rule of thumb for deciding when AUTOLOAD is appropate is: if I can tell a good request from a bad request (i.e., a request that I will raise an error upon) by looking only at the value of $AUTOLOAD, then I can surely find a more explicit way of generating the subs without AUTOLOAD. In these cases, AUTOLOAD is only saving me a little bit of typing.

For instance, you could use AUTOLOAD to be lazy about providing object accessors. But I prefer this more explicit way:

## only generate explicitly listed subs: BEGIN { no strict 'refs'; my @fields = qw/ name age favorite_color ... /; for my $f (@fields) { *$f = sub { ## argument, error checking, etc return $_[0]->{$f}; }; } } ## instead of sub AUTOLOAD { (my $func = $AUTOLOAD) =~ s/.*::/; ## argument, error checking, etc.. ## is $func a valid method?? return $_[0]->{$func}; }
I got that idea from this post a while back. In my opinion, it's just a better way of "lazily" providing accessors than with AUTOLOAD. Although it's a little more typing, it's still much less typing that writing out every sub by hand, and it better expresses my intent. (i.e, I don't want to waste time filtering out every misspelled method call)

Sometimes this isn't feasible -- sometimes it doesn't make sense to try anticipating every possibility. Other times you must cover different sets of method names depending on various contexts (i.e., you must look at the passed data and $AUTOLOAD before you know if it's a valid request). For instance, a while back I wrote a bare-bones object-persistence class à la Class::DBI. Because it was a superclass for many other object classes, I had to use AUTOLOAD for the accessors/mutators, because a name method might be ok for one subclass, but not another. This is where the power of AUTOLOAD really shines.

blokhead


In reply to Re: is autoload bad style? by blokhead
in thread is autoload bad style? by racer_x

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.