当“无法通过包y定位对象方法x”时,打印堆栈跟踪。 (Perl) [英] Print stack trace when "Can't locate object method x via package y" (Perl)

查看:94
本文介绍了当“无法通过包y定位对象方法x”时,打印堆栈跟踪。 (Perl)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是我在错误类型的对象上调用方法x,但是它在我的调用堆栈中下降了,因此并不明显。

Chances are I invoke method x on an object of the wrong type, but it's way down in my call stack, so it's not obvious.

所以:有一种在发生此错误时始终打印完整堆栈跟踪的方法?

So: is there a way of always printing a full stack trace when this error occurs?

推荐答案

要始终打印完整堆栈跟踪,请添加使用Carp :: Always; 或使用

To always print a full stack trace add use Carp::Always; or run the program with

perl -MCarp::Always script

编写脚本,或者使用bash

or, with bash

PERL5OPT=-MCarp::Always script

PERL5OPT 环境变量并运行(可执行)脚本。例如,这允许脚本中的shebang(#!)行决定使用哪个解释器。如果您导出(例如在shell配置文件中), export PERL5OPT = -MCarp :: Always ,则它该Shell中的所有Perl脚本都将使用它。感谢 ikegami 的评论。

what sets up the PERL5OPT environment variable and runs the (executable) script. For one, this allows the shebang (#!) line in the script to decide which interpreter is used. If you export it (say in the shell configuration file), export PERL5OPT=-MCarp::Always, then it will be used by all Perl scripts in that shell. Thanks to ikegami for comments.

要微调哪个特殊异常会引起更多关注,请添加 $ SIG {__ DIE __} 钩子

To fine-tune which particular exceptions get more attention add the $SIG{__DIE__} hook

use Carp;

BEGIN { 
    $SIG{__DIE__} = sub { 
        confess @_ if $_[0] =~ /Can't locate object method/;  #'
    };  
};

并且在钩子返回 ...之后,异常处理将继续进行如果没有钩子,除非... ,请参见perlvar中的%SIG die 上面的代码通常会出现其他错误。

and after the hook returns "... the exception processing continues as it would have in the absence of the hook, unless ...", see %SIG in perlvar. The code above dies normally on other errors.

因此,在这里您可以更改<$ c抛出$ c> die ,并在运行之前运行代码。例如,有关如何获取调用堆栈中每一帧的所有词汇的信息,请参见这篇文章

Thus here you can change what happens as the die is thrown, with code run right before it goes. For example, see this post for how to get all lexicals for each frame in the call stack.

使用此工具可能会比较棘手,因此请仔细阅读。例如,请参见这篇文章中的链接。

Messing with this can get tricky so please read up on it. See links in this post, for instance.

这篇关于当“无法通过包y定位对象方法x”时,打印堆栈跟踪。 (Perl)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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