--- name: any signature: 'any BLOCK LIST' since: 5.42 status: documented categories: ["Lists"] --- ```{index} single: any; Perl built-in ``` *[Lists](../perlfunc-by-category)* # any Return true if `BLOCK` yields true for at least one element of `LIST`. `any` runs `BLOCK` once per element of `LIST`, aliasing [`$_`](../perlvar) 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: ```perl use feature 'keyword_any'; ``` The long-standing [`List::Util::any`](../../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`](../../List/Util/any) when you also need other `List::Util` exports or have to support Perls older than 5.42. ## Synopsis ```perl 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`](../../List/Util/any). ## Global state it touches - [`$_`](../perlvar) is aliased to each element of `LIST` in turn while `BLOCK` runs. The aliasing is the same kind [`map`](map) and [`grep`](grep) use: modifying [`$_`](../perlvar) 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: ```perl 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: ```perl 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: ```perl use feature 'keyword_any'; if (any { $_ eq '--verbose' } @ARGV) { $verbose = 1; } ``` `any` paired with [`all`](all) as a sanity check — every record has an `id`, and at least one is flagged active: ```perl 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: ```perl 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`](map) for that. - **Empty `LIST` returns false.** `any { anything } ()` is false without ever running the block. - **[`$_`](../perlvar) aliasing is identical to [`map`](map) and [`grep`](grep).** Writing to [`$_`](../perlvar) mutates the source list element. The idiom `any { my $x = $_; ... }` makes the copy explicit. - **[`return`](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`](../../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`](all) — the universal-quantifier counterpart; true iff `BLOCK` yields true for **every** element - [`grep`](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`](../../List/Util/any) — pre-5.42 subroutine form with identical semantics - [`List::Util::first`](../../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`](map) — same [`$_`](../perlvar)-aliasing contract, but always visits every element and collects results