[英]How can I watch for changes to an expression in the Perl debugger?
With the Perl debugger, I know I can set breakpoints at certain lines of code with the b
command. 使用Perl调试器,我知道我可以使用
b
命令在某些代码行设置断点。 Can I get the debugger to stop as soon as the contents of a variable has changed? 一旦变量的内容发生变化,我可以让调试器停止吗?
You can create watch points using the w
command in the Perl debugger. 您可以使用Perl调试器中的
w
命令创建监视点。
w
debugger command: w
debugger命令的崩溃过程: Create a watch-expression by typing w
and then an expression that will monitored for changes: 通过键入
w
然后创建一个将监视更改的表达式来创建一个watch-expression:
DB<1> w $variablename
Enter c
to continue until the watched expression changes. 输入
c
继续,直到观察到的表达式发生变化。 Once you do, you will get output similar to this: 完成后,您将获得类似于此的输出:
DB<2> c
Watchpoint 0: $variablename changed:
old value: ''
new value: 'hi'
main::(ex.pl:6): $variablename = "";
Note that the debugger stops at the statement after the changed has happened , so the line displayed might not be relevant at all. 请注意, 调试器在更改发生后停止在语句处 ,因此显示的行可能根本不相关。
Also note that the expression is stringified . 另请注意表达式是字符串化的 。 So for example, changing a variable to
undef
will give you this output: 因此,例如,将变量更改为
undef
将为您提供此输出:
DB<2> c
Watchpoint 0: $variablename changed:
old value: 'hi'
new value: ''
main::(ex.pl:7): $variablename = undef;
If the variable is subsequently changed to an empty string, the debugger will not stop, as a stringified empty string and a stringified undef is considered equal . 如果随后将变量更改为空字符串,则调试器将不会停止,因为字符串化的空字符串和字符串化的undef被视为相等 。
If the watch expression is a list, the debugger will compare the stringified elements of the list: 如果监视表达式是列表,则调试器将比较列表的字符串化元素:
DB<1> w $variablename, "second"
DB<2> c
Watchpoint 0: $variablename, "second" changed:
old value: 'one', 'second'
new value: 'two', 'second'
main::(hi.pl:6): $variablename = "three";
You can use array variables or hash variables as watch-expressions, and they will be treated as any other list. 您可以将数组变量或散列变量用作监视表达式,它们将被视为任何其他列表。
To delete a watch-expression, use the W
command, and to view a list of active watch-expressions, use the L
command. 要删除watch-expression,请使用
W
命令,要查看活动监视表达式列表,请使用L
命令。
Since the watch-expression is re-evaluated with every statement, you can't expect a watch-expression that uses a lexical variable to work out of scope. 由于watch-expression是使用每个语句重新计算的,因此您不能指望使用词法变量的watch-expression超出范围。 A quick tip is to create a global reference to the lexical, and track that instead:
一个快速提示是创建一个词法的全局引用,并跟踪它:
DB<1> $main::my_debug_variable = $hashref_lexical_variable
DB<2> w $main::my_debug_variable->{key_im_watching}
Data::Dumper
Data::Dumper
Use Data::Dumper
to watch the contents of a non-scalar: 使用
Data::Dumper
观察非标量的内容:
DB<1> w Data::Dumper->Dump([$hashref])
This is preferable to a simple w $hashref
, because it will stop when the values of the hash change, rather than simply the address the reference is pointing (since a hashref stringifies to something like HASH(0x2a07a90)
). 这比简单的
w $hashref
,因为它会在散列值改变时停止,而不是简单地引用指向的地址(因为hashref字符串化为类似HASH(0x2a07a90)
)。
此外,您可以在Linux中使用"ddd your_script.pl&"
,并在GUI中观察C / C ++调试器等变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.