I'm wondering: When you detect you need to clean up at the end of the program (excluding "end of block"), you want to "queue an exit handler" that does the cleanup at some later time. In most cases you want the cleanup to happen even if the user presses ^C (SIGINT), so I wonder:
When installing possibly multiple exit handlers, should it be done with END
blocks, or should it be done using $SIG{__DIE__}
handlers (if so, how to "queue" them?)?
The manual says processes terminated by signals would skip the END
blocks, to is that a no-go for them?
Likewise, when a process is terminated by a signal, is the $SIG{__DIE__}
handler called at all ("man perlvar" is silent on that), or do I have to install a handler for each possible signal, too?
This is because unless the application itself installs a signal handler, the default action for most signals is to forcibly kill the process. This means there is no way for the application to run their own cleanups in this case. The process cleanup of the OS (closing files, sockets, ...) is still done though.
If you want to have your own cleanup you need to catch the signal using
%SIG
. If you then callexit
from inside the signal handler it will also runEND
functions in your program. Note that not all signals can be catched.$SIG{__DIE__}
is not relevant here. It will not be called on signals, but if the program throws a fatal exception (i.e.die
).