File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -533,6 +533,32 @@ If you are working with regexes, then there is no need to use C<.fc>
533
533
and you can use C < :i > (C < :ignorecase > ) adverb instead.
534
534
535
535
536
+ = head1 Pairs
537
+
538
+ = head2 Constants on LHS of pair notation
539
+
540
+ Consider this code:
541
+
542
+ = begin code
543
+ enum Animals <Dog Cat>;
544
+ my %h := :{ Dog => 42 };
545
+ say %h{Dog}; # OUTPUT: «(Any)»
546
+ = end code
547
+
548
+ The C < :{ … } > syntax is used to create
549
+ L < object hashes|/type/Hash#Non-string_keys_(object_hash) > . The
550
+ intentions of someone who wrote that code were to create a hash with
551
+ Enum objects as keys (and C < say %h{Dog} > attempts to get a value using
552
+ the Enum object to perform the lookup). However, that's not how pair
553
+ notation works.
554
+
555
+ For exapmple, in C « Dog => 42 » the key will be a C < Str > . That is, it
556
+ doesn't matter if there is a constant, or an enumeration with the
557
+ same name. The pair notation will always use the left-hand side as a
558
+ string literal, as long as it looks like an identifier.
559
+
560
+ To avoid this, use C « (Dog) => 42 » or C « ::Dog => 42 » .
561
+
536
562
= head1 Operators
537
563
538
564
Some operators commonly shared among other languages were repurposed in Perl 6 for other, more common, things:
You can’t perform that action at this time.
0 commit comments