I had always assumed that perl's debugging hooks would only work if perl was run as
perl -d, and would make your program horribly slow. It turns out that neither assumption is true. First, any code compiled while suitable
$^P flags are set (see
perldebguts), even when Perl is started without "-d", is debuggable. Second, when
$DB::trace is 0 and minimal debugging flags are set, there seems to be no appreciable overhead. Specifically, with
$^P = 0x303, which allows single-line stepping and gives nice
caller information for evals and anonymous subs, debugging is essentially free.
To me, at least, this is seriously cool. I'm surprised this isn't more widely known or used.
Update: If you're wondering what prompted this, check out Sepia version 0.90 shortly. It only takes a couple hundred lines of ELisp + Perl to get a pretty decent GUD debugger in Emacs, and a minimal one would probably be less than 100. Once again, thank you Free Software!
Here are the benchmarks, with typical runtimes on my system:
sub fib
{
my $n = shift;
if ($n < 2) {
return $n;
} else {
return fib($n-1) + fib($n-2);
}
}
__END__
$ time perl -le 'BEGIN { sub DB::DB {}; $^P=0x303 }; do "/tmp/fib.pl";
+ print fib(28)'
317811
real 0m0.949s
user 0m0.941s
sys 0m0.006s
$ time perl -le 'do "/tmp/fib.pl"; print fib(28)'
317811
real 0m0.924s
user 0m0.918s
sys 0m0.005s
$ time perl -le 'BEGIN { sub DB::DB {}; $^P=0xfff }; do "/tmp/fib.pl";
+ print fib(28)'
317811
real 0m1.719s
user 0m1.712s
sys 0m0.006s
$ time perl -le 'BEGIN { sub DB::DB {}; $^P=0x303; $DB::trace=1 }; do
+"/tmp/fib.pl"; print fib(28)'
317811
real 0m1.557s
user 0m1.548s
sys 0m0.006s