Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Document return value of loop statement
  • Loading branch information
skids committed Dec 25, 2015
1 parent 047a3cf commit 9edcf11
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions doc/Language/control.pod
Expand Up @@ -568,6 +568,21 @@ is equivalent to the C-ish idiom:
loop (;;) {...}
The C<loop> statement may be used to produce values from the result of each
run of the attached block if it appears in lists:
(loop ( my $i = 0; $i++ < 3;) { $i * 2 }).say; #-> "(2 4 6)"
my @a = (loop ( my $i = 0; $i++ < 3;) { $i * 2 }); @a.say; #-> "[2 4 6]"
my @a = do loop ( my $i = 0; $i++ < 3;) { $i * 2 }); @a.say; # same thing
Unlike a C<for> loop, one should not rely on whether returned values are produced
lazily, for now. It would probably be best to use C<eager> to guarantee that a
loop whose return value may be used actually runs:
sub heads-in-a-row {
(eager loop (; 2.rand < 1;) { "heads".say })
}
=head2 X<while, until|control flow,while until>
The C<while> statement executes the block as long as its condition is
Expand Down

0 comments on commit 9edcf11

Please sign in to comment.