class Pair
src
class Pair is Enum { ... }
Consists of two parts, a I<key> and a I<value>. Pair s can be seen as the
atomic units in Hash es, and they are also used in conjunction with named
arguments and parameters.
X<:> X<< => >> X<:!>
There are three syntaxes for Pair s:
'key' => 'value' # this...
:key<value> # ...means the same as this
:$foo # short for foo => $foo
Variants of this are
:key # same as key => True
:!key # same as key => False
The immutable version of a Pair is an Enum .
Methods
value
multi method value(Pair:D:) is rw
Returns the I<value> part of the Pair .
cmp
multi sub infix:<cmp>(Pair:D, Pair:D)
The type-agnostic comparator; compares two Pair s. Compares first their
I<key> parts, and then compares the I<value> parts if the keys are equal.
fmt
multi method fmt(Pair:D:) returns Str:D
Takes a I<format string>, and returns a string the I<key> and I<value>
parts of the Pair formatted. Here's an example:
my $pair = :Earth(1);
say $pair.fmt("%s is %.3f AU away from the sun")
# Prints "Earth is 1.000 AU away from the sun"
For more about format strings, see L<sprintf>.
kv
multi method kv(Pair:D:) returns Parcel:D
Returns a two-element Parcel with the I<key> and I<value> parts o
Pair , in that order. This method is a special case of the same-named
method on Hash , which returns all its entries as a list of keys and
values.
pairs
multi method pairs(Pair:D:)
Returns a list of one Pair , namely this one.