home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5880 < prev    next >
Encoding:
Internet Message Format  |  1992-09-11  |  2.7 KB

  1. Path: sparky!uunet!world!ora.com!minya!jc
  2. From: jc@minya.UUCP (John Chambers)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: die hook
  5. Message-ID: <1362@minya.UUCP>
  6. Date: 11 Sep 92 19:19:20 GMT
  7. References: <Aug.13.17.56.21.1992.22441@piecomputer.rutgers.edu> <1992Aug27.052530.11544@cs.ubc.ca>
  8. Lines: 67
  9.  
  10. In article <1992Aug27.052530.11544@cs.ubc.ca>, phillips@cs.ubc.ca (George Phillips) writes:
  11. > In article <1992Aug15.030457.21382@netlabs.com> lwall@netlabs.com (Larry Wall) writes:
  12. > >The code I'm developing for Perl 5 already has two magical subroutines
  13. > >in it called BEGIN and END.  They're so magical that you don't even have
  14. > >to put "sub" in front of them.  BEGIN is automatically called before
  15. > >the program starts, and END is automatically called just before the
  16. > >program exits, no matter how it exits.
  17. > Do you get a magical BEGIN and END for each package or just one set for
  18. > the entire program?  Seems like it would be a great way for required
  19. > subroutine modules to install cleanup routines without extra bookkeeping
  20. > on the part of the main program.  The BEGIN subroutine may be a problem
  21. > since the earliest it could run would be at require time, but then
  22. > again required files already have BEGIN.
  23.  
  24. Oh, no! If you do that, you'll shoot down my  most  favorite  pro-goto
  25. argument, and then I'll have to start writing goto-less code!
  26.  
  27. The argument uses examples of the form:
  28.  
  29. foo(bar)
  30. {    int r = 0;
  31.  
  32.     ...
  33.     for (...) {
  34.         if (...) {    /* or some other deeply-nested control structure */
  35.             ...
  36.             if (...)
  37.                 goto done;
  38.             ...
  39.         } else {
  40.             ...
  41.             if (...)
  42.                 goto fail;
  43.             ...
  44.         }
  45.     }
  46.     goto done;
  47. fail:
  48.     fprintf(stderr, ... );
  49.     r = -1;
  50. done:
  51.     if (debug > 4)
  52.         fprintf(stderr, ...);
  53.     return r;
  54. }
  55.  
  56. The idea, of course, is that deep down in a nested pile of code,  some
  57. condition  arises  that  means  you  should just get the hell out in a
  58. hurry, either because you have a disaster on your hands, or  you  have
  59. done  all  that  needs  to  be done (or have found what you're looking
  60. for).  It's a real pain (and typically requires wasting space and time
  61. -  both  cpu  and  debugging)  to  wend  your way out of the code in a
  62. structured manner; you just want to abort the current routine. You can
  63. use  "return",  but  that  doesn't give you any way to clean up, print
  64. failure or debug messages, and so on.
  65.  
  66. In most programming languages, the only simple, elegant, and  readable
  67. solution to this is a goto, as shown above.  If the proposed BEGIN/END
  68. clauses are added to perl programs and subroutines, then where will  I
  69. find a problem that requires goto for an elegant solution?
  70.  
  71. -- 
  72. All opinions Copyright (c) 1992 by John Chambers. Inquire for licensing at:
  73. 1-617-647-1813 ...!{bu.edu,harvard.edu,eddie.mit.edu,ruby.ora.com}!minya!jc 
  74. --
  75. Pensu tutmonde; agu loke.
  76.