@@ -568,6 +568,21 @@ is equivalent to the C-ish idiom:
568
568
569
569
loop (;;) {...}
570
570
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
+
571
586
= head2 X < while, until|control flow,while until >
572
587
573
588
The C < while > statement executes the block as long as its condition is
@@ -626,6 +641,8 @@ $x++ while $x < 12
626
641
627
642
Also see C < repeat/while > and C < repeat/until > below.
628
643
644
+ All these forms may produce a return value the same way C < loop > does.
645
+
629
646
= head2 X < repeat/while, repeat/until|control flow,repeat >
630
647
631
648
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>:
650
667
...
651
668
} until $x >= 10;
652
669
670
+ All these forms may produce a return value the same way C < loop > does.
671
+
653
672
= head2 X < return|control flow >
654
673
655
674
= comment TODO
0 commit comments