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

In reply to perl -d: faster and less scary than you think by educated_foo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.