home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / perl / 5329 < prev    next >
Encoding:
Internet Message Format  |  1992-08-14  |  2.0 KB

  1. Path: sparky!uunet!usc!news!netlabs!lwall
  2. From: lwall@netlabs.com (Larry Wall)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: die hook
  5. Message-ID: <1992Aug15.030457.21382@netlabs.com>
  6. Date: 15 Aug 92 03:04:57 GMT
  7. References: <Aug.13.17.56.21.1992.22441@piecomputer.rutgers.edu>
  8. Sender: news@netlabs.com
  9. Organization: NetLabs, Inc.
  10. Lines: 38
  11. Nntp-Posting-Host: scalpel.netlabs.com
  12.  
  13. In article <Aug.13.17.56.21.1992.22441@piecomputer.rutgers.edu> mende@piecomputer.rutgers.edu (Bob Mende Pie) writes:
  14. :    I am currently working on a set of programs that use common "required"
  15. : packages.  I use the die command in many of the different monules and
  16. : packages.  Depending on what main program I use to invoke the program, I
  17. : would possibly like to add a "hook" to the die command that would run a
  18. : subroutine before exiting.  It could be used for cleaning up lockfiles,
  19. : saving the state of arrays, whatever.  Since I can not be sure how my
  20. : modules will be called this hook may or not be in existance.   Is there a
  21. : subroutine that die will try to call when it is invoked.   If not this
  22. : would be a useful addition.   It could be as simple as the perl code:
  23. :     &'die if defined &'die;
  24.  
  25. The code I'm developing for Perl 5 already has two magical subroutines
  26. in it called BEGIN and END.  They're so magical that you don't even have
  27. to put "sub" in front of them.  BEGIN is automatically called before
  28. the program starts, and END is automatically called just before the
  29. program exits, no matter how it exits.
  30.  
  31. Not only does this give you your die hook, but it also gives you
  32. BEGIN and END blocks like awk.  So you'll be able to say things like:
  33.  
  34.     perl -pe 'BEGIN { $/ = "\r\n" } s/\r$//'
  35.  
  36. or
  37.  
  38.     perl -lane 'BEGIN { require "timelocal.pl" } print &timegm(@F)'
  39.  
  40. Note that it's a subroutine definition, so the order doesn't matter.
  41. You can say stuff in the order you think important, like:
  42.  
  43.     perl -pal -e 'BEGIN { $accum = 1 }' \
  44.           -e 'END { print $accum }'    \
  45.           -e '$accum *= $F[3]'
  46.  
  47. Awk has irrational hissyfits if you don't hide END down at the end.
  48.  
  49. Larry
  50.