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


in reply to cross scope gotos?

I think you are confusing two things: the normal goto, and goto &subroutine;, which are really two entirely different beasts. The latter is really tailcall. It replaces the current call frame, thus removing the current frame from the dynamic scope
what exactly is dynamic scope?
Dynamic scope is the current scope, plus the scope of the caller, plus the scope of the caller's caller etc.

Replies are listed 'Best First'.
Re^2: cross scope gotos?
by LanX (Saint) on Apr 06, 2010 at 12:21 UTC
    > It replaces the current call frame, thus removing the current frame from the dynamic scope

    which is part of the answer...

    But it's not the scoping visibility of the label variable which hinders the jump back to work like you can see in the following example:

    my $label; sub jump { goto $label ;# jump back}; sub cont { for $i (0..3) { for $j ("a".."c") { print "$i,$j\t"; $label=NESTED; #jump(); # works goto &jump; # fails } NESTED: } } cont();

    It's the point that gotos are only possible to destination within the scope of the call chain.

    BUT It may not be used to go into any construct that requires initialization, such as a subroutine or a "foreach" loop.

    Since jump() adds a new call frame, there is no more initialization of the old frame needed when jumping back, just closing the new frames.

    But with goto &jump the old call frame is replaced and all links to possible labels there!

    So jumping back would imply a re-initialization of this frame, which is not possible...

    I got it, thanks for discussing it! :)

    Cheers Rolf

      Maybe this code makes it clearer:

      sub jump { print "in\n"; goto BACK }; sub jump2 {goto &jump } sub cont { for $i (0..1) { for $j ("a".."b") { print "$i,$j\t"; jump2(); # works #goto &jump; # fails BACK: } } } cont();

      OUTPUT

      0,a in 0,b in 1,a in 1,b in
      in simple words:

      It's not only possible to jump within the same scope, but also into the scope of previous ("outer") call frames.

      (Pity I thought I found an easy pattern for continuations...)

      Cheers Rolf

        No serious application whatsoever for this nonsense...