home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!news!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Newsgroups: comp.lang.perl
- Subject: Re: die hook
- Message-ID: <1992Aug15.030457.21382@netlabs.com>
- Date: 15 Aug 92 03:04:57 GMT
- References: <Aug.13.17.56.21.1992.22441@piecomputer.rutgers.edu>
- Sender: news@netlabs.com
- Organization: NetLabs, Inc.
- Lines: 38
- Nntp-Posting-Host: scalpel.netlabs.com
-
- In article <Aug.13.17.56.21.1992.22441@piecomputer.rutgers.edu> mende@piecomputer.rutgers.edu (Bob Mende Pie) writes:
- :
- : I am currently working on a set of programs that use common "required"
- : packages. I use the die command in many of the different monules and
- : packages. Depending on what main program I use to invoke the program, I
- : would possibly like to add a "hook" to the die command that would run a
- : subroutine before exiting. It could be used for cleaning up lockfiles,
- : saving the state of arrays, whatever. Since I can not be sure how my
- : modules will be called this hook may or not be in existance. Is there a
- : subroutine that die will try to call when it is invoked. If not this
- : would be a useful addition. It could be as simple as the perl code:
- : &'die if defined &'die;
-
- 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.
-
- Not only does this give you your die hook, but it also gives you
- BEGIN and END blocks like awk. So you'll be able to say things like:
-
- perl -pe 'BEGIN { $/ = "\r\n" } s/\r$//'
-
- or
-
- perl -lane 'BEGIN { require "timelocal.pl" } print &timegm(@F)'
-
- Note that it's a subroutine definition, so the order doesn't matter.
- You can say stuff in the order you think important, like:
-
- perl -pal -e 'BEGIN { $accum = 1 }' \
- -e 'END { print $accum }' \
- -e '$accum *= $F[3]'
-
- Awk has irrational hissyfits if you don't hide END down at the end.
-
- Larry
-