Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by LanX (Canon)
on Jan 17, 2013 at 03:58 UTC ( #1013697=note: print w/ replies, xml ) Need Help??


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

> 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


Comment on Re^4: How does 'goto LABEL' search for its label? (yow?)
Select or Download Code
Replies are listed 'Best First'.
Re^5: How does 'goto LABEL' search for its label? (yow!)
by tye (Cardinal) on Jan 17, 2013 at 04:07 UTC

    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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (17)
As of 2015-07-31 17:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (279 votes), past polls