perl - 为什么在使用 say 函数时要指定 use 语句?

标签 perl

这个问题可能很愚蠢。但我才刚刚开始探索 Perl。我正在使用 Perl v5.16.2。我知道say语句已在 5.10 中引入。

#!/usr/bin/perl

say "Hello World!";

当我尝试运行上述程序时,出现以下错误:
$ ./helloPerl 
String found where operator expected at ./helloPerl line 3, near "say "Hello World!""
    (Do you need to predeclare say?)
syntax error at ./helloPerl line 3, near "say "Hello World!""
Execution of ./helloPerl aborted due to compilation errors.

但是当我添加声明 use 5.016; 时,它给了我正确的输出。
#!/usr/bin/perl

use 5.016;
say "Hello World!";

我的疑问是,我使用的是 5.010 以上的 perl v5.16.2。为什么我要使用 use 提及 Perl 版本这里声明?

最佳答案

默认情况下不会启用可能会破坏向后兼容性的功能。

perldoc feature :

It is usually impossible to add new syntax to Perl without breaking some existing programs. This pragma provides a way to minimize that risk. New syntactic constructs, or new semantic meanings to older constructs, can be enabled by use feature 'foo' , and will be parsed only when the appropriate feature pragma is in scope. (Nevertheless, the CORE:: prefix provides access to all Perl keywords, regardless of this pragma.)


use在版本号上,隐式启用所有功能,因为它还对 perl 版本应用了约束。所以你不会被say绊倒例如,未实现。

关于perl - 为什么在使用 say 函数时要指定 use 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37184887/

相关文章:

regex - 如何使用 Perl 正则表达式突出显示连续重复的单词?

Perl 内部变量。下一个表达式是否相同?

perl - Tree::Simple::traverse() 没有访问树的根 - 错误或功能?

perl - 是否可以在不执行其语句的情况下使用或要求 Perl 脚本?

windows - 在 Windows 操作系统上安装 ActivePerl

perl - typeglob 别名

perl - 理解代码 : Hash, grep 重复(修改为检查多个元素)

regex - perl的$-[0]对于非ASCII数据产生意外结果

perl - 访问Dancer Log对象?

regex - Perl 正则表达式匹配 perl 正则表达式文字