Perl CORE::say vs -E [英] Perl CORE::say vs -E
问题描述
在这个答案中使用perl one-liner as:
In this answer is used the perl one-liner as:
perl -we '... CORE::say "x=$x"'
使用 -e
和 CORE::say
而不是较短的:-E
和普通的 say 有什么好处
,例如:
What is the advantage using the -e
and CORE::say
instead of the shorter: -E
and plain say
, e.g.:
perl -wE '... say "x=$x"'
推荐答案
feature.pm was引入以允许向 Perl 添加向后不兼容的功能.-E
启用所有向后不兼容的功能,这意味着如果升级 perl
,使用 -E
的程序可能会崩溃.
feature.pm was introduced to allow backwards-incompatible features to be added to Perl. -E
enables all backward-incompatible features, which means a program that uses -E
may break if you upgrade perl
.
perl -E'... say "foo"; ...' # Forward-incompatible (5.10+)
perl -Mfeature=say -e'... say "foo"; ...' # ok (5.10+)
perl -Mv5.10 -e'... say "foo"; ...' # ok (5.10+)
perl -M5.010 -e'... say "foo"; ...' # ok (5.10+)
perl -e'... CORE::say "foo"; ...' # ok (5.16+)
<小时>
例如,假设您在 2010 年编写了以下程序:
For example, say you wrote the following program in 2010:
perl -E'sub fc { my $acc=1; $acc*=$_ for 2..$_[0]; $acc } say fc(5);'
使用 2010 年最新的 Perl (5.12),程序输出如下:
With the latest Perl in 2010 (5.12), the program outputs the following:
120
使用 2016 年最新的 Perl (5.24),程序输出如下:
With the latest Perl in 2016 (5.24), the program outputs the following:
5
不同之处在于 5.16 添加了一项功能,该功能在启用时会更改该程序的含义.如果避免使用-E
,程序的行为就不会改变.具体来说,5.24中的输出120
:
The difference is due to the addition of a feature to 5.16 that changes the meaning of that program when enabled. If one had avoided the use of -E
, the program's behaviour would not have changed. Specifically, the following outputs 120
in 5.24:
perl -e'sub fc { my $acc=1; $acc*=$_ for 2..$_[0]; $acc } CORE::say fc(5);'
这篇关于Perl CORE::say vs -E的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!