@@ -13,7 +13,7 @@ Synopsis 4: Blocks and Statements
13
13
Created: 19 Aug 2004
14
14
15
15
Last Modified: 26 Nov 2013
16
- Version: 131
16
+ Version: 132
17
17
18
18
This document summarizes Apocalypse 4, which covers the block and
19
19
statement syntax of Perl.
@@ -667,33 +667,20 @@ The final statement of a statement list is not a sink context, and
667
667
can return any value including a lazy list. However, to support the
668
668
expectations of imperative programmers (the vast majority of us,
669
669
it turns out), any explicit loop found as the final statement of
670
- a statement list is automatically forced to use eager semantics so
670
+ a statement list is automatically forced to use sink semantics so
671
671
that the loop executes to completion before returning from the block.
672
672
673
- In particular, a C<for> loop still returns the loop results as a list,
674
- but only after the loop completes. (To suppress even this list return,
675
- put a return statement after the C<for> loop, or declare the subroutine
676
- with a return type that indicates no return value is expected.)
677
-
678
- For other loops such as C<while>, C<repeat>, and C<loop>, not only
679
- are they assumed eager, the loops will also be assumed to be in sink
680
- context at statement level.
681
-
682
- This forced eager context (and sink context, if applicable)
683
- is applied to loops I<only> at the statement list
673
+ This forced sink context is applied to loops I<only> at the statement list
684
674
level, that is, at the top level of a compilation unit, or directly
685
675
inside a block. Constructs that parse a single statement or semilist
686
676
as an argument are presumed to want the results of that statement,
687
677
so such constructs remain lazy even when that statement is a loop.
688
678
Assuming each of the following statements is the final statement in a block,
689
- eager loops such as these may be indicated:
679
+ "sunk" loops such as these may be indicated:
690
680
691
- # assumed eager context
692
681
for LIST { ... }
693
- ... if COND for LIST # eager list comprehension
694
-
695
- # assumed sink context
696
- loop { ... } # (assumes sink in addition to eager)
682
+ ... if COND for LIST
683
+ loop { ... }
697
684
... while COND
698
685
while COND { ... }
699
686
repeat until COND { ... }
@@ -710,14 +697,26 @@ or by use of either a statement prefix or a phaser in statement form:
710
697
ENTER for LIST { ... }
711
698
712
699
Note that the corresponding block forms put the loop into a statement list, so
713
- these loops are eagerly evaluated:
700
+ these loops are evaluated in sink context :
714
701
715
702
lazy { for LIST { ... } } # useless use of 'lazy' here
716
703
ENTER { for LIST { ... } }
717
704
718
705
It doesn't matter that there is only one statement there; what matters
719
706
is that a sequence of statements is expected there by the grammar.
720
707
708
+ An eager loop may likewise be indicated by using the C<eager> statement prefix:
709
+
710
+ eager for LIST { ... }
711
+ eager ... if COND for LIST
712
+ eager loop { ... }
713
+ eager ... while COND
714
+ eager while COND { ... }
715
+ eager repeat until COND { ... }
716
+
717
+ It is erroneous to write an eager loop without a loop exit, since that will chew up
718
+ all your memory.
719
+
721
720
Note that since C<do> is considered a one-time loop, it is always
722
721
evaluated eagerly, despite being a statement prefix. This is no great
723
722
hardship; the C<lazy> prefix is better documentation in any case.
0 commit comments