perl开启新版本的新特性
2014-08-17 18:17:03 阿炯

本站赞助商链接,请多关照。 比如在5.10上的脚本中,使用'use feature qw()'或'use v5.10'之类的模块即可引入该版本的新特性。在命令行模式下,如果用'perl -Mmodule'之类的话有些复杂,如果想启用新特性,可以使用新的-E开关来打开所有的新特性。官方的《Perl pragma to enable new features》页面中有对历代新特性的位置索引。

% perl5.10.1 -E say.pl #开启5.10.1 版本的所有新特性

$ perl -E 'say "hello freeoa.net use $^O,at $^T"'
hello freeoa.net use linux,at 1408267915

在程序脚本中使用 use 指令之后指定perl版本号就可以了。
use 5.010; # 打开5.10版以来的新特性
从5.12 之后直接指定版本号,自动打开约束指令。如果不希望打开所有的新特性,可以使用feature编译指令,仅打开需要的特性。
use feature qw(switch say)

在cpan上有自动引入Perl最新特性模块之Modern::Perl

自动使用严格模式(warnings、strict)
If a script specifies a minimum version of 5.11.0 or higher (e.g. use 5.012), then strict is turned on automatically. This doesn't enable warnings, but perhaps that will be added in a future version. Also, if you do OO programming in Perl, you should know that using Moose automatically turns on both strict and warnings in that class.

If you are on a modern Perl, say so, you just have to enable it. 5.12 applies strict except for one-liners. It can't be default because of backward compatibility.

Since Perl 5.12.0 if you require a version of Perl >= 5.12.0, then your script will have all the backwards incompatible features turned on, including strict by default.

use 5.12.0;
use warnings;

Is the same as:
use strict;
use warnings;
use feature ':5.12';
It hasn't been turned on more broadly because doing so would break a lot scripts that people depend on to "just work".

Moose also automatically turns on strict and warnings when you use it. So if you do any Moose based Perl OOP, then you get a free pass here, too.(Moose当你使用它时会自动打开严格和警告,所以如果你使用任何基于Moose Perl OOP,都会自动继承这种特性。)


参考来源:
理解perl feature指令