Perl 如何将工作断点设置为常量表达式?

Perl 如何将工作断点设置为常量表达式?,perl,debugging,breakpoints,constant-expression,Perl,Debugging,Breakpoints,Constant Expression,我有一个Perl代码,它使用一个常量和一个初始化块,如下所示: use constant C => map { ...; } (0..255); use constant C => { map {; "C$_" => $_; } 0 .. 255 }; my $foo; BEGIN { $foo = 1; }; 当我试图在…设置断点时。。。;行,它不工作,这意味着:我可以设置断点,但调试器不会停在那里 我试过: 使用调试器

我有一个Perl代码,它使用一个常量和一个初始化块,如下所示:

use constant C => map {
    ...;
} (0..255);
use constant C => {
    map {;
        "C$_" => $_;
    } 0 .. 255
};
my $foo;

BEGIN {
    $foo = 1;
};
当我试图在…设置断点时。。。;行,它不工作,这意味着:我可以设置断点,但调试器不会停在那里

我试过:

使用调试器perl-d program.pl启动程序 在调试器b 2中设置断点 使用R重新加载,然后运行R程序 但调试器仍然没有停在这一行,就好像我没有设置断点一样


我的Perl不是最新的;它是5.18.2,以防万一…

您试图在使用块中设置断点。 use块实际上是一个包含require的BEGIN块。 默认情况下,Perl调试器不会在编译阶段停止。 但是,通过将变量$DB::single设置为1,可以在BEGIN块内强制Perl调试器进入单步模式

请参阅perldoc perldebug中的调试编译时语句

如果您将代码更改为

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

Perl调试器将在use语句中停止。

如果创建以下简单模块,可以避免更改代码:

然后,按如下方式运行代码:

perl -I./  -MStopBegin -d test.pl
中肯的答案在前面,不那么中肯的答案在下面

如果test.pl如下所示:

use constant C => map {
    ...;
} (0..255);
use constant C => {
    map {;
        "C$_" => $_;
    } 0 .. 255
};
my $foo;

BEGIN {
    $foo = 1;
};
以下是调试交互的外观:

% 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> 
% 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> 
以下是调试交互的外观:

% 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> 
% 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> 

注意使用s命令前进,否则它将跳过test.pl中的BEGIN块。

有没有办法避免像命令行开关那样修改源代码?@U.Windl我不知道,我在回答中已经说明了避免修改代码的方法。我有一个类似的想法,使用环境变量有条件地启用$DB::single=1。