Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Re (tilly) 4: Paradigm Shift - when to use goto

by TheDamian (Priest)
on Nov 18, 2001 at 03:21 UTC ( #126056=note: print w/replies, xml ) Need Help??


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

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. ;-)

Replies are listed 'Best First'.
Re^2: Re (tilly) 4: Paradigm Shift - when to use goto
by JadeNB (Chaplain) on Nov 04, 2009 at 04:36 UTC

    To shed my vast light on the discussion, 8 years later: Is this

    replacing the current subroutine via a goto &othersub (rare),
    really true? I thought that goto was the only way to get proper tail-call recursion, which means that those of us with functional backgrounds who aren't so fond of iteration would be using it a lot.

    Incidentally, nothingmuch has recently offered Sub::Call::Tail for those who recoil from the syntactic ugliness of the goto-based set-up. Given your contributions in the past, I imagine you have your own way of twisting tail-calling gotos into shape when the occasion warrants. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://126056]
help
Chatterbox?
[ambrus]: As for TeX, I'm not trying to discourage anyone from writing carefully beautifully typeset documents, in maths or outside. But most people aren't willing to do that, and will spend only little time about the formatting,
[ambrus]: and try to leave everything else to automated systems without checking how what they write came out format-wise, and for those people, discounting the part about journals with a specific format above,
[ambrus]: just blindly recommending to use LaTeX is a bad idea now.
[Discipulus]: I havery limited needs in such field. Sometimes (for children party invitations) i draw something by hand and I scan it to include into an ms doc.
Discipulus havery can be a good neologism for: have very..;=)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2017-09-26 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (293 votes). Check out past polls.

    Notices?