Skip to content

Commit af677c4

Browse files
committed
Added docs for C<for> in lib/Language/control.pod
1 parent da7dc52 commit af677c4

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

lib/Language/control.pod

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,49 @@ example because it is not caught up to this part of the design yet.
240240
241241
=head2 for
242242
243-
=comment TODO
243+
The C<for> loop iterates over a list.
244+
245+
=begin code
246+
247+
for @foo {.print}
248+
249+
=end code
250+
251+
Use pointy block syntax to get an iteration variable.
252+
253+
=begin code
254+
255+
for @foo -> $item { print $item }
256+
257+
=end code
258+
259+
Multiple parameters can be declared.
260+
261+
=begin code
262+
263+
for %hash.kv -> $key, $value { print "$key => $value\n" }
264+
265+
=end code
266+
267+
The list in a C<for> loop is evaluated lazily by default, so to read a file
268+
line by line, you could use
269+
270+
=begin code
271+
272+
for $*IN.lines -> $line {...}
273+
274+
=end code
275+
276+
Iteration variables are always lexical, so you don't need to use C<my> to give
277+
them the appropriate scope. Also, they are read-only aliases. If you need them
278+
to be read-write, use C«<->» instead of C«->». If you need to make C<$_>
279+
read-write in a for loop, do so explicitly.
280+
281+
=begin code
282+
283+
for @cars <-> $_ {...}
284+
285+
=end code
244286
245287
=head2 gather/take
246288

0 commit comments

Comments
 (0)