微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

这个问题可能是愚蠢的.但是我刚开始探索Perl.我使用Perl v5.16.2.我知道在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.

但是当我添加语句使用5.016,它给我正确的输出.

#!/usr/bin/perl

use 5.016;
say "Hello World!";

我的怀疑是,我使用的是perl v5.16.2,它高于5.010.为什么要在这里使用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.)

使用版本号,隐式启用所有功能,因为它也对perl版本应用约束.所以你不会因为说没有被执行而被绊倒了.

原文地址:https://www.jb51.cc/Perl/172535.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐