问题描述
在 Perl 中,有没有办法强制所有致命错误显示堆栈回溯,如 Carp::confess
产生的?
In Perl, is there a way to force all fatal errors to display a stack backtrace like Carp::confess
produces?
我知道你可以使用警告FATAL =>'all';
使警告在当前词法范围内是致命的.
I know you can do use warnings FATAL => 'all';
to make warnings fatal over the current lexical scope.
此外,可以使用 $SIG{__WARN__} = sub { CORE::die(@_) };
使所有警告在任何地方都是致命的(尚未本地化 SIGWARN 处理程序).
Further it is possible to use $SIG{__WARN__} = sub { CORE::die(@_) };
to make all warnings fatal everywhere (that hasn't localized the SIGWARN handler).
有没有一种干净的方法可以做到这一点,或者我需要调整 SIGDIE 吗?如果我确实编写了 SIGDIE 处理程序,那么获取跟踪的最佳方法是什么?
Is there a clean way to do this, or do I need to tweak SIGDIE? And if I do write a SIGDIE handler, what is the best way to get the trace?
理想的解决方案是与标准测试库、Test::More
和朋友一起使用.
An ideal solution would work with the standard testing libraries, Test::More
and friends.
更新: Mark Johnson 建议使用 SIGDIE 处理程序来调用 Carp::confess
.它工作得很好.代码如下:
Update: Mark Johnson suggests using a SIGDIE handler to call Carp::confess
. It works nicely. Here's the code:
use Carp;
$SIG{ __DIE__ } = \&Carp::confess;
推荐答案
安装一个调用 Carp::confess 的 SIGDIE 处理程序?或者只是将 Carp::confess 设置为 DIE 的处理程序?
Install a SIGDIE handler that calls Carp::confess? Or just set up Carp::confess as the handler for DIE?
注意与 eval 相关的标准问题.关于 BEGIN 块,还有一个更奇怪的问题.另请注意 perlvar 中的不祥警告.
Beware of the standard gotchas related to eval. There is an even weirder gotcha with regard to BEGIN blocks. Also note the ominous warning in perlvar.
查看这个问题了解更多有关生成堆栈跟踪的信息.
See this question for more information on generating stack traces.
这篇关于如何强制对 Perl 中的所有致命错误进行堆栈回溯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!