@@ -46,7 +46,7 @@ allows arbitrary objects as keys, etc.:
46
46
say %grade{"Ben"}; #-> B+
47
47
48
48
my $stats = ( Date.today => 4.18, Date.new(2015, 4, 5) => 17.253 ).Mix;
49
- say $stats{ Date.new(2015, 4, 5) }; #-> 17.253
49
+ say $stats{ Date.new(2015, 4, 4) + 1 }; #-> 17.253
50
50
51
51
For passing single-word string keys to C < { } > , you can also use the
52
52
L < angle-bracketed word quoting constructs|/language/quoting#Word_quoting:_qw >
@@ -62,6 +62,40 @@ C<{ }> form at compile-time:
62
62
%hash«foo $var»; # same as %hash{ «foo $var» }
63
63
%hash<<foo $var>>; # same as %hash{ <<foo $var>> }
64
64
65
+ You may have noted above that we avoided having to quote C < Zoe > by using
66
+ the C < = > > operator, but that same operator did not just put invisible
67
+ quotes around C < Date.new(2015, 4, 5) > , and we were able to find the
68
+ same element using C < $stats{ Date.new(2015, 4, 4) + 1 } > . This is because
69
+ C < = > > only puts invisible quotes around single words, and by "word" we
70
+ mean an identifier/name. The C < = > > operator is there to prevent us from
71
+ accidentally calling functions or using constants with that name.
72
+
73
+ Hash subscripts do not do the same thing as C < = > >. The default C < Hash >
74
+ has been made to behave the way new users have come to expect
75
+ from using other languages, and for general ease of use. On a
76
+ default C < Hash > , subscripts coerce keys into strings, as long as
77
+ those keys produce something C < Cool > . You can use C < .perl > on a
78
+ collection to be sure whether the keys are strings or objects:
79
+
80
+ ( 1 => 1 ).perl.say #-> 1 => 1
81
+ my %h; %h{1} = 1; say %h.perl; #-> { "1" => 1 }
82
+ ( 1/2 => 1 ).perl.say #-> 0.5 => 1
83
+ my %h; %h{1/2} = 1; say %h.perl; #-> { "0.5" => 1 }
84
+ ( pi => 1 ).perl.say #-> :pi(1)
85
+ my %h; %h{pi} = 1; say %h.perl; #-> { "3.14159265358979" => 1 }
86
+
87
+ While the invisible quotes around single names is built into C < = > >,
88
+ string conversion is not built into the braces: it is a behavior
89
+ of the default C < Hash > . Not all types of hashes or collections
90
+ do so:
91
+
92
+ my %h := MixHash.new;
93
+ $h{pi} = 1; %h.perl.say; #-> (3.14159265358979e0=>1).MixHash
94
+
95
+ (Any name that C < = > > would convert to a string can also be used to build
96
+ a pair using "adverbial notation" and will appear that way when viewed
97
+ through C < .perl > , which is why we see C < :pi(1) > above.)
98
+
65
99
= end item1
66
100
67
101
Subscripts can be applied to any expression that returns a subscriptable
@@ -97,8 +131,9 @@ is L<Any>):
97
131
However, other types of collections may react differently to subscripts that
98
132
address nonexistent elements:
99
133
100
- say (0, 10, 20)[3]; #-> Nil
101
- say bag(<a a b b b>)<c>; #-> 0
134
+ say (0, 10, 20)[3]; #-> Nil
135
+ say bag(<a a b b b>)<c>; #-> 0
136
+ say array[uint8].new(1,2)[2] #-> 0
102
137
103
138
To silently skip nonexistent elements in a subscripting operation, see
104
139
L < #Truncating slices > and the L < #:v > adverb.
0 commit comments