Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I concur with everything tilly said above. To summarize, a goto is not considered harmful when you're:
  • replacing the current subroutine via a goto &othersub (rare),
  • autogenerating code that will never be seen or maintained directly (rarer),
  • implementing a flow-of-control that Perl doesn't support natively (rarest).

For me, the key point in tilly's reply is that virtually all other "legitimate" uses of a goto in other languages are made redundant in Perl by the availability of named loops.

Whereas in C/C++ you might reasonably have to write:

for (i=1; i<10; i++) { for (j=1; j<10; j++) { for (k=1; k<10; k++) { /* Process data[i][j][k] here */ if (data[i][j][k] < threshold) goto EOLOOPS; } } } EOLOOPS:
Perl has a much cleaner way to escape from a deep nesting:
LOOPS: for $i (1..10) { for $j (1..10) { for $k (1..10) { # Process $data[$i][$j][$k] here last LOOPS if $data[$i][$j][$k] < $threshold; } } }
Long ago, before I discovered the Way of the Camel, I used to rely on a couple of moderately evil preprocessor commands to give myself named loops in C/C++ too:
#define named(name) goto name; name##_break: if (0) name: #define break(name) goto name##_break; /* Which then allows you to write... */ named (LOOPS) for (i=1; i<10; i++) { for (j=1; j<10; j++) { for (j=1; j<10; j++) { /* Process data[i][j][k] here */ if (data[i][j][k] < threshold) break(LOOPS); } } }
Exploring how those #defines work -- and why they don't interfere with the semantics of normal (unnamed) break statements -- is left as an exercise for those of you who still follow the Dark Path. ;-)

In reply to Re: Re (tilly) 4: Paradigm Shift - when to use goto by TheDamian
in thread Paradigm Shift - Don't use strict by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-24 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found