@@ -157,68 +157,88 @@ in list context.
157
157
158
158
= head1 Method Postfix Precedence
159
159
160
- = head2 postcircumfix C « { } »
160
+ = head2 postcircumfix C « [ ] »
161
161
162
- The hash indexing postcircumfix. Fail on type L < Any > , and on L < EnumMap > ,
163
- L < Hash > and related types it allows lookup of hash elements by key.
162
+ sub postcircumfix:<[ ]>(@container, **@index ,
163
+ :$k, :$v, :$kv, :$p, :$exists, :$delete)
164
164
165
- my %h = a => 1, b => 2;
166
- say %h{'a'}; # 1
167
- say %h{'a', 'b'}; # 1, 2
165
+ Universal interface for positional access to zero or more elements of a
166
+ @container, a.k.a. "array indexing operator".
168
167
169
- = head2 postcircumfix C « < > »
168
+ my @alphabet = 'a' .. 'z';
169
+ say @alphabet[0]; #-> a
170
+ say @alphabet[1]; #-> b
171
+ say @alphabet[*-1]; #-> z
172
+ say @alphabet[100]:exists; #-> False
173
+ say @alphabet[15, 4, 17, 11].join; #-> perl
174
+ say @alphabet[23 .. *].perl; #-> ("x", "y", "z")
175
+
176
+ @alphabet[1, 2] = "B", "C";
177
+ say @alphabet[0..3].perl #-> ("a", "B", "C", "d")
170
178
171
- The hash indexing quote-words operator. Interprets the argument list
172
- as a list of words, just like C << circumfix < > >> , and then calls
173
- C << postcircumfix:<{ }> >> , i.e. the hash indexing operator.
179
+ See L < Subscripts|/language/Subscripts > for a more detailed explanation of this
180
+ operator's behavior, and how to implement support for it in custom types.
174
181
175
- Thus you can write
182
+ = head2 postcircumfix C « { } »
176
183
177
- my %h = a => 1, b => 2;
178
- say %h<a>; # 1
179
- say %h<b a>; # 2, 1
184
+ sub postcircumfix:<{ }>(%container, **@key,
185
+ :$k, :$v, :$kv, :$p, :$exists, :$delete)
180
186
181
- This is not a real operator, just a syntactic desugaring to the C < { } >
182
- postcircumfix operator
187
+ Universal interface for associative access to zero or more elements of a
188
+ %container, a.k.a. "hash indexing operator".
183
189
184
- = head2 postcircumfix C « ( ) »
190
+ my %color = kiwi => "green", banana => "yellow", cherry => "red";
191
+ say %color{"banana"}; #-> yellow
192
+ say %color{"cherry", "kiwi"}.perl; #-> ("red", "green")
193
+ say %color{"strawberry"}:exists; #-> False
194
+
195
+ %color{"banana", "lime"} = "yellowish", "green";
196
+ %color{"cherry"}:delete;
197
+ say %color; #-> banana => yellowish, kiwi => green, lime => green
185
198
186
- The call operator. Treats the invocant as a L < Callable > and invokes it,
187
- using the expression between the parens as arguments.
199
+ See L « C « postcircumfix < > » |/routine/[ ]#postcircumfix_<_>» and
200
+ L < C < postcircumfix « » > |/routine/[ ]#postcircumfix_«_»> for convenient
201
+ shortcuts, and L < Subscripts|/language/Subscripts > for a more detailed
202
+ explanation of this operator's behavior, and how to implement support for it
203
+ in custom types.
188
204
189
- Note that an identifier followed by a pair of parens is always parsed as a
190
- subroutine call.
191
-
192
- If you want your objects to respond to the call operator, you need to
193
- implement a C << method postcircumfix:<( )> >> .
205
+ = head2 postcircumfix C « < > »
194
206
195
- = head2 postcircumfix C « [ ] »
207
+ Shortcut for L < C < postcircumfix { } > |/routine/[ ]#postcircumfix_{_}> that quotes
208
+ its argument using the same rules as the L « quote-words operator|
209
+ /routine/< >#circumfix_<_> » of the same name.
196
210
197
- The array indexing operator. Treats the invocant as a L < Positional > and
198
- indexes it by position.
211
+ my %color = kiwi => "green", banana => "yellow", cherry => "red";
212
+ say %color<banana>; #-> yellow
213
+ say %color<cherry kiwi>.perl; #-> ("red", "green")
214
+ say %color<strawberry>:exists; #-> False
199
215
200
- my @a = 'a' .. 'z';
201
- say @a[0]; # a
216
+ This is not a real operator, just syntactic sugar that is turned into the
217
+ C < { } > postcircumfix operator at compile-time.
202
218
203
- Lists of indexes produce list of results as if they were all indexed
204
- separately.
219
+ = head2 postcircumfix C < « » >
205
220
206
- my @a = 'a' .. 'z';
207
- say @a[15, 4, 17, 11].join; # perl
221
+ Shortcut for L < C < postcircumfix { } > |/routine/[ ]#postcircumfix_{_}> that quotes
222
+ its argument using the same rules as the L < interpolating quote-words operator|
223
+ /routine/« »#circumfix_«_» > of the same name.
208
224
209
- L < Callable > indexes are invoked with the number of elements as arguments.
225
+ my %color = kiwi => "green", banana => "yellow", cherry => "red";
226
+ my $fruit = "kiwi";
227
+ say %color«cherry $fruit».perl; #-> ("red", "green")
210
228
211
- This lets you write
229
+ This is not a real operator, just syntactic sugar that is turned into the
230
+ C < { } > postcircumfix operator at compile-time.
212
231
213
- my @a[*-1]; # z
232
+ = head2 postcircumfix C « ( ) »
214
233
215
- to index lists and arrays from the end.
234
+ The call operator. Treats the invocant as a L < Callable > and invokes it,
235
+ using the expression between the parens as arguments.
216
236
217
- Non-L < Positional > invocants are interpreted as a one-list element of the object.
237
+ Note that an identifier followed by a pair of parens is always parsed as a
238
+ subroutine call.
218
239
219
- See L < Positional|/type/Positional > for a more detailed description, and a list
220
- of low-level methods that this operator calls, and which types that want to
221
- offer an API similar to the built-in types can implement.
240
+ If you want your objects to respond to the call operator, you need to
241
+ implement a C << method postcircumfix:<( )> >> .
222
242
223
243
= head2 postfix C « . »
224
244
0 commit comments