PERL: Trapping EXIT

Hey Everyone,

Just starting with PERL (5.8.2) after years of KSH. Is there a way to trap the exit as you can in KSH (i.e., "trap EXIT_SCRIPT EXIT")?

Thanks in advance for any help,
gsatch

Don't think of it so much as "trap", but as a signal handler. Perl uses a simple signal handling model: the %SIG hash contains names or references of user-installed signal handlers. These handlers will be called with an argument which is the name of the signal that triggered it.

For example, catch an interrupt and exit with a nasty message:

$SIG{INT} = sub { die "\nWhy'd you interrupt what I'm doing?\n" };

You can call functions etc.

Cheers,

Keith

Thanks for the reply, Keith. The 'trap' in KSH allows a section of code to be ran everytime the script exits (the exception may be when it's killed via "kill -9"), allowing for the script to clean-up (i.e., remove temp. files, etc...) after itself. It's pretty broad where $SIG in PERL is more specific but, I could achieve the same thing if I trapped all of the appropriate signals and sent them all to the same block of code.

Thanks again Keith,
Greg

FYI, if you need to trap normal exit (including die()s, not triggered by OS signals), you may be able to use an END {} block, which is best for normal cleanups.

for some reason i'm not getting notified of updates to this thread so I just found this ...

cbkihong - looks to be EXACTLY what I'm looking for! I've done some simple testing and it's doing what I was hoping. Thanks for the reply.

Greg