That goto discussion seems to be saying that goto a subroutine ought only to be used in the most magical circumstances.
More to the point is to consider exactly when you would need to use it, and when you care. The circumstances are when you want to fake "caller" to the sub being called, i.e. make your current subroutine invisible.
One circumstance when you would do this is in an AUTOLOAD subroutine that is generating new subroutines on the hoof (or loading them in from files). Suppose we have a package Foo and function bar, which does not exist at the start of the program. The first call to Foo::bar results in a call to Foo::AUTOLOAD, with $Foo::AUTOLOAD set to 'Foo::bar'. The AUTOLOAD code creates a new subroutine and puts it into package name space as Foo::bar, so future calls to Foo::bar call this sub directly. We want the first call to Foo::bar not to see its caller as Foo::AUTOLOAD, but as the real caller. In order to achieve this, AUTOLOAD does a goto &Foo::bar once it has finished.
Other times when you might need to fake "caller" are more arcane, such as what Hook::LexWrap does internally. You also need to use these techniques if you are
writing a debugger, profiler or such like.
Hopefully this has explained the "magic" in this case.
-- I'm Not Just Another Perl Hacker
|