YAP6 Operator: The Pair Constructor

by Adriano Ferreira

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


3 Comments

dextius
2007-12-27 10:49:38
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?

Adriano Ferreira
2007-12-28 12:59:28
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.
Jon
2008-01-19 07:00:39
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]?