kiat has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I've not used AutoLoader before. I read that its purpose is to defer the loading of some subroutines until they are used rather than loading them all at once.

Is it always good to use AutoLoader then? Currently, I've code as follows:

package Module2; use autouse Module1 => qw(some_func); sub func1 { some_func(); ... } sub func2 { ... } 1;
How does "autouse" work with AutoLoader?

What are the things to look out for when using AutoLoader?

Thanks in anticipation :)

Edited AutoLoad to read AutoLoader. Thanks to Sporty!

Replies are listed 'Best First'.
Re: AutoLoader - when to use it?
by tilly (Archbishop) on Oct 25, 2004 at 14:58 UTC
    Um, AutoLoader and autouse are different modules that do somewhat related things. Both try to delay actually loading code to runtime. If you might not need to load the code, that can be a win. If you do load the code then you'd have beeen far better off doing it up front. (There are many penalties with doing things at runtime, for instance you blow the functionmethod cache.) In general I'd lean towards using neither, seeing both as Premature Optimization until the need is proven, and the win is demonstrable.

    Of the two, I'd prefer autouse because it doesn't complicate the development process nearly as much. Also AutoLoader installs an AUTOLOAD (see perlsub for details) and those get in the way in many scenarios (particularly when you get into OO Perl).

    UPDATE: I have no idea why I wrote function when I meant method...

      Thanks, tilly!

      That was really helpful :) I'll stick to using autouse then.

Re: AutoLoader - when to use it?
by hardburn (Abbot) on Oct 25, 2004 at 14:32 UTC

    The AUTOLOAD subroutine should never be used unless you have a very specific reason that cannot be done any other way.

    The autouse module is a different case. It allows you to defer loading of the subroutine code until runtime, thus saving some memory and time during the compile phase. This is occasionally useful, but you could easily spend a long career as a Perl programmer without ever using it.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

      Thanks, hardburn!

      I'm glad I checked it out. I thought it was something that should be used readily because of its benefits.

Re: AutoLoader - when to use it?
by exussum0 (Vicar) on Oct 25, 2004 at 14:47 UTC
    Autoload can be used for at least one really cool thing. Proxy objects. For instance, if you have a pattern to how you code calls to methods/functions that do db calls, say, selectXXX for selects, insertXXXX for inserts, and you want to track how a particular object/package gets treated, proxy it around an autoloader.

    1. Create an autoloader.
    2. Regexp against the method name.
    3. If the regexp matches, do something.
    4. Call the original function.

    Another pattern, is if you do any setXXX type things, write it out to the db. Now mind you, this may be quite sub optimial, but there have been instances where it is useful. Such that, if you have a UI, and someone changes the data, you dont' have to have the UI invoke something to save, but the proxy object would save automagically. It, in some ways, make your API a little more user friendly.. but it can and will take a performance hit if not done properly. Write through cache or something may be of use in that instance.

    ----
    Then B.I. said, "Hov' remind yourself nobody built like you, you designed yourself"

      I believe that the poster was asking about the AutoLoader module, not the uses of AUTOLOAD routines.
        100% correct. The poster used autoload in his/her post once, by accident, I'm sure, and I followed suit. Lesson learned, don't mix up the two and they are rather closley named. Fear the reaper?

        ----
        Then B.I. said, "Hov' remind yourself nobody built like you, you designed yourself"

Re: AutoLoader - when to use it?
by jcoxen (Deacon) on Oct 25, 2004 at 17:53 UTC
    Interesting timing on this question. I received the latest IBM developerWorks newletter in today's email and inside I find an article about optimizing Perl.

    One of the suggestions was to use AutoLoader. The full article is available here. Registration is required so I can cut and paste the pertinant portion of the article if it's OK with the powers that be. I'll avoid any copyright problems for now by not quoting from the article here but the gist of the argument for AutoLoader is that it acts as a sort of dynamic loader for modules and, when used in conjunction with AutoSplit, only loads the functions you actually use which shortens load and compile time and shrinks program size.

    Could anyone comment on this? I've never used AutoLoader so I have no knowledge of nor opinion about the wisdom (or lack thereof) about doing this.

    Jack

    Cogito cogito, ergo cogito sum
    (I think I think, therefore I think I am)
      That article is of horrible quality. I'd suggest that you disregard most of what it has to say.

      About AutoLoader, if you won't use most of the functions, then it should be faster. If you use a significant fraction of them, then your code will be slower. It makes development more complicated. Furthermore load time may or may not be what you want to focus on. For instance in a web environment you really want to use mod_perl and preload as much as feasible - so AutoLoader is definitely a loss.

        That article is of horrible quality.
        Maybe that is why it (and the rest of the site) is behind a password threshold? :)