Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^2: cross scope gotos?

by LanX (Bishop)
on Apr 06, 2010 at 12:21 UTC ( #833031=note: print w/replies, xml ) Need Help??

in reply to Re: cross scope gotos?
in thread cross scope gotos?

> 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

Replies are listed 'Best First'.
Re^3: cross scope gotos?
by LanX (Bishop) on Apr 06, 2010 at 12:36 UTC
    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...
        > No serious application whatsoever for this nonsense...

        sigh, YOU again...

        you may use it for

        1. easily leaving deeply nested recursions, without the need to return step by step
          perl -e ' my $x; sub rec { goto STOP if ++$x>10; print $x; rec()};rec();STOP:' 12345678910
        2. easily defining specific labels and jump-commands in DSLs (Domain Specific Languages) and encapsulating all the logic within the subs.

          For instance if you want to emulate and/or compile an assembler language you can easily implement it as valid perl-code then easily using Perl as macro language:

          use Assembler::6502; # fictitious module # implementing Mnemonics as subs MARK: LDX 10; ADD 3; macro1(); # Perl sub to include/call mnemonics JSR SUB2; JMP MARK; SUB2: ... ... RET

        serious enough?

        Cheers Rolf

        UPDATE: added examples

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2018-11-14 02:16 GMT
Find Nodes?
    Voting Booth?
    My code is most likely broken because:

    Results (161 votes). Check out past polls.