I'm working in a large Perl application and would like to get stack traces every time 'die' is called. I'm aware of the Carp module, but I would prefer not to search/replace every instance of 'die' with 'confess'. In addition, I would like full stack traces for errors in Perl modules or the Perl interpreter itself, and obviously I can't change those to use Carp.
我正在使用大型Perl应用程序,并且每次调用“die”时都希望获得堆栈跟踪。我知道Carp模块,但我不想用'confess'搜索/替换'die'的每个实例。另外,我想要Perl模块或Perl解释器本身的错误的完整堆栈跟踪,显然我不能改变使用Carp的那些。
So, is there a way for me to modify the 'die' function at runtime so that it behaves like 'confess'? Or, is there a Perl interpreter setting that will throw full stack traces from 'die'?
那么,有没有办法让我在运行时修改'die'函数,使其行为像'confess'?或者,是否有一个Perl解释器设置会从'die'中抛出完整的堆栈跟踪?
4 个解决方案
#1
32
Use Devel::SimpleTrace or Carp::Always and they'll do what you're asking for without any hard work on your part. They have global effect, which means they can easily be added for just one run on the commandline using e.g. -MDevel::SimpleTrace
.
使用Devel :: SimpleTrace或Carp :: Always,他们会做你想要的,而不需要你付出任何努力。它们具有全局效果,这意味着可以使用例如命令行在一个运行中轻松添加它们。 -MDevel :: SimpleTrace。
#2
12
What about setting a __DIE__
signal handler? Something like
设置__DIE__信号处理程序怎么样?就像是
$SIG{__DIE__} = sub { Carp::confess @_ };
at the top of your script? See perlvar %SIG for more information.
在脚本的顶部?有关更多信息,请参阅perlvar%SIG。
#3
2
I usually only want to replace the die
s in a bit of code, so I localize the __DIE__
handler:
我通常只想在一些代码中替换die,所以我本地化__DIE__处理程序:
{
use Carp;
local $SIG{__DIE__} = \&Carp::confess;
....
}
As a development tool this can work, but some modules play tricks with this to get their features to work. Those features may break in odd ways when you override the handler they were expecting. It's not a good practice, but it happens sometimes.
作为一种开发工具,这可以起作用,但是有些模块可以通过这种技巧来实现它们的功能。当您覆盖他们期望的处理程序时,这些功能可能会以奇怪的方式破坏。这不是一个好习惯,但有时会发生。
#4
0
The Error module will convert all die
s to Error::Simple
objects, which contain a full stacktrace (the constructor parses the "at file... line.." text and creates a stack trace). You can use an arbitrary object (generally subclassed from Error::Simple
) to handle errors with the $Error::ObjectifyCallback
preference.
Error模块将所有die转换为Error :: Simple对象,其中包含完整的堆栈跟踪(构造函数解析“at file ... line ..”文本并创建堆栈跟踪)。您可以使用任意对象(通常是从Error :: Simple继承)来处理$ Error :: ObjectifyCallback首选项的错误。
This is especially handy if you commonly throw around exceptions of other types to signal other events, as then you just add a handler for Error::Simple
(or whatever other class you are using for errors) and have it dump its stacktrace or perform specialized logging depending on the type of error.
如果您通常抛出其他类型的异常以发信号通知其他事件,这是特别方便的,因为您只需为Error :: Simple(或您正在使用的任何其他类中的错误)添加处理程序并让它转储其堆栈跟踪或执行专门的操作记录取决于错误的类型。
#1
32
Use Devel::SimpleTrace or Carp::Always and they'll do what you're asking for without any hard work on your part. They have global effect, which means they can easily be added for just one run on the commandline using e.g. -MDevel::SimpleTrace
.
使用Devel :: SimpleTrace或Carp :: Always,他们会做你想要的,而不需要你付出任何努力。它们具有全局效果,这意味着可以使用例如命令行在一个运行中轻松添加它们。 -MDevel :: SimpleTrace。
#2
12
What about setting a __DIE__
signal handler? Something like
设置__DIE__信号处理程序怎么样?就像是
$SIG{__DIE__} = sub { Carp::confess @_ };
at the top of your script? See perlvar %SIG for more information.
在脚本的顶部?有关更多信息,请参阅perlvar%SIG。
#3
2
I usually only want to replace the die
s in a bit of code, so I localize the __DIE__
handler:
我通常只想在一些代码中替换die,所以我本地化__DIE__处理程序:
{
use Carp;
local $SIG{__DIE__} = \&Carp::confess;
....
}
As a development tool this can work, but some modules play tricks with this to get their features to work. Those features may break in odd ways when you override the handler they were expecting. It's not a good practice, but it happens sometimes.
作为一种开发工具,这可以起作用,但是有些模块可以通过这种技巧来实现它们的功能。当您覆盖他们期望的处理程序时,这些功能可能会以奇怪的方式破坏。这不是一个好习惯,但有时会发生。
#4
0
The Error module will convert all die
s to Error::Simple
objects, which contain a full stacktrace (the constructor parses the "at file... line.." text and creates a stack trace). You can use an arbitrary object (generally subclassed from Error::Simple
) to handle errors with the $Error::ObjectifyCallback
preference.
Error模块将所有die转换为Error :: Simple对象,其中包含完整的堆栈跟踪(构造函数解析“at file ... line ..”文本并创建堆栈跟踪)。您可以使用任意对象(通常是从Error :: Simple继承)来处理$ Error :: ObjectifyCallback首选项的错误。
This is especially handy if you commonly throw around exceptions of other types to signal other events, as then you just add a handler for Error::Simple
(or whatever other class you are using for errors) and have it dump its stacktrace or perform specialized logging depending on the type of error.
如果您通常抛出其他类型的异常以发信号通知其他事件,这是特别方便的,因为您只需为Error :: Simple(或您正在使用的任何其他类中的错误)添加处理程序并让它转储其堆栈跟踪或执行专门的操作记录取决于错误的类型。