Skip to content

Instantly share code, notes, and snippets.

@dha
Last active September 10, 2015 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dha/d8ab9b8cf852d358bfaf to your computer and use it in GitHub Desktop.
Save dha/d8ab9b8cf852d358bfaf to your computer and use it in GitHub Desktop.
Suggested documentation for C<for> in Perl 6
=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
@coke
Copy link

coke commented Sep 8, 2015

if this isn't going in the 5 to 6 section, I'd leave out the reference to 5 behavior. dha++

@raiph
Copy link

raiph commented Sep 9, 2015

parameters vs arguments

Maybe
Multiple parameters can be declared
or
Multiple arguments can be passed

@dha
Copy link
Author

dha commented Sep 10, 2015

Edited to include coke and raiph's suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment