List::MoreUtils unexpected behaviour

February 16, 2010

I’ve long been a fan of the List::MoreUtils perl module so I don’t know why this “feature” has never bitten me before. The module provides a number of functions for manipulating lists, in particular I was using any() and none().

any() returns a true value if any item in LIST meets the criterion given through BLOCK, for example:

 print "At least one value undefined"
               if any { !defined($_) } @list;

none() is logically the negation of any. It returns a true value if no item in LIST meets the criterion given through BLOCK, for example:

 print "No value defined"
               if none { defined($_) } @list;

The gotcha here is that both of them will return an undef value when the list is empty. It’s not such an issue with any() but this particularly caught me out when using none() as I was expecting it to return true when the list was empty. To my mind it really should return true as an empty list definitely doesn’t contain any elements which match the condition. Surely other people have had the same experience. In future I think I will stick to using the standard grep() and just get the size of the list returned.