4
4
5
5
= SUBTITLE Statements used to control the flow of execution
6
6
7
+ = head2 X < statements|control flow >
8
+
9
+ Perl 6 programs consists of one or more statements. Simple statements
10
+ are separated by semicolons. The following program will say "Hello"
11
+ and then say "World" on the next line.
12
+
13
+ say "Hello";
14
+ say "World";
15
+
16
+ In most places where spaces appear in a statement, and before the
17
+ semicolon, it may be split up over many lines. Also, multiple statements
18
+ may appear on the same line. It would be awkward, but the above could
19
+ also be written as:
20
+
21
+ say
22
+ "Hello"; say "World";
23
+
7
24
= head2 X < blocks|control flow >
8
25
9
- Like many languages, Perl6 uses C < blocks > delimited by C < { > and C < } >
10
- to compartmentalize code. When a block stands alone as a statement,
11
- it will be entered immediately after the statement before it finishes,
12
- and the statements inside it will be executed. Otherwise, a block
13
- simply creates a closure, which may be executed at a later time:
26
+ Like many languages, Perl6 uses C < blocks > enclosed by C < { > and C < } > to turn
27
+ multiple statements into a single statement. It is ok to skip the semicolon
28
+ between the last statement in a block and the closing C < } > .
29
+
30
+ { say "Hello"; say "World" }
31
+
32
+ When a block stands alone as a statement, it will be entered immediately
33
+ after the previous statement finishes, and the statements inside it will be
34
+ executed.
35
+
36
+ say 1; # says "1"
37
+ { say 2; say 3 }; # says "2" then says "3"
38
+ say 4; # says "4"
39
+
40
+ Unless it stands alone as a statement, a block simply creates a closure. The
41
+ statements inside are not executed immediately. Closures are another topic
42
+ and how they are used is explained
43
+ L < elsewhere|language/functions#Blocks and Lambdas > . For now it is just
44
+ important to understand when blocks run and when they do not:
14
45
15
46
say "We get here"; { say "then here." }; { say "not here"; 0; } or die;
16
47
17
- In the above example, after running the first statement, the first
18
- block stands alone as a second statement, so we run the statement inside
19
- it. The second block does not stand alone as a statement, so it instantiates
20
- an object of type C < Block > , but does not run it. Since any object instance
21
- is true, the code does not die, even though that block would evaluate to 0,
22
- were it to be executed.
48
+ In the above example, after running the first statement, the first block stands
49
+ alone as a second statement, so we run the statement inside it. The second
50
+ block does not stand alone as a statement, so instead, it makes an object of
51
+ type C < Block > but does not run it. Object instances are usually considered to
52
+ be true, so the code does not die, even though that block would evaluate to 0,
53
+ were it to be executed. The example does not say what to do with the C < Block >
54
+ object, so it just gets thrown away.
23
55
24
- Most of the flow control constructs covered below are just ways to
25
- tell perl6 when, how, and how many times, to enter blocks like that
26
- second block.
56
+ Most of the flow control constructs covered below are just ways to tell perl6
57
+ when, how, and how many times, to enter blocks like that second block.
27
58
28
59
Before we go into those, an important side-note on syntax: If there is
29
60
nothing (or nothing but comments) on a line after a closing curly brace where
@@ -116,19 +147,13 @@ run the block, or it will return the value which the block produces:
116
147
my $c = 0; say (1, (if 1 { $c += 42; 2; }), 3, $c); # says "1 2 3 42"
117
148
my $d = 0; say (1, (if 0 { $d += 42; 2; }), 3, $d); # says "1 3 0"
118
149
119
- Implementation note: Currently, Rakudo will say "1 Nil 3 0" for the last
120
- example because it is not caught up to this part of the design yet.
121
-
122
150
For the statement modifier it is the same, except you have the value
123
151
of the statement instead of a block:
124
152
125
153
say (1, (42 if True) , 2); # says "1 42 2"
126
154
say (1, (42 if False), 2); # says "1 2"
127
155
say (1, 42 if False , 2); # says "1 42" because "if False, 2" is true
128
156
129
- Implementation note: Currently, Rakudo will say "1 Nil 2" for the second
130
- example because it is not caught up to this part of the design yet.
131
-
132
157
The C < if > does not change the topic (C < $_ > ) by default. In order to access
133
158
the value which the conditional expression produced, you have to ask
134
159
for it more strongly:
@@ -200,9 +225,6 @@ or the value produced by the block that did run:
200
225
(if 0 { $d += 42; "two"; } elsif False { $d += 43; 2; }),
201
226
3, $d); # says "1 3 0"
202
227
203
- Implementation note: Currently, Rakudo will say "1 Nil 3 0" for the last
204
- example because it is not caught up to this part of the design yet.
205
-
206
228
It's possible to obtain the value of the previous expression inside an
207
229
C < else > , which could be from C < if > or the last C < elsif > if any are
208
230
present:
@@ -234,9 +256,6 @@ two differences C<unless> works the same as L<if>:
234
256
my $c = 0; say (1, (unless 0 { $c += 42; 2; }), 3, $c); #-> says "1 2 3 42"
235
257
my $c = 0; say (1, (unless 1 { $c += 42; 2; }), 3, $c); #-> says "1 3 0"
236
258
237
- Implementation note: Currently, Rakudo will say "1 Nil 3 0" for the last
238
- example because it is not caught up to this part of the design yet.
239
-
240
259
= head3 X < with, orwith, without|control flow,with orwith without >
241
260
242
261
The C < with > statement is like C < if > but tests for definedness rather than
@@ -394,7 +413,8 @@ as though the rest of the statements in the block are skipped.
394
413
}
395
414
# The above block evaluates to 43
396
415
397
- A C < when > statement will also do this.
416
+ A C < when > statement will also do this (but a C < when > statement modifier
417
+ will I < not > .)
398
418
399
419
In addition, C < when > statements C < smartmatch > the topic (C < $_ > ) against
400
420
a supplied expression such that it is possible to check against values,
@@ -417,6 +437,20 @@ C<when> statements. The following code says C<"Int"> not C<42>.
417
437
}
418
438
#-> Int
419
439
440
+ When a C < when > statement or C < default > statement causes the outer
441
+ block to return, nesting C < when > or C < default > blocks do not count
442
+ as the outer block, so you can nest these statements and still
443
+ be in the same "switch" just so long as you do not open a new block:
444
+
445
+ given 42 {
446
+ when Int {
447
+ when 42 { say 42 }
448
+ say "Int"
449
+ }
450
+ default { say "huh?" }
451
+ }
452
+ #-> 42
453
+
420
454
= head3 X < proceed and succeed|control flow,proceed succeed >
421
455
422
456
Both C < proceed > and C < succeed > are meant to be used only from inside C < when >
@@ -479,6 +513,22 @@ specify a final value for the block.
479
513
}
480
514
#-> Int
481
515
516
+ If you are not inside a when or default block, it is an error to try
517
+ to use C < proceed > or C < succeed > . Also remember, the C < when > statement
518
+ modifier form does not cause any blocks to be left, and any C < succeed >
519
+ or C < proceed > in such a statement applies to the surrounding clause,
520
+ if there is one:
521
+
522
+ given 42 {
523
+ { say "This says" } when Int;
524
+ "This says too".say;
525
+ when * > 41 {
526
+ { "And this says".say; proceed } when * > 41;
527
+ "This never says".say;
528
+ }
529
+ "This also says".say;
530
+ }
531
+
482
532
= head2 X < loop|control flow >
483
533
484
534
The C < loop > statement is the C-style C < for > loop in disguise:
0 commit comments