home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!world!ora.com!minya!jc
- From: jc@minya.UUCP (John Chambers)
- Newsgroups: comp.lang.perl
- Subject: Re: die hook
- Message-ID: <1362@minya.UUCP>
- Date: 11 Sep 92 19:19:20 GMT
- References: <Aug.13.17.56.21.1992.22441@piecomputer.rutgers.edu> <1992Aug27.052530.11544@cs.ubc.ca>
- Lines: 67
-
- In article <1992Aug27.052530.11544@cs.ubc.ca>, phillips@cs.ubc.ca (George Phillips) writes:
- > In article <1992Aug15.030457.21382@netlabs.com> lwall@netlabs.com (Larry Wall) writes:
- > >The code I'm developing for Perl 5 already has two magical subroutines
- > >in it called BEGIN and END. They're so magical that you don't even have
- > >to put "sub" in front of them. BEGIN is automatically called before
- > >the program starts, and END is automatically called just before the
- > >program exits, no matter how it exits.
- >
- > Do you get a magical BEGIN and END for each package or just one set for
- > the entire program? Seems like it would be a great way for required
- > subroutine modules to install cleanup routines without extra bookkeeping
- > on the part of the main program. The BEGIN subroutine may be a problem
- > since the earliest it could run would be at require time, but then
- > again required files already have BEGIN.
-
- Oh, no! If you do that, you'll shoot down my most favorite pro-goto
- argument, and then I'll have to start writing goto-less code!
-
- The argument uses examples of the form:
-
- foo(bar)
- { int r = 0;
-
- ...
- for (...) {
- if (...) { /* or some other deeply-nested control structure */
- ...
- if (...)
- goto done;
- ...
- } else {
- ...
- if (...)
- goto fail;
- ...
- }
- }
- goto done;
- fail:
- fprintf(stderr, ... );
- r = -1;
- done:
- if (debug > 4)
- fprintf(stderr, ...);
- return r;
- }
-
- The idea, of course, is that deep down in a nested pile of code, some
- condition arises that means you should just get the hell out in a
- hurry, either because you have a disaster on your hands, or you have
- done all that needs to be done (or have found what you're looking
- for). It's a real pain (and typically requires wasting space and time
- - both cpu and debugging) to wend your way out of the code in a
- structured manner; you just want to abort the current routine. You can
- use "return", but that doesn't give you any way to clean up, print
- failure or debug messages, and so on.
-
- In most programming languages, the only simple, elegant, and readable
- solution to this is a goto, as shown above. If the proposed BEGIN/END
- clauses are added to perl programs and subroutines, then where will I
- find a problem that requires goto for an elegant solution?
-
- --
- All opinions Copyright (c) 1992 by John Chambers. Inquire for licensing at:
- 1-617-647-1813 ...!{bu.edu,harvard.edu,eddie.mit.edu,ruby.ora.com}!minya!jc
- --
- Pensu tutmonde; agu loke.
-