给定以下 Perl 程序:
package Foo;
use strict;
use warnings;
sub new {
my ($class) = @_;
return bless {}, $class;
}
sub c {
print "IN ORIG C\n";
}
sub DESTROY {
print "IN DESTROY\n";
c();
}
1;
package main;
use strict;
use warnings;
no warnings qw/redefine once/;
local *Foo::c = sub { print "IN MY C\n" };
my $f = Foo->new();
undef $f;
我希望输出为:
IN DESTROY
IN MY C
但我实际上得到的输出为:
IN DESTROY
IN ORIG C
问:为什么我对
Foo::c
的本地化重新定义不生效?
最佳答案
编译 perl 代码时,会查找(并根据需要创建)包变量/符号的 glob,并直接从编译代码中引用。
因此,当您(临时)替换 *Foo::c
的符号表条目时在运行时,所有使用 *Foo::c
的已编译代码仍然使用原始的 glob。但是做/需要代码或 eval STRING 或符号引用不会。
(与 Access package variable after its name is removed from symbol table in Perl? 非常相似,请参阅那里的示例。)
关于perl - 为什么我对包子的本地化重新定义没有生效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29157449/