[英]How do I turn off “when is experimental …” in perl 5.24?
The code below was copied directly from the current perlsyn page on perldoc.perl.org. 下面的代码直接从perldoc.perl.org上的当前perlsyn页面复制而来。 [I've added an initialization and declarations as needed to make it run (and it works as expected), but the point is the 'when' keyword]
[我根据需要添加了初始化和声明以使其运行(并且可以按预期运行),但关键是'when'关键字]
Perl 5.24 complains about 'when' being experimental. Perl 5.24抱怨“何时”处于实验状态。 Fair enough, but I don't want to see this every time I use the keyword.
足够公平,但是我不想每次使用关键字都看到它。
no warnings qw(experimental::when) does not work - "Unknown warning category ..." 没有警告qw(experimental :: when)不起作用-“未知警告类别...”
I also tried "switch" as the category 我也尝试将“ switch”作为类别
Is there another way to suppress this warning (other than disabling warnings in general)? 是否有其他方法可以抑制此警告(除了通常禁用警告以外)?
use v5.14;
for ($var) {
$abc = 1 when /^abc/;
$def = 1 when /^def/;
$xyz = 1 when /^xyz/;
default { $nothing = 1 }
}
You can enable this construct without warnings via 您可以通过以下方式启用此结构而不会发出警告
use experimental 'switch';
or merely disable the "experimental" warning category via 或仅通过以下方式禁用“实验性”警告类别
no warnings 'experimental::smartmatch';
(see the full list of available warning categories in perldoc warnings
). (请参阅
perldoc warnings
中可用警告类别的完整列表 )。
But please note that smartmatch, given
, and when
constructs are considered to be fundamentally broken, and are being removed/redesigned. 但请注意,smartmatch,
given
,并且when
被认为是结构从根本上打破了,要被删除/重新设计。 Just silencing the warning will break your code when you run that code on other perl versions. 在其他perl版本上运行该代码时,仅使警告沉默将破坏您的代码。
Instead, please prefer out to spell this code out explicitly: 相反,请更喜欢明确地将此代码拼出:
for ($var) {
if (/^abc/) { $abc = 1 }
elsif (/^def/) { $def = 1 }
elsif (/^xyz/) { $xyz = 1 }
else { $nothing = 1 }
}
Yes, it's more ugly, but it will also work for all values of $var
and for all Perl5 versions. 是的,它比较丑陋,但它也适用于
$var
所有值和所有Perl5版本。
given
和when
是smartmatch
实验的一部分,因此可以使用以下命令禁用这些警告:
no warnings qw( experimental::smartmatch );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.