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


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

Replies are listed 'Best First'.
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 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        

        > 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

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

        Good idea about the warning

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