Lists

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#

  • $_ is aliased to each element of LIST in turn while BLOCK runs. The aliasing is the same kind map and grep use: modifying $_ inside the block modifies the list element itself. Use a named lexical via my $x = $_ in the first line of the block if you need a safe copy.

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 BLOCK run only for elements examined before (and including) the first true element. Do not use any to drive a loop that must touch every element — use for or map for that.

  • Empty LIST returns false. any { anything } () is false without ever running the block.

  • $_ aliasing is identical to map and grep. Writing to $_ mutates the source list element. The idiom any { my $x = $_; ... } makes the copy explicit.

  • return inside BLOCK returns from the enclosing sub, not from any. To terminate the scan early with a specific truth value, let the block evaluate to that value — any already short-circuits.

  • Core keyword vs List::Util::any. Both implement the same semantics. The core keyword avoids one Perl-level call frame per invocation of any itself, 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_any warning. Because the keyword is still flagged experimental, enabling it emits a warning in the experimental::keyword_any category on first use. Silence it with no 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 iff BLOCK yields true for every element

  • grep — collects every matching element; scalar grep { ... } @list has the same truth value as any { ... } @list but runs the block for every element

  • List::Util::any — pre-5.42 subroutine form with identical semantics

  • List::Util::first — returns the first element for which the block is true, rather than a boolean; use this when you need the element itself

  • map — same $_-aliasing contract, but always visits every element and collects results