Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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.


Comment on Re: cross scope gotos?
Download Code
Re^2: cross scope gotos?
by LanX (Canon) 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...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://833020]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-09-19 03:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls