Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by Anonymous Monk
on Jan 16, 2013 at 17:23 UTC ( #1013621=note: print w/ replies, xml ) Need Help??


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

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 :)


Comment on Re^2: How does 'goto LABEL' search for its label?
Download Code
Re^3: How does 'goto LABEL' search for its label?
by LanX (Canon) on Jan 16, 2013 at 17:46 UTC
    > > 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.
        Constructs that require initialization such as a subroutine or a foreach loop can't be deprecated now because they never worked.

        Moritz showed a jump into a "construct" (an if block) w/o deprecation warning.

        So which blocks or "pieces of syntax made up of smaller pieces" exactly do belong to that set of now deprecated "constructs" is not defined within the perldocs.

        I hope you are aware that many kinds of loops are not foreach -loops.

        Cheers Rolf

Re^3: How does 'goto LABEL' search for its label? (yow?)
by tye (Cardinal) on Jan 17, 2013 at 03:01 UTC

    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

        I didn't say it should be disallowed. I said it is squirrely. In particular, it is easy to see doing this by accident. So it is appropriate to issue a warning which even provides the opportunity to add a line indicating "yes, I really did intentionally use 'goto' to exit this subroutine".

        And die is a less squirrely way to unwind a stack.

        - tye        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2014-08-30 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls