home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5476 < prev    next >
Encoding:
Internet Message Format  |  1992-08-25  |  3.1 KB

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