Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^3: How does 'goto LABEL' search for its label? (yow?)

by tye (Sage)
on Jan 17, 2013 at 03:01 UTC ( #1013687=note: print w/replies, xml ) Need Help??

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

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        

Replies are listed 'Best First'.
Re^4: How does 'goto LABEL' search for its label? (yow?)
by LanX (Chancellor) on Jan 17, 2013 at 03:58 UTC
    > 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        

        > I didn't say it should be disallowed.

        I didn't say you did! :)

        Just wanted to show a motivation for this technique.

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

        ehm ... you mean within an eval block?

        Well tastes differ...

        EDIT: Hmm ... at least die can easily send a message...

        Cheers Rolf

Re^4: How does 'goto LABEL' search for its label? (yow?)
by Anonymous Monk on Jan 17, 2013 at 03:06 UTC

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

    Good idea about the warning

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1013687]
[1nickt]: I though the gamers like that because they bash the keys so hard.
[ambrus]: and I've seen motherboards with no fast expansion ports for a video card but built-in hardware RAID advertized as "gaming".
LanX has a shaming laptop
[ambrus]: 1nickt: my impression is that the gamers like the softer springs, because fast reaction time is more important to them then feedback from keypresses to recognize typos.
[1nickt]: Ah, I see. I did read some gamer mag reviews, and yes, they lamented the fact that laptops with no discrete video card are sold as "gaming" hardware.
[1nickt]: But, they do have red keyboard backlighting! And gargoyles on the front, or words like "Maxxx" here and there. They know their demographic!
[1nickt]: True gamers don;t buy Dells, HPs, or Lenovos, I think ;-)
[ambrus]: The keyboard I bought doesn't have backlighting. I specifically had to select for that, because so many keyboards these days have lights. Why would I want lights in the keyboard? I want lights in the room, such as on the ceiling, not in the keyboard.
choroba has a small USB lamp

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2017-03-27 13:47 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.