home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!news!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Newsgroups: comp.lang.perl
- Subject: Re: Using Gotos?
- Message-ID: <1992Aug25.204023.10211@netlabs.com>
- Date: 25 Aug 92 20:40:23 GMT
- References: <DAVE.92Aug25182357@pipi.iis.u-tokyo.ac.jp>
- Sender: news@netlabs.com
- Distribution: comp
- Organization: NetLabs, Inc.
- Lines: 55
- Nntp-Posting-Host: scalpel.netlabs.com
-
- In article <DAVE.92Aug25182357@pipi.iis.u-tokyo.ac.jp> dave@pipi.iis.u-tokyo.ac.jp (David Wuertele) writes:
- : P.S. I have been equating "next LABEL;" with GOTO, perhaps mistakenly.
- : Please flame if I'm being naive.
-
- OK, prepare to be crispy crittered. :-)
-
- The generic "goto" has been rightly condemned as obfuscatory. You really
- have no idea where the label is, or whether you're jumping into the middle
- of some other control flow that would be better off without your presence.
- It's trivial using goto to write a control flow that is too complicated
- to think about except when high on Jolt Cola (and maybe not even then).
-
- By contrast, certain customary uses of goto (and goto-like constructs)
- are easy to understand because they don't violate the shape of the
- control flow, but merely short-circuit it.
-
- In the early days of structured programming, one stated goal was that
- every block should have only one entry and one exit. This is half
- right. Or perhaps twice right. Every block *should* have only one
- entry. But the one-exit idea is bogus, because most logical branch
- structures are in fact tree structures, and not linear. A block needs
- to categorize things, and once it has categorized things, it's more or
- less done. It shouldn't be forced to uncategorize things again.
-
- There is implicit recognition of this in a language like C. Notice
- that the "return" statement can be used anywhere in a subroutine.
- There is also the notion of "returning" from the current iteration of a
- loop (via "continue") to start the next iteration, and "returning" from
- the loop as a whole (via "break") for some reason that wouldn't have
- been apparent at the top of the loop. In real life, many control
- structures are tree-shaped, so many loops have multiple (valid!) exits
- or short-circuit conditions.
-
- Perl just takes this notion one step further than C, by allowing you to
- exit or short-curcuit a named loop. Note however that, unlike with
- goto, you KNOW that you're passing control out to an enclosing
- construct. That fact, together with the fact that Perl encourages you
- to use meaningful loop labels, lets you write near-English statements
- such as:
-
- next LINE if /^$/; # skip blank lines
- next LINE if /^#/; # skip comment lines
-
- last FILE if /^end$/; # reached logical end of file
-
- This is much better even than some proposed C extensions that would let
- you specify how many levels you're breaking (as in "break 2"). The
- Perl construct maps better onto your mental constuct of what you're
- iterating over. You don't have to count enclosing blocks, or even find
- the block label. You already know it, presuming you named it sensibly.
-
- So there's a good reason you can't rewrite the "findword" example easily:
- I'm such a lazy critter that I already did it the easy way! :-)
-
- Larry
-