Skip to content

Commit d0e295b

Browse files
committed
solidify documentation of subscripts at /language/operators
The corresponding secions now provide a short overview / usage demonstration, and link to /language/subscripts for further details. Also moved up the .[ ] section so that all subscript operator sections are grouped together.
1 parent f80d844 commit d0e295b

File tree

1 file changed

+61
-41
lines changed

1 file changed

+61
-41
lines changed

lib/Language/operators.pod

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -157,68 +157,88 @@ in list context.
157157
158158
=head1 Method Postfix Precedence
159159
160-
=head2 postcircumfix C«{ }»
160+
=head2 postcircumfix C«[ ]»
161161
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)
164164
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".
168167
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")
170178
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.
174181
175-
Thus you can write
182+
=head2 postcircumfix C«{ }»
176183
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)
180186
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".
183189
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
185198
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.
188204
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«< >»
194206
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.
196210
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
199215
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.
202218
203-
Lists of indexes produce list of results as if they were all indexed
204-
separately.
219+
=head2 postcircumfix C<« »>
205220
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.
208224
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")
210228
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.
212231
213-
my @a[*-1]; # z
232+
=head2 postcircumfix C«( )»
214233
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.
216236
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.
218239
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:<( )> >>.
222242
223243
=head2 postfix C«.»
224244

0 commit comments

Comments
 (0)