Name

first

Synopsis

first { BLOCK } @list
                  

Evaluates a block of Perl code and sets $_ to each element of the list in turn. If BLOCK is true, first returns the first element. If BLOCK never returns true, or @ list has no items, then first returns undef. Note that first doesn’t necessarily return the first item in a list. Consider the following:

my @ll = qw(1 2 3);
my $fir = first { $_ > 1 } @ll;
print "$fir\n"; # Prints '2', since as 2 is the first item
                # in BLOCK that's > 1

Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.