If you're adept, you can use them to install subroutines on the fly, the first time they're called (instead of compiling everything at the beginning). Not only does that save you a bit of typing if you have many similar methods (accessors -- get_variable, set_variable), but if your program only uses a few at a time, you get a bit of a speed benefit.

If you were really tricky-minded, you could add new subroutines on the fly, with some eval magic. Rough, untested code follows, as I'm not completely positive of the AUTOLOAD syntax without checking a book:

sub AUTOLOAD { next if $AUTOLOAD =~ /DESTROY/; # be safe my $sub_text = shift; $AUTOLOAD =~ s/.*:://; eval qq|*{$AUTOLOAD} = sub { $sub_text };| # the following goto may require # no strict 'refs'; goto &$AUTOLOAD; }
That's deep magic, and you'd better understand exactly what it does before you even think about using it. (There are only a couple of situations where I'd consider using it, and it would most definitely NOT be available to anyone off the street. Big security risk here.)

Update: I added the goto line because jlistf made me think of it.


In reply to RE: autoload and selfload by chromatic
in thread autoload and selfload by jlistf

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.