Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How does 'goto LABEL' search for its label?

by LanX (Canon)
on Jan 16, 2013 at 16:26 UTC ( #1013610=note: print w/ replies, xml ) Need Help??


in reply to How does 'goto LABEL' search for its label?

Do you have a clear definition for "construct"?

from perlglossary

construct As a noun, a piece of syntax made up of smaller pieces.

I think in this context it's supposed to mean "block of a loop".

IMHO this discussion helps: cross scope gotos?

From what I remember labels are only visible from the actual "frame" and parent frames.

Frames are constructs which need an initialization like subs or for-each-loops (remember the aliased loop-var!).

That means you can't jump into these constructs and bypass initialization.

But if you call another sub from within such a frame, that means after run-time initialization, you can goto back.

So lables are a run-time thing. They belong to the dynamic scope like localized vars.

EDIT: I think the deprecation is supposed to disallow jumps into other kinds of loops like while or c-style fors , which have a "logical" but no "physical" initialization like frames.

HTH!

Cheers Rolf

) remember that naked blocks are internally just while-loops


Comment on Re: How does 'goto LABEL' search for its label?
Select or Download Code
Re^2: How does 'goto LABEL' search for its label?
by Anonymous Monk on Jan 16, 2013 at 17:23 UTC

    I think in this context it's supposed to mean "block of a loop".

    goto says that literally

    IMHO this discussion helps...

    The whole time I was writing Re: How does 'goto LABEL' search for its label? I was thinking "dynamic scoping" but didn't have an example, and here it is

    $ perl -wE " sub F { G(); LF: say 6; } sub G { say 2; goto LF; } G; " 2 Can't find label LF at -e line 1. $ perl -wE " sub F { return G(); LF: say 6; } sub G { say 2; goto LF; +} F" 2 6

    yow :)

      > > I think in this context it's supposed to mean "block of a loop".

      > goto says that literally

      Really? Where?

      And I hate the fact that it uses the term "construct" in two different meanings!!!

      I'd really love to have the $cash to hire a crowd of math students to look thru the perldocs to clean up the wording.

      Cheers Rolf

        :) In the docs, in goto, in the part i quoted, the part in bold, the part that says: any construct that requires initialization, such as a subroutine or a foreach loop.

      I sympathized with the "yow" remark because it looked like the difference between finding the label and not finding the label was whether "return" was used. But that was a red herring.

      The real difference is whether the one-liner ends with "G" or with "F". The inclusion of the 'return' keyword makes no difference (but lacking the 'return' when calling "F" makes the reason for the output less obvious).

      Once I noticed the single-character difference off at the end of the long lines, then I lost sympathy for a "yow" response because the behavior is exactly as I would have expected from the documentation. And, it seems a reasonable restriction.

      Now, I think using 'goto' to leave a subroutine is a rather squirrely technique. I would much prefer that it produced a warning so you'd have to write such squirrely code more like:

      sub F { return G(); LF: say 6; } sub G { say 2; no warnings 'exiting'; goto LF; } G();

      Indeed, I'm not sure why the -w you used fails to trigger this:

      =item Exiting subroutine via %s (W exiting) You are exiting a subroutine by unconventional means, such as a goto, or a loop control statement.

      - tye        

        Well return traditionally exits a subroutine, so the say wasn't supposed to happen without the goto

        Good idea about the warning

        > Now, I think using goto to leave a subroutine is a rather squirrely technique.

        It's a way to directly break out of a deep recursion.

        my $counter; sub rec { goto OUT if $counter++ >= 10; print $counter; rec(); # never reached } rec(); OUT:

        This can be handy to avoid the code-block after a recursive call w/o needing to check a status-flag.

        Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-09-18 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (126 votes), past polls