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

Short and sweet examples:
sub a { print } sub b { local ( $_ ) = @_; goto &a } b("Hi\n");
prints nothing, but
sub a { print } sub b { local ( $_ ) = @_; a(@_) } b("Hi\n");
(changing only goto &a to a(@_)) prints Hi. It's not clear to me that Temporary Values via local() indicates that this is the expected behaviour. Is it happening because dynamic scoping and TCO are incompatible (is that even true?), or is it just an artifact of the guts of Perl, or … other?

UPDATE: Ha, I'm reading the wrong docs; shoulda looked at goto, as lakshmananindia and moritz indicated—thanks! Still, I wonder if there's a reason for this—it seems to me that the combination of dynamic scoping and TCO could be useful.
UPDATE 2: Thanks to anonymonk, I also noticed that I forgot the locals. No more 3 AM posting for me. :-)

Replies are listed 'Best First'.
Re: goto escapes local bindings
by moritz (Cardinal) on Aug 03, 2009 at 07:45 UTC
    To quote perldoc -f goto:
    The "goto-&NAME" form is quite different from the other forms of "goto". In fact, it isn’t a goto in the normal sense at all, and doesn’t have the stigma associated with other gotos. Instead, it exits the current subroutine (losing any changes set by local()) and immediately calls in its place the named subroutine using the current value of @_.

    So yes, that is to be expected

Re: goto escapes local bindings
by lakshmananindia (Chaplain) on Aug 03, 2009 at 07:45 UTC

    because it exists from the current subroutine after goto &a.

    Read the perldoc -f goto for more detail

    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


Re: goto escapes local bindings
by Anonymous Monk on Aug 03, 2009 at 07:46 UTC
    prints nothing, but

    It prints Hi for me, perl 5.8.9

      Thanks, you're right—in a post about the effect of local, I forgot to insert local in the code!
Re: goto escapes local bindings
by Anonymous Monk on Aug 03, 2009 at 20:30 UTC

    The whole point of goto ⊂ is to replace the current stack frame, while the whole point of local is to temporary save a variable's value on the stack.