2011-08-24 55 views
7

我認爲noneList::MoreUtils子程序不起作用的描述。根據文件,Is List :: MoreUtils :: none buggy?

none BLOCK LIST邏輯上否定任何。如果 返回真實值LIST中的項目不符合通過BLOCK,或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"; 
} 

工程確定,但有一個空(my @arr =();或者乾脆my @arr;)取代@arr,你會得到一個錯誤的答案。

發生了什麼事?

更新:我有List :: MoreUtils ver 0.22。更新到最新,似乎還可以。雖然很奇怪!

+2

0.22版本之間實現改變和最新的 - 看到我的回答 – Zaid

回答

9

該文檔符合v 0.33純Perl implementation。它失敗的原因是因爲實施版本在版本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; 
           | } 

MetaCPAN還提供了一個comprehensive diff between versions 0.22 and 0.33

+3

酷使用SO語法突出顯示=>並排代碼比較:) – Zaid