all#
Test whether BLOCK returns true for every element of LIST.
all walks LIST left to right, aliasing $_ to each element in
turn and evaluating BLOCK with that element. It returns a true
value if every evaluation yields truth, and a false value as soon as
any evaluation yields falsity. As soon as one element makes the
block false, all stops and returns — it does not evaluate the
block for the remaining elements.
This is the language-level all keyword, enabled by the
keyword_all feature. It is distinct from
List::Util::all — see Edge cases below.
Synopsis#
use feature 'keyword_all';
no warnings 'experimental::keyword_all';
all { /\w+/ } @words;
all { defined $_ } @list; # canonical "all defined" check
all { $_ > 0 } @numbers;
What you get back#
A boolean. True if every element passes, false otherwise. The exact true/false values are unspecified — treat the result as a condition, not a datum to propagate.
Empty list returns true. This is vacuous truth: the statement
“every element satisfies BLOCK” is trivially true when there are no
elements to check. It is the single most common source of surprise
with all:
all { $_ > 0 } (); # TRUE — no elements, nothing to fail
Guard with an explicit size check when an empty list should be treated as failure:
if (@numbers && all { $_ > 0 } @numbers) { ... }
Global state it touches#
Examples#
Check that every value in a list is defined — the canonical use:
my @row = get_record();
die "missing field" unless all { defined $_ } @row;
Check that every number is positive:
my @prices = (1.99, 4.50, 0.99);
say "ok" if all { $_ > 0 } @prices; # ok
Validate a record where every field must match its own predicate.
Combine multiple tests inside the block rather than chaining all
calls:
my @fields = ($name, $age, $email);
my $ok = all {
defined $_ && length $_ > 0
} @fields;
Combine all with any for an exclusive check — at least one
match, but not every element the same:
if (any { $_ eq 'admin' } @roles
&& !all { $_ eq 'admin' } @roles) {
say "mixed roles, including admin";
}
Short-circuit behaviour: the block runs only until the first failure. Useful for predicates with side effects or cost:
all { expensive_check($_) } @candidates; # stops at first failure
Edge cases#
Empty list returns true. Vacuous truth. Guard with
@list && all { ... } @listwhen emptiness should fail.Short-circuits on first false. The block is not called for any element after the first failure. Do not rely on side effects in the block running for every element — use a
forloop if you need that.$_is aliased, not copied. Modifying$_inside the block modifies the list element:my @xs = (1, 2, 3); all { $_++; $_ > 0 } @xs; # @xs is now (2, 3, 4)
Experimental under 5.42. The feature emits a compile-time warning in the
experimental::keyword_allcategory unless that category is silenced:no warnings 'experimental::keyword_all';
Not the same as
List::Util::all. The built-in is a language keyword enabled by thekeyword_allfeature and parsesBLOCKas a true block (no leadingsub, no trailing comma).List::Util::allis a regular subroutine imported from a module and takes a code reference / block prototype argument. For most practical purposes they behave the same on non-empty lists, but:The keyword form is a parser-level construct and can be optimised more aggressively.
The two share neither implementation nor warning category.
Code that needs to run on pre-5.42 perls should use
List::Util::all.
Equivalent
grepform.all { COND } @xsis semantically equivalent to@xs == grep { COND } @xs— butgrepalways evaluates the block for every element, so it lacksall’s short-circuit behaviour.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
any— true if the block returns true for at least one element; the mirror operator toallgrep— evaluates the block for every element and returns matches;scalar @list == grep { … } @listisallwithout the short-circuitdefined— the predicate inside the canonicalall { defined $_ } @listcheckList::Util::all— module-level equivalent available on perls older than 5.42; also providesnoneandnotallwhich have no language-level counterpartList::Util::any— module-level counterpart to theanykeyword, alongsidenoneandnotallfor the opposite polarities