从 Perl 的符号表中删除名称后访问包变量? [英] Access package variable after its name is removed from symbol table in Perl?
问题描述
我尝试从 main::
符号表中删除包变量的名称,如下所示,但后来的代码似乎仍然可以引用该变量,为什么删除后可以引用?从符号表中删除名称有什么影响?
I tried delete the name of a package variable from the main::
symbol table as below, but seems later code could still reference the variable, why it can be referenced after deletion? What's the effect of removing a name from symbol table?
$n = 10;
delete $main::{'n'};
print $n;
推荐答案
在编译 perl 代码时,会查找(并根据需要创建)包变量/符号的 glob,并直接从编译的代码中引用.
When perl code is compiled, globs for package variables/symbols are looked up (and created as necessary) and referenced directly from the compiled code.
所以如果你删除一个符号表条目$pkg::{n}
,所有使用了$pkg::n
、@的已编译代码pkg::n
等,还是用原来的glob.但是 do
/require
'd code 或 eval STRING
或符号引用不会.
So if you delete a symbol table entry $pkg::{n}
, all the already compiled code that used $pkg::n
, @pkg::n
, etc., still use the original glob. But do
/require
'd code or eval STRING
or symbolic references won't.
这段代码以两个 *n glob 结尾;在删除执行之前编译的所有代码都将引用原始代码;之后编译的所有代码都将引用新的代码(符号引用将在运行时获得符号表中的任何一个).
This code ends up with two *n globs; all the code compiled before the delete executes will reference the original one; all the code compiled after will reference the new one (and symbolic references will get whichever is in the symbol table at that point in runtime).
$n = 123;
delete $::{n};
eval '$n=456';
print $n;
eval 'print $n';
另一个例子:
$n = 123;
sub get_n { $n }
BEGIN { delete $::{n} }
$n = 456;
print get_n();
print $n;
这篇关于从 Perl 的符号表中删除名称后访问包变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!