pairfirst#

Return the first pair for which a block returns true.

Synopsis#

my ($k, $v) = pairfirst { BLOCK } @kvlist;
my $found   = pairfirst { BLOCK } @kvlist;   # scalar context

For each pair the block is called in scalar context with $a set to the key and $b set to the value. pairfirst stops at the first pair for which the block returns true.

What you get back#

In list context, the matching ($key, $value) pair, or an empty list if nothing matched. In scalar context, a boolean indicating whether any pair matched. $a and $b are aliased to the original list elements.

Examples#

my ($k, $v) = pairfirst { $a =~ /^upper_/ } %config;
if ( pairfirst { $b > 1_000 } %totals ) {
    warn "at least one big entry";
}

Differences from upstream#

Fully compatible with upstream.

See also#

  • first — non-pair equivalent, uses $_.

  • pairgrep — collect every matching pair.