为什么我能';t覆盖Perl的内置打印?

为什么我能';t覆盖Perl的内置打印?,perl,Perl,随后, 我试图用我自己的文件覆盖打印: BEGIN {*CORE::GLOBAL::print = sub {print 1};} print 2; 但事实证明它不起作用,2仍然被打印,而不是1 为什么?因为它有非常特殊的解析规则,普通函数无法复制,所以不能重写print操作符 print "foo\n"; print { *STDOUT } "foo\n"; 您可以找到可以使用 PS-如果您的代码实际上覆盖了从到另一个问题的print,那么您将有一个无限循环: 无法覆盖以下关键字: cho

随后,

我试图用我自己的文件覆盖
打印

BEGIN {*CORE::GLOBAL::print = sub {print 1};}
print 2;
但事实证明它不起作用,
2
仍然被打印,而不是
1


为什么?

因为它有非常特殊的解析规则,普通函数无法复制,所以不能重写
print
操作符

print "foo\n";
print { *STDOUT } "foo\n";
您可以找到可以使用

PS-如果您的代码实际上覆盖了从到另一个问题的
print

,那么您将有一个无限循环:

无法覆盖以下关键字:

chop、defined、delete、do、dump、each、elsif、eval、exists、for、foreach、format、glob、goto、grep、if、keys、last、local、m、map、my、next、no、package、pop、pos、print、printf、prototype、push、q、qq、qw、qx、redo、return、s、scalar、shift、sort、splite、splite、stud、sub、tie、tie、tie、tie、tr、unde、unef、unef、unef、unef、unsfit,而y


@new_perl,因为它没有普通sub无法复制的特殊解析规则。(它有prototype
;$
)为什么要选中
CORE::exit
,而
exit
实际上指的是
CORE::GLOBAL::exit
?@new_perl,1),因为文档是这么说的。你没有关注我提供的链接吗?2) 因为
CORE::GLOBAL::exit
exit
不一样<代码>核心::退出和退出是等效的
CORE::GLOBAL::exit()
甚至不起作用。如果是这样的话,根据post@new\u perl的说法,为什么
exit
可以被override
CORE::GLOBAL::exit
覆盖,因为这就是
CORE::GLOBAL::exit
所做的。顺便说一下,它们可以被另一种方式覆盖:
PL\u关键字插件
。例如,覆盖
qw
>perl -E"say qq{$_: }, defined(prototype(qq{CORE::$_})) ? 'yes' : 'no' for @ARGV" print map time chr
print: no
map: no
time: yes
chr: yes