Override die in Perl and return the correct exit code in Windows

On Windows, I want to execute something only when the script dies. The block below did not help; I think this is because Windows does not 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;
}

He completed taskkill, but exited with a completion code of 0. I need to deploy exit_code, as we do further processing based on it.

+4
source share
1 answer

ENDblocks can be set $?to control the output value.

END {
      qx(taskkill /F /IM telnet.exe);
      $? = $exit_code;
}
+8
source