Yet Another Perl 6 Operator: The Pair Constructor


Maintainer: Adriano Ferreira <>
Date: 22 Dec 2007
Last Modified: 22 Dec 2007
Number: 16
Version: 1
Status: Unfinished


Binary '=>' is no longer just a "fancy comma". In Perl 6, it now constructs a Pair object that can, among other things, be used to pass named arguments to functions.

my $pair = (one => 1); 
$pair.isa(Pair)        # Bool::True
$pair.key              # 'one'
$pair.value            # 1

Just like Perl 5, it autoquotes identifiers on its left. The operator provides item context to both sides. That explains

my @a = (1,2,3);
my $p = @a => '123';
$p.isa(Pair)           # Bool::True
$p.key                 # (1,2,3)
$p.value               # '123'

because @a in item context coerces into the Array object itself, instead of the array size as Perl 5 did.

The fatarrow is right associative.

a => b => 'c'   # a => (b => 'c')

Its precedence is now equivalent to assignment, although it does not actually do any assignment except in a notional sense. Unlike in Perl 5, '=>' binds tighter than comma.

As expected (for consistency with how it worked in Perl 5), when a pair is flattened into a list context, it produces a list with its key and value.

my @kv = @(one => 1);
$kv[0]               # 'one'
$kv[1]               # 1

The fat arrow construct is accompanied by a lot of syntax to build pairs known as the adverbial forms of Pair notation.

Fat arrow          Adverbial pair
a => 1             :a
a => 0             :!a
a => 'foo'         :a<foo>

These are meant to provide handy forms to typical cases like expressing pairs with literal key and value. (All adverbial forms are listed in Synopsis 02 .) That adverbial notation has a lot of ramifications in other parts of Perl 6 syntax, which favours them over the explicit fatarrow notation.

In the end, that means that this operator will not be so common in Perl 6 as it was in Perl 5, being available as the unsugared version to build pairs.

$Revision: 120 $