Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override die in Perl and still return correct exit code on Windows

Tags:

perl

On Windows, I want to execute something only when the script dies. Below block didn't help; I think it's because Windows don't support signals.

$SIG{__DIE__} = sub {
    qx(taskkill /F /IM telnet.exe);
    CORE::die @_;
}

Then I tried this:

END {
    qx(taskkill /F /IM telnet.exe);
    exit $exit_code;
}

It performed taskkill, but exited with exit code 0. I need to propagate exit_code as we do further processing based on it.

like image 507
rodee Avatar asked Feb 09 '23 18:02

rodee


1 Answers

END blocks can set $? to control the exit value.

END {
      qx(taskkill /F /IM telnet.exe);
      $? = $exit_code;
}
like image 51
ikegami Avatar answered May 24 '23 11:05

ikegami