gpt4 book ai didi

perl - 为什么这种魔术 Perl 内部变量的解决方法有效?

转载 作者:行者123 更新时间:2023-12-05 00:18:36 25 4
gpt4 key购买 nike

t2.pl

1;
2;
3;

t.pl
package DB;

sub DB {
print "HERE";

our(undef, $f, undef) = caller;

# This does not work
# my $ref = \%{ "main::_<$f" };
# $ref->{ 3 } = {};

# This does not work
# *x = $main::{ "_<$f" };
# $x{ 3 } = {};

*dbline = $main::{ "_<$f" };
$dbline{ 3 } = {};

$DB::single = 0;
}

1;
PERL5DB="BEGIN{ require 't.pl' }" perl -d t2.pl

当您运行此代码时,您会得到 HEREHERE ,但是当您不使用 *dbline 为其别名时,神奇的 Perl 变量不起作用。 .

因此,当您将前两个示例更改为如下所示时:
*dbline =  $main::{ "_<$f" }; # <<<<<<<<< With this it works!!!!
*x = $main::{ "_<$f" };
$x{ 3 } = { };

断点开始工作。 (这适用于 Perl 5.14.4)

为什么它以这种方式工作?

DOC

最佳答案

这是reported前:

perldebguts says:

  • Each hash %{"_<$filename"} contains breakpoints and actions keyed by line number. Individual entries (as opposed to the whole hash) are settable.

This implies that breakpoints set on %{"_<..."} apply to the named file. That is not actually true, as every %{"_<..."} hash sets breakpoints on lines in @DB::dbline, regardless of which file it refers to. The assumption is that debuggers will alias *DB::dbline to *{"_<..."} before setting any breakpoints.

Hence, all %{"_<..."} hashes are the same.

Is this a case where the documentation should be expanded to match the implementation? Or should we change the implementation to make each %{"_<..."} hash work on its corresponding @{"_<..."} array? The latter seems more useful to me.


在 5.20.0 中,行为是 changed这样你就不必别名 @DB::dbline :
$ PERL5DB='
sub DB::DB {
($p,$f,$l) = caller;
print "$f:$l\n";
${"::_<$f"}{3} = 1; # no need for alias
$DB::single = 0
}
' perl -d foo
foo:1
foo:3

关于perl - 为什么这种魔术 Perl 内部变量的解决方法有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37665719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com