I needed to run the debugger on a long-running process, and the conditional breakpoint required was too hairy for my poor brain to figure out. After a bit of spelunking in perldoc perldebug and perldoc DB I came up with the code below.

There are a few things to be aware of:

  1. Note the -d switch on the shebang line. This will run the program under the debugger.
  2. Using -d will by default stop execution at the first executable statement. To start the program immediately without have to wait for a person to hit the 'r' key and press enter, the environment variable PERLDB_OPTS should be set to nonstop. Hence, in a Bournish shell, the program should be run with PERLDB_OPTS=nonstop myprogram myarg1 myarg2...
  3. Setting $DB::single to 1 will cause the debugger to take over control at the beginning of the next statement. The documentation warns that this variable should be considered read-only, but setting it doesn't appear to have any ill effects that I could see.

In my real-life example, the setting of $DB::single involved opening sockets to remote machine and database lookups, but in essence was doing nothing more than the example below. It might make more sense to say $DB::single = maybe_breakpoint($foo, $bar, $rat,...) and encapsulate the mess in a subroutine.

Also note that XML::SAX::PurePerl is really, really slow under the debugger. Do not adjust your terminal, it will respond... eventually :)

#! /usr/bin/perl -wd use strict; for my $step( 0..9 ) { print "$step\n"; $DB::single = 1 if $step == 5; }

In reply to Invoking the debugger on yourself by grinder

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.