Perl CORE::say vs -E

后端 未结 2 1015
栀梦
栀梦 2021-01-04 13:25

In this answer is used the perl one-liner as:

perl -we \'... CORE::say \"x=$x\"\'

What is the advantage using the -e and

2条回答
  •  长发绾君心
    2021-01-04 14:24

    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+)
    

    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);'
    

    With the latest Perl in 2010 (5.12), the program outputs the following:

    120
    

    With the latest Perl in 2016 (5.24), the program outputs the following:

    5
    

    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);'
    

提交回复
热议问题