我怎样才能把所有的“die”替换成“confess”?在 Perl 应用程序中?
我正在一个大型 Perl 应用程序中工作,并且希望在每次调用“die”时获取堆栈跟踪。我知道 Carp 模块,但我不想搜索/替换每个实例“死”与“承认”。此外,我想要 Perl 模块或 Perl 解释器本身的错误的完整堆栈跟踪,显然我无法更改它们以使用 Carp。
那么,有没有办法让我在运行时修改“die”函数,使其表现得像“confess”?或者,是否有一个 Perl 解释器设置可以从“die”中抛出完整的堆栈跟踪?
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.
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'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Devel::SimpleTrace 或 Carp::Always 他们会做你所要求的事情,而无需你付出任何努力。它们具有全局作用,这意味着可以使用例如
-MDevel::SimpleTrace
在命令行上运行一次来轻松添加它们。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
.设置 __DIE__ 信号处理程序怎么样? 东西
像你脚本顶部的 ?有关详细信息,请参阅 perlvar %SIG。
What about setting a
__DIE__
signal handler? Something likeat the top of your script? See perlvar %SIG for more information.
我通常只想替换一些代码中的die,因此我本地化了__DIE__处理程序:
作为开发工具,这可以工作,但有些模块会玩弄花招这是为了让他们的功能发挥作用。当您覆盖它们期望的处理程序时,这些功能可能会以奇怪的方式中断。这不是一个好的做法,但有时会发生。
I usually only want to replace the
die
s in a bit of code, so I localize the__DIE__
handler: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.
Error 模块会将所有
die
转换为Error::Simple
对象,其中包含完整的堆栈跟踪(构造函数解析“at file...line..”文本并创建堆栈跟踪)。您可以使用任意对象(通常是Error::Simple
的子类)通过$Error::ObjectifyCallback
首选项来处理错误。如果您经常抛出其他类型的异常来指示其他事件,那么这尤其方便,因为您只需为
Error::Simple
添加一个处理程序(或您用于处理错误的任何其他类)并拥有它转储其堆栈跟踪或根据错误类型执行专门的日志记录。The Error module will convert all
die
s toError::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 fromError::Simple
) to handle errors with the$Error::ObjectifyCallback
preference.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.