Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute some code always before the Perl script ends

Tags:

perl

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.

like image 821
Jānis Elmeris Avatar asked Jul 11 '12 12:07

Jānis Elmeris


People also ask

What is $? In Perl script?

$? 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.

Which of the following blocks will get executed when we compile with the Perl code?

Every BEGIN block is executed after the perl script is loaded and compiled but before any other statement is executed.


1 Answers

Trap the termination signals and re-route them so something, simplest would be:

$SIG{TERM} = $SIG{INT} = $SIG{QUIT} = $SIG{HUP} = sub { die; };
like image 52
beresfordt Avatar answered Sep 28 '22 03:09

beresfordt