Perl新版本中对Experimental特性的警告
2015-10-15 16:27:10 阿炯

Perl 5.18 provides a new way to introduce experimental features in a program, augmenting the feature pragma that v5.10 added. This change marks certain broken v5.10 features as experimental with an eye toward possible removal from the language.

Perl 5.18提供了一种引入实验特性到脚本程序中新方法,增加的功能编译指示对v5.10是一种补充。这种变化会将从v5.10引入的新特性发出警告,因为这些新特性可能从后继的新的版本中移除。

Smart matching in v5.10 led to several broken and conflated features. The given used a lexical version of $_, which broke many other common uses of that variable inside the given, which I explain in Use for() instead of given() and you can see in given/when and lexical $_ ….

从v5.10引入的智能匹配将变得麻烦,给定的使用默认值$_也会如此,它破坏了许多其他常见的给定变量,下面将解释在for()而不是given()这样便可以看到given/when和$_这新版本中的表现。

Under v5.18, when you use given, when, or ~~, you get a warning, even if there is no smart match involved:

在v5.18之中,当使用given, when, or ~~,将会得到一个警告,即使没有涉及智能匹配:

# given_warning.pl
use v5.10; # earliest occurance of feature
for( 'Buster' ) {
 when( 1 == 1 ) { say "Hello" }
}

These warnings might cause test suites to fail when people try to install modules on the new perl, like it does for Unicode::Tussle.
这些警告可能导致测试套件失败当人们试图安装新perl模块时,就像在安装Unicode::Tussle时。

% perl5.10.1 given_warning.pl
Hello
% perl5.18.0 given_warning.pl
when is experimental at given_warning.pl line 4.
Hello

Using the diagnostics shows the sort of warning it is:
使用diagnostics调试时会有如下简单的警告:

% perl5.18.0 -Mdiagnostics given_warning.pl
when is experimental at -e line 1 (#1)
(S experimental::smartmatch) when depends on smartmatch, which is
experimental.  Additionally, it has several special cases that may
not be immediately obvious, and their behavior may change or
even be removed in any future release of perl.
See the explanation under "Experimental Details on given and when"
in perlsyn.

Hello

To get rid of this warning, you do the same thing you do with other warnings. Take the category of the warning and turn it off with no (Item 100: Use lexical warnings to selectively turn on or off complaints):
相摆脱这个警告,可以采取关闭对应类别的警告(可选择打开或关闭警告):

# given_warning.pl
use v5.10; # earliest occurance of feature
no warnings 'experimental::smartmatch';
for( 'Buster' ){
 when( 1 == 1 ) { say "Hello" }
}

The lexical $_ is another broken fature that’s now marked as experimental.
词法$_是另一个现在标记为实验的用法。

# lexical_.pl
use v5.10;
sub cat {my $_}

Any use in v5.18 gives a warning:
% perl5.18.0 lexical_.pl
Use of my $_ is experimental at lexcial_.pl line 3.

The category is different:

% perl5.18.0 -Mdiagnostics lexical_.pl
Use of my $_ is experimental at lexcial_.pl line 4 (#1)
(S experimental::lexical_topic) Lexical $_ is an experimental
feature and its behavior may change or even be removed in any
future release of perl. See the explanation under "$_" in perlvar.

That takes care of the two retro features. Perl v5.18 introduces two new experimental features, set logic in character classes (for complete Unicode Level 1 regular expression compliance), and lexical subroutines, which I’ll cover in other items.
请注意有两个'复古'的特性。Perl v5.18引入了两个新的实验特性,设置逻辑字符类(完整的Unicode一级正则表达式规范),和词汇子例程,还有其他特性介绍。

# regex.pl
use v5.18;

print "Match" if 'foo' =~ /(?[ \p{Thai} & \p{Digit} ])/;

Without turning off the warning, perl knows about the feature and points it out:
不必关闭的警告,perl知道此特性并将提示出来:

% perl5.18.0 regex.pl
The regex_sets feature is experimental in regex; marked by <-- HERE in m/(?[ <-- HERE  \p{Thai} & \p{Digit} ])/ at regex.pl line 4.

In this case, diagnostics is not any help:

% perl5.18.0 -Mdiagnostics regex.pl
The regex_sets feature is experimental in regex; marked by <-- HERE in m/(?[
<-- HERE  \p{Thai} & \p{Digit} ])/ at regex.pl line 3 (#1)
The regex_sets feature is experimental in regex; marked by <-- HERE in m/(?[ <-- HERE  \p{Thai} & \p{Digit} ])/ at regex.pl line 3.

For lexical named subroutines, you have explicitly enable the feature but you then have to explicitly turn off its warnings.
对于词汇命名的子程序,可以显式地启用这个功能,但需要显式地关闭与之相关的警告。

# lexical_sub.pl
use v5.18;
no warnings 'experimental::lexical_subs';
use feature "lexical_subs";

my sub foo { say "Hello" }

Handling older perls
处理老版本的perl脚本

In v5.18, that's all fine and good, but older versions don't understand those warnings categories and will stop your program.

% perl5.10.1 -e 'no warnings qw(smartmatch)'
Unknown warnings category 'smartmatch' at -e line 1
BEGIN failed--compilation aborted at -e line 1.

Instead of using warnings, you can use the non-core (until v5.20) experimental module that handles that for you:
代替使用警告,可像用下面的方式使用非核心(直到v5.20)实验模块,像这样:

use experimental qw(smartmatch);

For versions without that warning category, nothing happens. For versions with that feature, it turns off the warning.
当该版本没有警告类别时什么也不会发生,如该版本有此功能的话可以将警告关闭。

However, this is an additional dependency prior to v5.20. You should limit the features you use to the minimum version of Perl you allow.
在一个额外的依赖v5.20之前,你应该限制使用这些功能在你的Perl允许的最低版本。

Summary
总结


This table summarizes the new experimental warnings categories and the features they affect.
这个表总结了新的实验性质警告类别及它们的影响。

Category     Features
experimental::smartmatch     given, when, ~~
experimental::lexical_topic     my $_
experimental::regex_sets     (?[ ])
experimental::lexical_subs     my sub NAME {}, our sub NAME {}

注意事项

Some v5.10 features now warn under v5.18
在5.10版本中的新特性在5.18会被警告
Some new experimental features must be explicitly enabled
必须显式地启用一些新的实验特性
Even explicitly enabled features still warn
甚至显式启用特性仍然会被警告
The experimental module is version safe
实验模块依然是安全版本


参考来源:

Experimental features now warn (reaching back to v5.10)

experimental - Experimental features made easy