Skip to content

Commit 06b5c0c

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 46dd698 + 6e322dc commit 06b5c0c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

doc/Language/control.pod

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,21 @@ is equivalent to the C-ish idiom:
568568
569569
loop (;;) {...}
570570
571+
The C<loop> statement may be used to produce values from the result of each
572+
run of the attached block if it appears in lists:
573+
574+
(loop ( my $i = 0; $i++ < 3;) { $i * 2 }).say; #-> "(2 4 6)"
575+
my @a = (loop ( my $i = 0; $i++ < 3;) { $i * 2 }); @a.say; #-> "[2 4 6]"
576+
my @a = do loop ( my $i = 0; $i++ < 3;) { $i * 2 }); @a.say; # same thing
577+
578+
Unlike a C<for> loop, one should not rely on whether returned values are produced
579+
lazily, for now. It would probably be best to use C<eager> to guarantee that a
580+
loop whose return value may be used actually runs:
581+
582+
sub heads-in-a-row {
583+
(eager loop (; 2.rand < 1;) { "heads".say })
584+
}
585+
571586
=head2 X<while, until|control flow,while until>
572587
573588
The C<while> statement executes the block as long as its condition is
@@ -626,6 +641,8 @@ $x++ while $x < 12
626641
627642
Also see C<repeat/while> and C<repeat/until> below.
628643
644+
All these forms may produce a return value the same way C<loop> does.
645+
629646
=head2 X<repeat/while, repeat/until|control flow,repeat>
630647
631648
Perl 5 allows one to apply a statement modifier to a C<do> block such that
@@ -650,6 +667,8 @@ This can also be written quite naturally with C<until>:
650667
...
651668
} until $x >= 10;
652669
670+
All these forms may produce a return value the same way C<loop> does.
671+
653672
=head2 X<return|control flow>
654673
655674
=comment TODO

0 commit comments

Comments
 (0)