在 perl 中,$DB::single = 1 和 2 有什么区别? [英] In perl, what is the difference between $DB::single = 1 and 2?

查看:51
本文介绍了在 perl 中,$DB::single = 1 和 2 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在您的代码中加入 $DB::single=1$DB::single=2 有什么区别?当我在 perl 调试器命令行上执行c"时,两者似乎都具有在赋值之后的语句处停止执行的相同效果.

What is the difference between putting $DB::single=1 and $DB::single=2 in your code? 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.

perldebug 表示值为 1 相当于有只是按 's' 进入下一个语句,而 2 与 'n' 相同,但它与您如何到达该语句有什么区别?

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:

如果您将 $DB::single 设置为 2,则相当于输入了 n 命令(该命令在子例程调用上执行)),而 1 的值表示 s 命令(进入子程序调用).

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.pm 源代码.

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.

让我们顺理成章地遵循这一点.您可能需要参考源代码.我已经简化了一些代码以删除不必要的细节,但您应该能够从我的描述中得到想法.

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.

当您在调试器中执行代码时,(至少)有两个重要的变量,runningsingle.这些的组合决定了代码是否运行:

When you are executing code in the debugger, there are (at least) two important variables, running and single. 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

DB() 函数对每一行都执行,它包含以下代码段,如果 single 已设置,它将停止运行(它总是执行不考虑当前行):

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):

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

这就是为什么,如果您在 Perl 代码中设置变量,它会在下一行中断(中断,我的意思是停止运行代码",而不是以某种方式损坏")调试器.

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.

running0时,DB()函数进入这个小循环:

When running is 0, the DB() function enters this little loop:

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

换句话说,它等待用户命令将 running 设置回 1.这可以通过以下三个函数之一来完成:

In other words, it waits on a user command which sets running back to 1. 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;
}

您可以看到这三个命令设置了 singlerunning 的不同组合,它们将在执行下一个 Perl 行时使用(请参阅前面的表格以了解这些组合意味着什么).

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).

能够在 Perl 代码中使用 12 的直接结果是您使用了狡猾但聪明的技巧 通过设置通常由调试器命令设置的变量来中断 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.

这就是为什么重要的不是值,而是强制调试器进入特定状态的事实.

That's why it's not the value that matters so much as the fact you're forcing the debugger into a particular state.

这篇关于在 perl 中,$DB::single = 1 和 2 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆