Colons can be used as part of a variable name in Perl 6, together with angle brackets. These are apparently called extended identifiers, and are used to define things such as infix:<+>
.
my $foo:bar = 3;
my $foo:bar<2> = 2;
my $foo:bar<baz> = 'quux';
say $foo:bar; # 3
say $foo:bar<2>; # 2 (and so on)
This creates identifiers with the same name in the current scope
say MY::.keys;
Prints ($=pod $_ $/ $buz !UNIT_MARKER $=finish EXPORT $foo:bar<2> $foo:bar<baz> $! ::?PACKAGE GLOBALish $bur::quux $¢ $foo:bar $?PACKAGE
But here's the thing.
say $foo:bar.kv; # prints key-value pairs.
print (0 3)
. So these coloned variables are creating a key-value pair. However, the other two "keys" (2
and baz
) are not included in that set of key-value pairs. And if we really try to do say $foo:bar{'0'}
or say $foo:bar<0>;
we will obtain different errors, so there does not seem to be an actual way of using that as a real key. So I guess there are at least a couple of questions here:
infix
variables?So these coloned variables are creating a key-value pair.
No, .kv
(or kv
) is producing the key-value pair:
my $foo = 3;
say kv $foo # (0 3)
When you call .kv
on a list you get a list of indexes each followed by the associated value.
And every singular value will be treated as a list containing one value when you call a listy method on it.
Basically these are the same:
$foo:bar.kv
$foo:bar.list.kv
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With