This post is in response to all the very interesting responses i got to my recent AUTOLOAD post: AUTOLOAD cascade

Basically I'm trying to get an understanding on what people use AUTOLOAD for in general, and what is considered good practice (holy war to follow). There are a lot of *interesting* things that can be done with AUTOLOAD, but as i get deeper, i keep finding conflicting opinions / reasons as to AUTOLOAD or not to AUTOLOAD.

It's a nice timesaver to use it to handle simple accessor / mutators without having to write them explicitly:

package some_package; ... { my %simple_data = ( 'name' => 1, 'age' => 1, ... } sub AUTOLOAD { my $self = shift; (my $method) = $AUTOLOAD) =~ s/.*:://; return if $method eq 'DESTROY'; if ( exists $simple_data{$method} ) { if ( defined $_[0] ) { $self->{$method} = $_[0]; } return $self->{$method}; } } }
But if you're inheriting from this class, you definitely need to know too much detail about the implementation, particularly that its using AUTOLOAD for some accessor / mutators.

As was sort of talked about in my other thread, perhaps a better way to do this is along the lines of:

package some_package; ... { BEGIN { my @simple_data = qw(name age ...); no strict 'refs'; foreach my $data (@simple_data) { *{$data} = sub { my $self = shift; my ($value) = @_; if ( defined $value ) { $self->{$data} = $value; } return $self->{$data}; } } use strict; }
This way, these accessor / mutators are in the symbol table, and will be looked up faster, will be found by can(), etc. This avoids the inheritance problem from the other post too.
What else do people use AUTOLOAD for? I have used it as a delegator if an object will contain a collection of other objects, if the circumstances are correct.

Perhaps part of my problem is that a lot of my ideas come from Damian Conways 'Object Oriented Perl' (Manning 2000), which is perhaps outdated.

Or maybe AUTOLOAD can be used for a lot of neat tricks in little scripts, but becomes quite annoying for large software projects.


In reply to AUTOLOAD - the good, the bad, and the ugly by shemp

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.