first#
Return the first list element for which a block returns true.
Synopsis#
my $val = first { BLOCK } @list;
my $d = first { defined } @list;
my $big = first { $_ > 100 } @values;
For each element first sets $_ to that element and calls the
block in scalar context; the first element for which the block
returns true is returned. Remaining elements are not examined.
What you get back#
The matching element (the original value, not the block’s return).
If no element matches — or the list is empty — returns undef.
Examples#
my $first_defined = first { defined $_ } @list;
my $first_big = first { $_ > $threshold } @measurements;
my $first_admin = first { $_->is_admin } @users;
Differences from upstream#
Fully compatible with upstream.
See also#
any— boolean “at least one matches” test.grep— collect every matching element.pairfirst— pair-oriented counterpart.