@@ -23,24 +23,38 @@ also be written as:
23
23
24
24
= head2 X < blocks|control flow >
25
25
26
- Like many languages, Perl6 uses C < blocks > delimited by C < { > and C < } >
27
- to compartmentalize code. When a block stands alone as a statement,
28
- it will be entered immediately after the statement before it finishes,
29
- and the statements inside it will be executed. Otherwise, a block
30
- 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:
31
45
32
46
say "We get here"; { say "then here." }; { say "not here"; 0; } or die;
33
47
34
- In the above example, after running the first statement, the first
35
- block stands alone as a second statement, so we run the statement inside
36
- it. The second block does not stand alone as a statement, so it instantiates
37
- an object of type C < Block > , but does not run it. Since any object instance
38
- is true, the code does not die, even though that block would evaluate to 0,
39
- 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.
40
55
41
- Most of the flow control constructs covered below are just ways to
42
- tell perl6 when, how, and how many times, to enter blocks like that
43
- 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.
44
58
45
59
Before we go into those, an important side-note on syntax: If there is
46
60
nothing (or nothing but comments) on a line after a closing curly brace where
0 commit comments