How can I set a code that must execute before a Perl script stops?
In here how to run piece of code just before the exit of perl script I read about the END subroutine, but it only executes if the script ends normally. However, I want the code to be executed also if, for example, user aborts the program by ^C.
$? The status returned by the last pipe close, backtick (\`\`) command or system operator. Note that this is the status word returned by the wait() system call, so the exit value of the subprocess is actually ($? >> 8). $? & 255 gives which signal, if any, the process died from, and whether there was a core dump.
Every BEGIN block is executed after the perl script is loaded and compiled but before any other statement is executed.
Trap the termination signals and re-route them so something, simplest would be:
$SIG{TERM} = $SIG{INT} = $SIG{QUIT} = $SIG{HUP} = sub { die; };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With