这个问题可能很愚蠢。但我才刚刚开始探索 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/