perlmeditation
ysth
I shot myself in the foot (but only slightly) the other day and thought I'd share with you all.
<p>
I was testing a piece of code something like:
<code>
my $foo = $obj->foo() or die;
my $bar = $foo->bar() or die;
...
baz() or die;
my @quux = $bar->quux();
for my $quux (@quux) {
...
}
</code>
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 <code>sub AUTOLOAD { bless {} }</code>.
<p>
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 <code>perl -d -we'sub AUTOLOAD { bless {} } foo()'</code> 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.
<p>
In retrospect, the problem was simply solved by an additional sub declaration:
<spoiler>
<code>sub DESTROY {}
</code>
The objects that were created by AUTOLOAD would go out of scope and have DESTROY called, but since there was no DESTROY, AUTOLOAD would handle it. Then AUTOLOAD would return a new object which itself would need to be destroyed, ad infinitum (well, to 100 times).
</spoiler>