我认为none子程序List::MoreUtils不起作用.根据文件,
无块列表逻辑上否定任何.如果LIST中的项目不符合通过BLOCK给出的条件,或者LIST为空,则返回true值.依次为LIST中的每个项目设置$ _
现在,尝试:
use strict;
use warnings;
use 5.012;
use List::MoreUtils qw(none);
my @arr = ( 1, 2, 3 );
if ( none { $_ == 5 } @arr ) {
say "none of the elements in arr equals 5";
}
else {
say "some element in arr equals 5";
}
Run Code Online (Sandbox Code Playgroud)
工作正常,但替换@arr为空(my @arr = ();或简单my @arr;),你得到一个错误的答案.
这是怎么回事?
更新:我有List :: MoreUtils ver 0.22.更新到最新版本似乎没问题.虽然奇怪!
该文档符合v 0.33纯Perl 实现.失败的原因是因为版本0.22和0.33之间的实现发生了变化.
在v 0.33中,如果@array为空,则for循环不会执行,因此YES将返回.
以下是两个版本并排:
# v 0.33 | # v 0.22
------------------------------+----------------------------------------
sub none (&@) { | sub none (&@) {
my $f = shift; | my $f = shift;
foreach ( @_ ) { | return if ! @_; # root cause
return NO if $f->(); | for (@_) {
} | return 0 if $f->();
return YES; | }
} | return 1;
| }
Run Code Online (Sandbox Code Playgroud)
MetaCPAN还提供了0.22和0.33版本之间的全面差异
| 归档时间: |
|
| 查看次数: |
232 次 |
| 最近记录: |