Perl:强制堆栈跟踪“不能调用未定义的方法” [英] Perl: Force stacktrace for "can't call method on undefined"
问题描述
我使用Perl和Catalyst作为Web框架。
I'm using Perl and Catalyst as Web-Framework.
我如何全局强制堆栈跟踪,如果一个例外,如code>无法调用方法XXX在未定义的值... 被抛出?
How can I globally force a stacktrace if an exception like Can't call method "XXX" on an undefined value...
is thrown?
假设您的 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
抛出当然的例外:
在MyApp :: Controller :: Root-> test中捕获异常'
'subtract_datetime'在
/usr/local/lib/perl/5.10.1/DateTime.pm第1619行1003上的未定义值。
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."
堆栈跟踪丢失!
如何在这种情况下强制进行堆栈跟踪?
我想获得(不是真正的输出,只由手工制作):
I.e. I would like to get (not a real output, only composed by hand):
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行引起的。
With this I know exactly that the exception was caused by the line 114 in Root.pm
推荐答案
我不知道它是否在Catalyst内工作,但是通常您可以通过包括CPAN模块来强制堆栈跟踪。 Carp :: Always
,例如通过在程序中的某个地方写入使用Carp :: Always;
或者使用启动perl脚本-MCarp :: Always
开关。
I am not sure if it works within Catalyst, but usually you can force a stacktrace by including the CPAN module Carp::Always
, e.g. by writing somewhere in your program use Carp::Always;
or starting the perl script with the -MCarp::Always
switch.
这篇关于Perl:强制堆栈跟踪“不能调用未定义的方法”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!