Perl调试器 - 在错误(异常)时中断?

7

考虑这个小的Perl程序,test.pl

#!/usr/bin/env perl
use warnings;
use strict;
use Number::Format qw(:subs); # sudo perl -MCPAN -e 'install Number::Format'

my $tstr = "";
my $numFormatter = new Number::Format();

for (my $ix=0; $ix<20; $ix++) {
  $tstr = $tstr . int(rand(10));
  my $ftstr = $numFormatter->format_number($tstr, 2, 1);
  print "ix: $ix ; in: $tstr ; out: $ftstr\n";
}

如果我运行它,就会出现错误。如果我使用 perl -d 在Perl调试器中运行它,也会出现错误:

$ perl -d test.pl

Loading DB routines from perl5db.pl version 1.39_10
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

main::(test.pl:6):  my $tstr = "";
  DB<1> c
ix: 0 ; in: 6 ; out: 6.00
ix: 1 ; in: 63 ; out: 63.00
ix: 2 ; in: 637 ; out: 637.00
ix: 3 ; in: 6379 ; out: 6,379.00
ix: 4 ; in: 63790 ; out: 63,790.00
ix: 5 ; in: 637906 ; out: 637,906.00
ix: 6 ; in: 6379062 ; out: 6,379,062.00
ix: 7 ; in: 63790624 ; out: 63,790,624.00
ix: 8 ; in: 637906246 ; out: 637,906,246.00
ix: 9 ; in: 6379062467 ; out: 6,379,062,467.00
ix: 10 ; in: 63790624671 ; out: 63,790,624,671.00
ix: 11 ; in: 637906246715 ; out: 637,906,246,715.00
ix: 12 ; in: 6379062467152 ; out: 6,379,062,467,152.00
ix: 13 ; in: 63790624671522 ; out: 63,790,624,671,522.00
round() overflow. Try smaller precision or use Math::BigFloat at test.pl line 11.
 at /usr/local/share/perl/5.18.2/Number/Format.pm line 535.
    Number::Format::round('Number::Format=HASH(0x9d0b6cc)', 637906246715226, 2) called at /usr/local/share/perl/5.18.2/Number/Format.pm line 601
    Number::Format::format_number('Number::Format=HASH(0x9d0b6cc)', 637906246715226, 2, 1) called at test.pl line 11
Debugged program terminated.  Use q to quit or R to restart,
use o inhibit_exit to avoid stopping after program termination,
h q, h R or h o to get additional info.
  DB<1> p $ix         

  DB<2> 

...但当它失败时,它不会像使用C程序的gdb那样在失败的行处“停止”——程序再次终止,因此我没有上下文变量可以检查了。

当然,像这样的循环可能运行数千次,这就是为什么在有问题的行处设置断点并手动执行c命令并不会有太大帮助...

是否有一种方法可以让Perl调试器在出现错误/异常时中断程序,以便保留本地变量上下文,以便检查其中的变量?

2个回答

6

将有问题的代码行用eval包裹起来,并在$@被设置时设置$DB::single

#!/usr/bin/env perl
use warnings;
use strict;
use Number::Format qw(:subs); # sudo perl -MCPAN -e 'install Number::Format'

my $tstr = "";
my $numFormatter = new Number::Format();

for (my $ix=0; $ix<20; $ix++) {
  $tstr = $tstr . int(rand(10));
  my $ftstr = eval { $numFormatter->format_number($tstr, 2, 1); };
  $DB::single = 1 if $@;
  print "ix: $ix ; in: $tstr ; out: $ftstr\n";
}

那么,

% perl -d test.pl

Loading DB routines from perl5db.pl version 1.49
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

main::(test.pl:6):  my $tstr = "";

  DB<1> r
ix: 0 ; in: 7 ; out: 7.00
ix: 1 ; in: 71 ; out: 71.00
ix: 2 ; in: 715 ; out: 715.00
ix: 3 ; in: 7153 ; out: 7,153.00
ix: 4 ; in: 71537 ; out: 71,537.00
ix: 5 ; in: 715379 ; out: 715,379.00
ix: 6 ; in: 7153794 ; out: 7,153,794.00
ix: 7 ; in: 71537941 ; out: 71,537,941.00
ix: 8 ; in: 715379417 ; out: 715,379,417.00
ix: 9 ; in: 7153794174 ; out: 7,153,794,174.00
ix: 10 ; in: 71537941740 ; out: 71,537,941,740.00
ix: 11 ; in: 715379417408 ; out: 715,379,417,408.00
ix: 12 ; in: 7153794174086 ; out: 7,153,794,174,086.00
ix: 13 ; in: 71537941740864 ; out: 71,537,941,740,864.00
main::(test.pl:13):   print "ix: $ix ; in: $tstr ; out: $tstr\n";

  DB<1> print $tstr
715379417408646

  DB<2> 

什么鬼??

这些神奇的背后有两个原则:

  1. 防止异常成为致命错误(例如捕获异常)
  2. 在不重复步骤的情况下,在代码的某个点停止调试器

要捕获异常,请使用eval BLOCK结构。这将把异常存储在$@变量中。如果$@不是空字符串,则表示已引发异常。请注意,虽然上面的代码很惯用,但并不完全正确;如果引发的异常是字符串0,它将被忽略(因为if 0会为假)。Perl中的异常处理很复杂。 Try::Tiny有一个很好的讨论。

既然异常不再是致命的了,如何停止调试器呢?有许多方法可以实现此目的。该示例使用$DB::single变量,当为真时,表示调试器停止。缺点是您必须编辑代码才能实现此行为。另一个选项是设置带条件的断点

% perl -d test.pl

Loading DB routines from perl5db.pl version 1.49
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

main::(test.pl:6):  my $tstr = "";

  DB<1> b 11 $@ ne ''
  DB<2> r
[... output as above ...]
main::(test.pl:11):   my $ftstr = eval { $numFormatter->format_number($tstr, 2, 1); };

  DB<2> p $tstr
3247014520717436

请查看Perl调试器文档获取更多信息。


这很好,但它只适用于非常特定的调试。您可能需要扩展$DB::single - simbabque
@simbabque 我理解你的观点。但是感兴趣的人会说“这个eval和那个行噪声$@以及$DB::single是什么鬼?”,然后他们会查找并学习比我在这里能够传达的更多信息。而不感兴趣的人也会盲目模仿,这种情况无论如何都会发生。适当添加一些文档链接是合适的,我会加上它们。 - Diab Jerius

5
使用 eval{}$DB::single = 1 if $@; 来捕获错误是一个不错的技巧,但如果您想要找到库中问题的根源,您需要在引发错误的地方设置断点。
可以使用以下方式实现:
  DB<1> b Carp::croak
  DB<2> c
Carp::croak(/opt/local/lib/perl5/5.22/Carp.pm:166):
166:    sub croak   { die shortmess @_ }
  DB<3> T
@ = DB::DB called from file '/opt/local/lib/perl5/5.22/Carp.pm' line 166
. = Carp::croak('round() overflow. Try smaller precision or use Math::BigFloat') 
    called from file '/opt/local/lib/perl5/site_perl/5.22/Number/Format.pm' line 535
$ = Number::Format::round(ref(Number::Format), 637906246715227, 2)
    called from file '/opt/local/lib/perl5/site_perl/5.22/Number/Format.pm' line 601
$ = Number::Format::format_number(ref(Number::Format), 637906246715227, 2, 1)
    called from file 'mytest.pl' line 12

优雅的解决方案。 - Diab Jerius

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接