[comp.lang.perl] on exit functionality?

nagel@paris.ics.uci.edu (Mark Nagel) (01/17/90)

I was rewriting an old beaten-up shell script in perl (of course)
and noticed that there's no obvious way to duplicate sh's easy way
of creating an exit handler (using trap cmd 0).  Is there any way to
trap signal 0 in perl?  Or is there some other exit handler
mechanism?  I thought of appropriating some other signal and killing
myself with that signal number.  This seems messy, but I'll do it if
there's no other way...
-- 
Mark Nagel
UC Irvine Department of ICS   +----------------------------------------+
ARPA: nagel@ics.uci.edu       | Help!  Somebody!  I'm being oppressed! |
UUCP: ucbvax!ucivax!nagel     +----------------------------------------+

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (01/18/90)

In article <25B3E15F.25354@paris.ics.uci.edu> nagel@paris.ics.uci.edu (Mark Nagel) writes:
: I was rewriting an old beaten-up shell script in perl (of course)
: and noticed that there's no obvious way to duplicate sh's easy way
: of creating an exit handler (using trap cmd 0).  Is there any way to
: trap signal 0 in perl?  Or is there some other exit handler
: mechanism?  I thought of appropriating some other signal and killing
: myself with that signal number.  This seems messy, but I'll do it if
: there's no other way...

Why not just write a sub my_exit that does your cleanup and then calls
exit or die as appropriate?

If you're worried about also catching errors that cause an exit, just put the
whole script inside an eval:

eval <<'The_End';
[Your script here.]
The_End
&my_exit($@);

"Trap cmd 0" is just a poor man's substitute for a subroutine.  Just as "set"
is a poor man's substitute for an array.  Why anyone would want to program
in a language that only has one array, and that you can only index the
first 9 elements of...

Of course, I never write any sh programs...    :-)

Larry