考虑这个小的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调试器在出现错误/异常时中断程序,以便保留本地变量上下文,以便检查其中的变量?
$DB::single
。 - simbabqueeval
和那个行噪声$@
以及$DB::single
是什么鬼?”,然后他们会查找并学习比我在这里能够传达的更多信息。而不感兴趣的人也会盲目模仿,这种情况无论如何都会发生。适当添加一些文档链接是合适的,我会加上它们。 - Diab Jerius