any#
Return true if BLOCK yields true for at least one element of LIST.
any runs BLOCK once per element of LIST, aliasing $_ to the
current element, and returns a true value the moment the block yields
true. If no element makes the block true — including the case where
LIST is empty — any returns false. It never runs the block more
times than it has to: the first true result decides the answer and
the remaining elements are not examined.
any is not unconditionally available. It is a feature-gated core
keyword introduced in Perl 5.42; enable it with:
use feature 'keyword_any';
The long-standing List::Util::any, available since at least Perl
5.20, implements the same semantics as a subroutine. The two are
interchangeable for everyday code — pick the core keyword when you
want the slightly lower call overhead and an extra stack frame fewer
in profiles, or List::Util::any when you also need other
List::Util exports or have to support Perls older than 5.42.
Synopsis#
use feature 'keyword_any';
any BLOCK LIST
What you get back#
A boolean: a true value if any iteration of BLOCK yielded true, a
false value otherwise. The exact shape of the true/false value is
unspecified — treat the return as boolean, do not rely on getting
1 and "" specifically.
An empty LIST returns false. This is the mathematically correct
answer (the existential quantifier over an empty domain is false)
and matches List::Util::any.
Global state it touches#
Examples#
Test whether any line in a list matches a pattern:
use feature 'keyword_any';
my @lines = ("foo\n", "bar ERROR baz\n", "quux\n");
if (any { /ERROR/ } @lines) {
warn "log contains an error line\n";
}
Numeric threshold check:
use feature 'keyword_any';
my @nums = (1, 4, 7, 12, 3);
print "over limit\n" if any { $_ > 10 } @nums; # over limit
Command-line flag detection, without pulling in a module:
use feature 'keyword_any';
if (any { $_ eq '--verbose' } @ARGV) {
$verbose = 1;
}
any paired with all as a sanity check — every record has an id,
and at least one is flagged active:
use feature qw(keyword_any keyword_all);
my @records = (
{ id => 1, active => 0 },
{ id => 2, active => 1 },
);
die "malformed" unless all { defined $_->{id} } @records;
die "none active" unless any { $_->{active} } @records;
Using the List::Util version when you cannot depend on Perl 5.42:
use List::Util qw(any);
print "found\n" if any { $_ eq 'needle' } @haystack;
Edge cases#
Short-circuits on the first true result. Side effects in
BLOCKrun only for elements examined before (and including) the first true element. Do not useanyto drive a loop that must touch every element — useforormapfor that.Empty
LISTreturns false.any { anything } ()is false without ever running the block.$_aliasing is identical tomapandgrep. Writing to$_mutates the source list element. The idiomany { my $x = $_; ... }makes the copy explicit.returninsideBLOCKreturns from the enclosing sub, not fromany. To terminate the scan early with a specific truth value, let the block evaluate to that value —anyalready short-circuits.Core keyword vs
List::Util::any. Both implement the same semantics. The core keyword avoids one Perl-level call frame per invocation ofanyitself, which shows up in tight profiling; the block itself still runs per-element either way. Mixing both in the same file is legal but confusing; pick one per file.experimental::keyword_anywarning. Because the keyword is still flagged experimental, enabling it emits a warning in theexperimental::keyword_anycategory on first use. Silence it withno warnings 'experimental::keyword_any';once you have decided to depend on the keyword.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
all— the universal-quantifier counterpart; true iffBLOCKyields true for every elementgrep— collects every matching element;scalar grep { ... } @listhas the same truth value asany { ... } @listbut runs the block for every elementList::Util::any— pre-5.42 subroutine form with identical semanticsList::Util::first— returns the first element for which the block is true, rather than a boolean; use this when you need the element itselfmap— same$_-aliasing contract, but always visits every element and collects results