0

I have Perl code that uses a constant with an initializing block like this:

use constant C => map {
    ...;
} (0..255);

When I try to set a breakpoint at the ...; line, it does not work, meaning: I can set the breakpoint, but the debugger does not stop there.

I tried:

  1. Start the program with the debugger (perl -d program.pl)
  2. Set the breakpoint in the debugger (b 2)
  3. Reload using R, then run (r) the program

But still the debugger did not stop at the line, just as if I had no breakpoint set.

My Perl is not the latest; it's 5.18.2, just in case it matters...

U. Windl
  • 3,480
  • 26
  • 54

2 Answers2

5

You are trying to put a break point in a use block. A use block is in effect a BEGIN block with a require in it. The Perl debugger by default does not stop in compile phase. However you can force the Perl debugger into single step mode inside a BEGIN block by setting the variable $DB::single to 1

See Debugging Compile-Time Statements in perldoc perldebug

If you change your code to

use constant C => map {
    $DB::single = 1;
    ...;
} (0..255);

The Perl debugger will stop in the use statement.

U. Windl
  • 3,480
  • 26
  • 54
JGNI
  • 3,933
  • 11
  • 21
3

You can avoid altering your code if you create a simple module like this (concept originated here):

package StopBegin;

BEGIN {
    $DB::single=1;
}
1;

Then, run your code as

perl -I./  -MStopBegin -d test.pl

Pertinent Answer (previous, not-so-pertinent answer is below this one)

If test.pl looks like this:

use constant C => {
    map {;
        "C$_" => $_;
    } 0 .. 255
};

here's what the debug interaction looks like:

% perl -I./  -MStopBegin -d test.pl

Loading DB routines from perl5db.pl version 1.53
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

StopBegin::CODE(0x55db6287dac0)(StopBegin.pm:8):
8:  1;
  DB<1> s
main::CODE(0x55db6287db38)(test.pl:5):
5:  };
  DB<1> -
1   use constant C => {
2:      map {;
3:          "C$_" => $_;
4       } 0 .. 255
5==>    };
  DB<2> b 3
  DB<3> c
main::CODE(0x55db6287db38)(test.pl:3):
3:          "C$_" => $_;
  DB<3> 

Note the use of the breakpoint to stop inside the map.

Previous, Not-So-Pertinent Answer

If test.pl looks like this:

my $foo;

BEGIN {
    $foo = 1;
};

here's what the debug interaction looks like:

% perl -I./  -MStopBegin -d test.pl

Loading DB routines from perl5db.pl version 1.53
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

StopBegin::CODE(0x5567e3d79a80)(StopBegin.pm:8):
8:  1;
  DB<1> s
main::CODE(0x5567e40f0db0)(test.pl:4):
4:      $foo = 1;
  DB<1> s
main::(test.pl:1):  my $foo;
  DB<1> s
Debugged program terminated.  Use q to quit or R to restart,
use o inhibit_exit to avoid stopping after program termination,
h q, h R or h o to get additional info.
  DB<1> 

Note the use of the s command to advance, otherwise it'll skip over the BEGIN block in test.pl

Diab Jerius
  • 2,310
  • 13
  • 18