rodee
rodee

Reputation: 3181

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

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.

Upvotes: 4

Views: 488

Answers (1)

ikegami
ikegami

Reputation: 386561

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

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

Upvotes: 8

Related Questions