简体   繁体   English

在perl中,$ DB :: single = 1和2之间有什么区别?

[英]In perl, what is the difference between $DB::single = 1 and 2?

What is the difference between putting $DB::single=1 and $DB::single=2 in your code? 在代码中放入$DB::single=1$DB::single=2什么区别? Both seem to have the identical effect of halting execution at the statement following the assignment when I do a 'c' on the perl debugger command line. 当我在perl调试器命令行上执行“c”时,两者似乎都具有在赋值后停止执行的相同效果。

perldebug says that a value of 1 is equivalent to having just pressed 's' to get to the next statement, and 2 is the same as 'n', but what difference does it make how you got to the statement? perldebug说,值为1相当于只按下's'进入下一个语句,2与'n'相同,但它与你如何达到声明有什么区别?

From perldebug : 来自perldebug

If you set $DB::single to 2 , it's equivalent to having just typed the n command (which executes over subroutine calls), whereas a value of 1 means the s command (which enters subroutine calls). 如果将$DB::single设置$DB::single 2 ,则相当于只键入n命令(在子例程调用上执行),而值1表示s命令(进入子例程调用)。

That much you know already. 你已经知道了很多。


From a user point of view, I'm pretty sure there is no difference. 从用户的角度来看,我很确定没有区别。 I base this on an examination of the actual DB.pm source code . 我的基础是对实际DB.pm 源代码的检查。

Let's follow this logically. 让我们逻辑地遵循这一点。 You may want to refer to the source code. 您可能想参考源代码。 I've simplified some of the code to remove unnecessary detail but you should be able to get the idea from my descriptions. 我已经简化了一些代码来删除不必要的细节,但你应该能够从我的描述中得到这个想法。

When you are executing code in the debugger, there are (at least) two important variables, running and single . 当您在调试器中执行代码时,(至少)有两个重要的变量,即runningsingle The combination of these is what decides whether code is run: 这些的组合决定了代码是否运行:

running  single  description
-------  ------  -----------
   0       ?     not running
   1       0     running flat-out
   1       1     single stepping, execute into function
   1       2     single stepping, execute over function

The DB() function is executed for every single line, and it contains the following snippet which will stop the running if single has been set (it always executes the current line regardless): DB()函数是针对每一行执行的,它包含以下片段,如果设置了single ,它将停止运行(无论如何总是执行当前行):

if ($DB::single) {
    $DB::single = 0;
    $running = 0;
}

That's why, if you set the variable in your Perl code, it will break (by break, I mean "stop running the code", not "damage somehow") the debugger at the next line. 这就是为什么,如果你在你的Perl代码中设置变量,它将在下一行中断(通过中断,我的意思是“停止运行代码”,而不是“以某种方式损坏”)调试器。

When running is 0 , the DB() function enters this little loop: running0DB()函数进入这个小循环:

# Now sit in an event loop until something sets $running
do {
    $c->idle;          # call client event loop; must not block
} until $running;

In other words, it waits on a user command which sets running back to 1 . 换句话说,它等待用户命令设置running1 This can be done by one of the following three functions: 这可以通过以下三个功能之一来完成:

sub next {
    $DB::single = 2;
    $running = 1;
}

sub step {
    $DB::single = 1;
    $running = 1;
}

sub cont {
    $DB::single = 0;
    $running = 1;
}

You can see that these three commands set up a different combination of single and running which will be used while executing the next Perl line (see the earlier table to see what these combinations mean). 您可以看到这三个命令设置了singlerunning的不同组合,这些组合将在执行下一个Perl行时使用(请参阅前面的表以了解这些组合的含义)。

The ability to use either 1 or 2 in your Perl code is a direct result of the fact that you're using a sneaky but clever trick to break execution from your Perl code itself, by setting a variable that would normally be set by a debugger command. 在Perl代码中使用12的能力直接导致你通过设置一个通常由调试器设置的变量,使用一个偷偷摸摸但聪明的技巧来打破Perl代码本身的执行。命令。

That's why it's not the value that matters so much as the fact you're forcing the debugger into a particular state. 这就是为什么重要的不是因为你迫使调试器进入特定状态的事实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM