Subject: | Returning undef when none is passed an empty array is counterintuitive |
This snippet of code should logically output "foo NOT found in bar":
##############################
my @bar = ();
if (none {/foo/} @bar) {
print "foo NOT found in bar\n";
}
else {
print "foo found in bar\n"
}
##############################
It doesn't as none returns undef if the given array is empty. The
attached patch fixes this problem.
-Clif
Subject: | none_empty_array.patch |
diff --git a/List-MoreUtils-0.22/MoreUtils.xs b/List-MoreUtils-0.22/MoreUtils.xs
index 9011867..595f343 100644
--- a/List-MoreUtils-0.22/MoreUtils.xs
+++ b/List-MoreUtils-0.22/MoreUtils.xs
@@ -295,7 +295,7 @@ CODE:
CV *cv;
if (items <= 1)
- XSRETURN_UNDEF;
+ XSRETURN_YES;
cv = sv_2cv(code, &stash, &gv, 0);
PUSH_MULTICALL(cv);
diff --git a/List-MoreUtils-0.22/lib/List/MoreUtils.pm b/List-MoreUtils-0.22/lib/List/MoreUtils.pm
index 01a2510..4923fbb 100644
--- a/List-MoreUtils-0.22/lib/List/MoreUtils.pm
+++ b/List-MoreUtils-0.22/lib/List/MoreUtils.pm
@@ -47,7 +47,7 @@ sub all (&@) {
sub none (&@) {
my $f = shift;
- return if ! @_;
+ return 1 if ! @_;
for (@_) {
return 0 if $f->();
}
@@ -386,7 +386,8 @@ criterion given through BLOCK. Sets C<$_> for each item in LIST in turn:
print "No value defined"
if none { defined($_) } @list;
-Returns false otherwise, or C<undef> if LIST is empty.
+As an empty list will never match the criterion, C<none> returns C<1> if LIST is empty,
+returns false otherwise.
=item notall BLOCK LIST
diff --git a/List-MoreUtils-0.22/t/List-MoreUtils-pp.t b/List-MoreUtils-0.22/t/List-MoreUtils-pp.t
index 6382877..234b2aa 100644
--- a/List-MoreUtils-0.22/t/List-MoreUtils-pp.t
+++ b/List-MoreUtils-0.22/t/List-MoreUtils-pp.t
@@ -54,7 +54,7 @@ BEGIN { $TESTS += 4 }
ok(none { !defined } @list);
ok(none { $_ > 10000 } @list);
ok(!none { defined } @list);
- ok(!defined none { });
+ ok(none { });
}
# notall (16...)
diff --git a/List-MoreUtils-0.22/t/List-MoreUtils.t b/List-MoreUtils-0.22/t/List-MoreUtils.t
index 299b866..631eccc 100644
--- a/List-MoreUtils-0.22/t/List-MoreUtils.t
+++ b/List-MoreUtils-0.22/t/List-MoreUtils.t
@@ -63,7 +63,7 @@ BEGIN { $TESTS += 4 }
ok(none { !defined } @list);
ok(none { $_ > 10000 } @list);
ok(!none { defined } @list);
- ok(!defined none { });
+ ok(none { });
}
# notall (15...)