http://qs321.pair.com?node_id=316353

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

Replies are listed 'Best First'.
Re: Invoking the debugger on yourself
by fizbin (Chaplain) on Jan 02, 2004 at 19:30 UTC
    I'm not sure I would have modified my script to include -d on the shebang line. That's the kind of thing I'd forget to take out. Is there some reason you didn't want to start your script with:
    PERLDB_OPTS=nonstop perl -wd myprogram myarg1 myarg2...
    ? Or even:
    PERLDB_OPTS=nonstop PERL5OPT=-wd myprogram myarg1 myarg2...