在 Perl 脚本结束之前执行一些代码 [英] Execute some code always before the Perl script ends

查看:61
本文介绍了在 Perl 脚本结束之前执行一些代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置必须在 Perl 脚本停止之前执行的代码?

How can I set a code that must execute before a Perl script stops?

在这里如何在 perl 脚本退出之前运行一段代码 我阅读了 END 子例程,但它仅在脚本正常结束时才执行.但是,例如,如果用户通过 ^C 中止程序,我也希望代码也能执行.

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.

推荐答案

捕获终止信号并重新路由它们,最简单的方法是:

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

$SIG{TERM} = $SIG{INT} = $SIG{QUIT} = $SIG{HUP} = sub { die; };

这篇关于在 Perl 脚本结束之前执行一些代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆