Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by tye (Sage)
on Jan 17, 2013 at 03:01 UTC ( [id://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 (Saint) 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-03-28 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found