gpt4 book ai didi

Perl:对未定义的 "can' t 调用方法强制堆栈跟踪"

转载 作者:行者123 更新时间:2023-12-01 08:32:11 27 4
gpt4 key购买 nike

我使用 Perl 和 Catalyst 作为 Web 框架。

如果抛出类似 Can't call method "XXX"on an undefined value... 之类的异常,我如何全局强制执行堆栈跟踪?

假设您的 Controller/Root.pm

中有以下代码
use DateTime;

sub test :Local :Args(0) {
my ( $self, $c ) = @_;
my $now = DateTime->now(time_zone=>'local');
my $tmp = undef;

my $throwing = $tmp - $now; #this will throw an exception!
$c->res->body("OK");
}

打开 http://localhost:3000/test 当然会抛出异常:

Caught exception in MyApp::Controller::Root->test "Can't call method "subtract_datetime" on an undefined value at /usr/local/lib/perl/5.10.1/DateTime.pm line 1619, line 1003."

堆栈跟踪丢失!

在这种情况下如何强制执行堆栈跟踪?

即我想得到(不是真正的输出,只能手工组成):

MyApp::Controller::Root::__ANON__('Can\'t call method "subtract_datetime" on an undefined value at /usr/local/lib/perl/5.10.1/DateTime.pm line 1619, <DATA> line 1003.^J') called at /home/user/MyApp/script/../lib/MyApp/Controller/Root.pm line 114
MyApp::Controller::Root::test('MyApp::Controller::Root=HASH(0x20583b60)', 'MyApp=HASH(0x206e9140)') called at /usr/local/share/perl/5.10.1/Catalyst/Action.pm line 65
Catalyst::Action::execute('Catalyst::Action=HASH(0x20668e88)', 'MyApp::Controller::Root=HASH(0x20583b60)', 'MyApp=HASH(0x206e9140)') called at /usr/local/share/perl/5.10.1/Catalyst.pm line 1687
eval {...} at /usr/local/share/perl/5.10.1/Catalyst.pm line 1687
Catalyst::execute('MyApp=HASH(0x206e9140)', 'MyApp::Controller::Root', 'Catalyst::Action=HASH(0x20668e88)') called at /usr/local/share/perl/5.10.1/Catalyst/Action.pm line 60
[ and the rest of the exception ...]

这样我就知道异常是由 Root.pm 中的第 114 行引起的

最佳答案

我不确定它是否在 Catalyst 中工作,但通常您可以通过包含 CPAN 模块 Carp::Always 来强制堆栈跟踪,例如通过在程序中的某处编写 use Carp::Always; 或使用 -MCarp::Always 开关启动 perl 脚本。

关于Perl:对未定义的 "can' t 调用方法强制堆栈跟踪",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17659663/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com