http://www.perlmonks.org?node_id=504750


in reply to Using AUTOLOAD to create class methods

AUTOLOAD does not only create subs, but is used to decide on which sub to call. So, you can either call an existing sub, or create one and then call it. to avoid confusion with caller() and stuff, usually goto is used instead of a simple call. But in your code, there is neither a call nor a goto in AUTOLOAD().

I think I saw a tutorial here somewhere, but the last time I was here was about 2.5 years ago, so there.


perl -e'$b=unpack"b*",pack"H*","59dfce2d6b1664d3b26cd9969503";\ for(;$a<length$b;$a+=9){print+pack"b8",substr$b,$a,8;}'
My public key

Replies are listed 'Best First'.
Re^2: Using AUTOLOAD to create class methods
by radiantmatrix (Parson) on Nov 01, 2005 at 22:21 UTC

    Yep, you're correct. Sometimes it's just an extra pair of eyes that helps. Thank you! I updated my node to reflect the solution.

    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law

      Regarding your update,

      • You didn't call the function in the else.
      • In fact, there's no real need to create a function in the else since the program will likely die if you get there.
      • You're keeping AUTOLOAD in the call stack by not using goto.
      • You're using a regexp where you could be using much fast rindex.

      See my (updated) reply below for an implementation without those problems.

        I did catch the first issue, see my reply to Ovid for the currently-working version (I'll update my top node eventually).

        As to the second point, I think you might have mis-read the code. The else is paired with if (DBI::db->can($op)) -- I don't die when that is false. Basically, if I try TestModule->prepar by accident, the else block is run because a database handle can't 'prepar'. My test suite verifies this.

        Tests:

        is ($obj->heekify, undef, 'Autoloading a bad sub fails'); is ($obj->error, 'Cannot autoload heekify', 'Error set correctly');

        And both pass under the code in the reply to Ovid I mentioned above (the first passes in the code you're referencing, but as you point out, the error wasn't set).

        As for the last two points, that is useful information for optimization. However, under the current form of usage, the whole script that calls this module only takes 3s to run. I will keep those tips handy for later, if optimization becomes an issue. Thank you -- I always love thinking about stuff like that!

        <-radiant.matrix->
        A collection of thoughts and links from the minds of geeks
        The Code that can be seen is not the true Code
        "In any sufficiently large group of people, most are idiots" - Kaa's Law