我怎样才能把所有的“die”替换成“confess”?在 Perl 应用程序中?

发布于 2024-08-14 03:28:29 字数 293 浏览 4 评论 0原文

我正在一个大型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泛泛之交 2024-08-21 03:28:29

使用 Devel::SimpleTraceCarp::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.

莫相离 2024-08-21 03:28:29

设置 __DIE__ 信号处理程序怎么样? 东西

$SIG{__DIE__} = sub { Carp::confess @_ };

像你脚本顶部的 ?有关详细信息,请参阅 perlvar %SIG

What about setting a __DIE__ signal handler? Something like

$SIG{__DIE__} = sub { Carp::confess @_ };

at the top of your script? See perlvar %SIG for more information.

哭泣的笑容 2024-08-21 03:28:29

我通常只想替换一些代码中的die,因此我本地化了__DIE__处理程序:

 {
 use Carp;
 local $SIG{__DIE__} = \&Carp::confess;

 ....
 }

作为开发工具,这可以工作,但有些模块会玩弄花招这是为了让他们的功能发挥作用。当您覆盖它们期望的处理程序时,这些功能可能会以奇怪的方式中断。这不是一个好的做法,但有时会发生。

I usually only want to replace the dies in a bit of code, so I localize the __DIE__ handler:

 {
 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.

北城挽邺 2024-08-21 03:28:29

Error 模块会将所有 die 转换为 Error::Simple 对象,其中包含完整的堆栈跟踪(构造函数解析“at file...line..”文本并创建堆栈跟踪)。您可以使用任意对象(通常是 Error::Simple 的子类)通过 $Error::ObjectifyCallback 首选项来处理错误。

如果您经常抛出其他类型的异常来指示其他事件,那么这尤其方便,因为您只需为 Error::Simple 添加一个处理程序(或您用于处理错误的任何其他类)并拥有它转储其堆栈跟踪或根据错误类型执行专门的日志记录。

The Error module will convert all dies 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文