Lists

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#

  • $_ — aliased to each element of LIST in turn for the duration of the block. Because this is an alias and not a copy, assigning to $_ inside the block mutates the underlying list element. The previous value of $_ is restored when all returns.

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 { ... } @list when 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 for loop 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_all category 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 the keyword_all feature and parses BLOCK as a true block (no leading sub, no trailing comma). List::Util::all is 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 grep form. all { COND } @xs is semantically equivalent to @xs == grep { COND } @xs — but grep always evaluates the block for every element, so it lacks all’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 to all

  • grep — evaluates the block for every element and returns matches; scalar @list == grep { } @list is all without the short-circuit

  • defined — the predicate inside the canonical all { defined $_ } @list check

  • List::Util::all — module-level equivalent available on perls older than 5.42; also provides none and notall which have no language-level counterpart

  • List::Util::any — module-level counterpart to the any keyword, alongside none and notall for the opposite polarities