pairs#

Group an even-sized list into key/value pair objects.

Synopsis#

my @pairs = pairs @kvlist;
for my $p ( pairs %hash ) {
    my ($k, $v) = @$p;           # or $p->key / $p->value
}

What you get back#

A list of two-element blessed array references (class List::Util::_Pair). Each pair responds to ->key, ->value, and ->TO_JSON, and can also be dereferenced as a plain arrayref. For an odd-sized input, the final pair’s value is undef.

Examples#

for my $p ( pairs one => 1, two => 2 ) {
    print $p->key, '=', $p->value, "\n";
}

## one=1

## two=2
my @sorted = sort { $a->key cmp $b->key } pairs %h;

Differences from upstream#

Fully compatible with upstream.

See also#

  • unpairs — inverse: flatten an arrayref list back to key/value.

  • pairkeys / pairvalues — extract just one side.

  • pairmap / pairgrep — transform or filter pairs in place.