http://www.perlmonks.org?node_id=316353
Description:

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