Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: cross scope gotos?

by moritz (Cardinal)
on Apr 06, 2010 at 11:29 UTC ( #833020=note: print w/replies, xml ) Need Help??

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 (Bishop) 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();


      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...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://833020]
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2018-04-22 21:21 GMT
Find Nodes?
    Voting Booth?