@@ -890,18 +890,25 @@ C<given> can follow a statement to set the topic in the statement it follows.
890
890
= head1 X < loop|control flow, loop >
891
891
892
892
The C < loop > statement takes three statements in parentheses separated by C < ; >
893
- that take the role of initializer, conditional and incrementer. The initializer
894
- is executed once and any variable declaration will spill into the surrounding
895
- block. The conditional is executed once per iteration and coerced to C < Bool > ,
896
- if C < False > the loop is stopped. The incrementer is executed once per
897
- iteration. CAUTION: Even though the loop iterator variable is I < inside > the loop's
898
- parentheses, it is a lexical variable in the loop's I < outer or containing > scope and can
899
- be used in code following the loop.
900
-
901
- loop (my $i = 0; $i < 10; $i++) {
893
+ that take the roles of initializer, conditional and incrementer, respectively.
894
+ The initializer is executed once before the conditional is first tested. In case
895
+ the initializer involves a variable declaration, the variable is declared as a
896
+ lexical variable in the loop's I < outer or containing > scope so that it can be
897
+ used in code following the loop statement. The conditional is executed before
898
+ each iteration and coerced to C < Bool > ; if C < False > the loop is stopped. The
899
+ incrementer is executed after each iteration, and before the conditional is
900
+ tested again.
901
+
902
+ loop (my $i = 0; $i < 10; $i++) { # A typical loop
902
903
say $i;
903
904
}
904
905
906
+ my @str = "However Long".comb; # Our very own .char routine:
907
+ loop (my $l = 0;;) { # Declare $l in outer scope
908
+ last if !@str[$l++] # and count chars until we hit an
909
+ } # undefined element (Any)
910
+ say "The string is {--$l} chars long.";
911
+
905
912
The infinite loop does not require parentheses.
906
913
907
914
= for code
0 commit comments