代码之家  ›  专栏  ›  技术社区  ›  U. Windl

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

  •  0
  • U. Windl  · 技术社区  · 6 年前

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

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

    当我尝试 设置断点 ...; 行,它不起作用,意思是:我 可以设置 断点,但调试器 不停 那里。

    我尝试过:

    1. 用调试器启动程序( perl -d program.pl )
    2. 在调试器中设置断点( b 2 )
    3. 使用重新加载 R 然后运行( r )程序

    但调试程序仍然没有在该行停止,就好像我没有设置断点一样。

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

    2 回复  |  直到 6 年前
        1
  •  4
  •   U. Windl    6 年前

    你试图在一个 use 块。 使用块实际上是 BEGIN 用A块 require 在里面。 默认情况下,Perl调试器不会在编译阶段停止。 但是,可以在 开始 通过设置变量阻止 $DB::single 1

    Debugging Compile-Time Statements 在里面 perldoc perldebug

    如果您将代码更改为

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

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

        2
  •  3
  •   Diab Jerius    6 年前

    如果创建这样一个简单的模块,就可以避免修改代码。( concept originated here ):

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

    然后,将代码运行为

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

    相关答案(上一个,不太相关的答案在这个答案下面)

    如果test.pl如下所示:

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

    调试交互如下所示:

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

    注意使用断点在 map .

    以前的,不太相关的答案

    如果 test.pl 如下所示:

    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(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 命令前进,否则将跳过 BEGIN 方块内 测试程序

    推荐文章