如何在Perl调试器中监视表达式的更改? [英] How can I watch for changes to an expression in the Perl debugger?

查看:101
本文介绍了如何在Perl调试器中监视表达式的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Perl调试器,我知道可以使用 b 命令在某些代码行设置断点。变量的内容更改后,是否可以立即停止调试器?

With the Perl debugger, I know I can set breakpoints at certain lines of code with the b command. Can I get the debugger to stop as soon as the contents of a variable has changed?

推荐答案

您可以使用Perl调试器中的 w 命令。

You can create watch points using the w command in the Perl debugger.

通过键入 w 然后创建将监视其内容的表达式来创建监视表达式更改:

Create a watch-expression by typing w and then an expression that will monitored for changes:

DB<1> w $variablename

输入 c 继续直到观看的表情发生变化。完成后,您将获得类似于以下的输出:

Enter c to continue until the watched expression changes. 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.

还要注意,表达式是 stringized 。因此,例如,将变量更改为 undef 将为您提供以下输出:

Also note that the expression is stringified. So for example, changing a variable to undef will give you this output:

  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.

如果watch表达式是一个列表,调试器将比较该列表的字符串化元素:

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.

要删除监视表达式,请使用 W 命令,并查看活动监视列表。 -表达式,请使用 L 命令。

To delete a watch-expression, use the W command, and to view a list of active watch-expressions, use the L command.

由于每个表达式都会重新评估watch-expression,因此不能期望使用词法变量的watch-expression超出范围。快速提示是创建对词法的全局引用,并对其进行跟踪:

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. 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 观看内容非标量:

DB<1> w Data::Dumper->Dump([$hashref])

w $ hashref ,因为它会在哈希值更改时停止,而不是简单地指向引用指向的地址(因为hashref字符串化为<$ c $之类的东西) c> HASH(0x2a07a90))。

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

这篇关于如何在Perl调试器中监视表达式的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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