Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

AUTOLOAD mystery

by ysth (Canon)
on Jul 22, 2005 at 06:24 UTC ( #477121=perlmeditation: print w/replies, xml ) Need Help??

I shot myself in the foot (but only slightly) the other day and thought I'd share with you all.

I was testing a piece of code something like:

my $foo = $obj->foo() or die; my $bar = $foo->bar() or die; ... baz() or die; my @quux = $bar->quux(); for my $quux (@quux) { ... }
but many of the classes and subs called were not available. So I thought: no problem, I'll wrap a test framework around the code that provides a sub quux to return sample data and a sub AUTOLOAD to make sure things that need to return true. Since some of the things that need to return true are constructors, I'll make it sub AUTOLOAD { bless {} }.

And I got it running; the real subs that existed got called; the sub quux that provided fake data got called; everything else was effectively a noop. But there was a problem with the output, and I needed to run it in the debugger, and ran into a mystery that stumped me for a little while. You can try it yourself; run perl -d -we'sub AUTOLOAD { bless {} } foo()' and note that "s" stepping into foo seems ineffective; you can do it over and over again without terminating. And using "n" gets you a "100 levels deep in subroutine calls!" warning.

In retrospect, the problem was simply solved by an additional sub declaration:

Replies are listed 'Best First'.
Re: AUTOLOAD mystery
by rinceWind (Monsignor) on Jul 22, 2005 at 10:53 UTC

    Although single stepping gives the 100 levels message, setting a breakpoint on AUTOLOAD catches the problem and gives you enough information about what is going on. I've encountered this one myself, when I was doing "creative" things with AUTOLOAD.

    A more interesting and challenging problem is what happens when there is inheritance. Perl scans the @ISA tree and will call a sub there if it finds one. If there was no sub in the whole tree of name spaces, it then scans the @ISA tree for an AUTOLOAD sub. Have a peep inside the source code of my module Devel::Leak::Object for a convoluted example.


    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

Re: AUTOLOAD mystery
by wazoox (Prior) on Jul 22, 2005 at 09:54 UTC
    AUTOLOAD may bite :) Thanks for the warning!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://477121]
Approved by kvale
Front-paged by Tanalis
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2022-01-28 03:31 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (72 votes). Check out past polls.