sdbbs sdbbs - 1 month ago 11
Perl Question

Perl debugger - break on error (exception)?

Consider this small perl program,

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";
}


If I run it, it fails with an error. If I run it in the Perl debugger, using
perl -d
, it also fails with an error:

$ 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>


... but when it fails, it doesn't "stop" at the failing line, as say
gdb
with a C program might do - the program again terminates, and thus I have no context variables to inspect anymore.

Of course, a loop like this might run for thousands of times, which is why setting a breakpoint at the problematic line and doing a
c
ontinue manually would not help much here...

Is there a way to have Perl debugger break a program upon error/exception, such that the local variable context is preserved, so as to inspect the variables there?

Answer

Wrap the offending line in an eval and set $DB::single when $@ is set:

#!/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";
}

Then,

% 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> 

What the Heck??

Behind the magic are two principles:

  1. Preventing exceptions from being fatal (e.g. catching exceptions)
  2. Stopping the debugger at a certain point in the code without repeated steps

To catch an exception, use the eval BLOCK construct. This will store the exception in the $@ variable. If $@ is not the empty string, an exception was thrown. Note that the code above, while idiomatic, is not quite correct; if the exception thrown was the string 0, it'd be ignored (because if 0 would be false). Exception handling in Perl is complex. Try::Tiny has a good discussion.

Now that the exception is no longer fatal, how to stop the debugger? There are a number of ways to do that. This example uses the $DB::single variable, which when true, signals the debugger to stop. The downside is that you must edit your code to effect this behavior. Another option is to set a breakpoint with a condition:

% 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

See perl debugger documentation for more.

Comments