Another article of the series “Yet Another Perl 6 Operator”
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.
DISCLAIMERS
- These articles are not intented as a deep presentation of Perl 6 features, but only as a quick demonstration. If you want more, get involved.
- Some of the code here is unimplemented in current Perl 6 implementations.
- The flaws of these articles is sole responsibility of the author.
Next article is due tomorrow (Dec 28, 2007).
LINKS
- Synopsis S03, the official source
- The introduction of this series
- Official Perl 6 Documentation
- Perl 6 in your browser


You have indirectly introduced me to a new concept that I was not aware of in perl6. "Item Context" sounds like a smart version of scalar context. You placed a list as a key in your pair, but instead of it turning into scalar context (and becoming a "3") it maintained it's state as a list.
Are there any other new contexts? Does List and Scalar context still exist?
Perl 6 still has scalar, list and void contexts. It adds some specialized scalar contexts like boolean, string, numeric, which Perl 5 already has internally (but maybe not that structured as in the new language) and which could be guessed by the overloaded context operators like
${}, bool, +, ""and modules like Want. It has some contexts like item to get together all but junctions IIRC.You have $kv[0] and $kv[1] to get at individual elements of your @kv array, but in Perl6 shouldn't that read @kv[0] and @kv[1]?