Perl: fixed crash when a script exits abnormally