perldebuggingconditional-breakpoint

Can I enable a `$DB::single =1` breakpoint conditionally from a program?


I have a case where a local variable in a sub has the wrong un-blessed value after several invocations, and I'd like to break to the Perl debugger (when the program runs under debugger control, of course) if a specific condition is met.

I could try a conditional breakpoint interactively, but that would be a lot of typing work when repeating the "modify, debug" cycle many times.

Anyway I'd like to know whether its possible to use a construct like $DB::single=1 if (...);. When I tried it, it did not work, and the breakpoint was active all the time.

So is it possible?

What I had tried

Basically my problem is that the line @l_socks = @{$lsa->sockets()}; triggered a "Can't call "sockets" on unblessed reference at ...". As the code is in a subroutine that is called many times before the problem happens, I added $DB::single = 1 unless (ref $lsa); before the problematic line, but that did not work.

So maybe my problem is that $lsa is not a scalar as expected, but a reference; unblesswd however.


Solution

  • For completeness, here is the "answer" to my question:

    $DB::single = 1 correctly enables a debugger breakpoint after the instruction following the command if the condition to trigger it is correct.

    In my case I used an incorrect condition that would never trigger, so it seemed the $DB::single = 1 was ignored.

    Fixing the condition fixed the problem. For the code example shown (you are not expected to understand the details; SocketArray is a package written for this program), using

    unless (UNIVERSAL::isa($lsa, 'SocketArray') {
        $DB::single = 1;
        1;
    }
    

    fixed it, and the debugger stopped at 1; when it triggered.