Last active
September 10, 2015 20:45
-
-
Save dha/d8ab9b8cf852d358bfaf to your computer and use it in GitHub Desktop.
Suggested documentation for C<for> in Perl 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=head2 for | |
The C<for> loop iterates over a list. | |
=begin code | |
for @foo {.print} | |
=end code | |
Use pointy block syntax to get an iteration variable. | |
=begin code | |
for @foo -> $item { print $item } | |
=end code | |
Multiple parameters can be declared. | |
=begin code | |
for %hash.kv -> $key, $value { print "$key => $value\n" } | |
=end code | |
The list in a C<for> loop is evaluated lazily by default, so so to read a file line by line, you could use | |
=begin code | |
for $*IN.lines -> $line {...} | |
=end code | |
Iteration variables are always lexical, so you don't need to use C<my> to give | |
them the appropriate scope. Also, they are read-only aliases. If you need them | |
to be read-write, use C«<->» instead of C«->». If you need to make C<$_> | |
read-write in a for loop, do so explicitly. | |
=begin code | |
for @cars <-> $_ {...} | |
=end code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Edited to include coke and raiph's suggestions.