4
4
5
5
= SUBTITLE Statements used to control the flow of execution
6
6
7
- = head2 blocks
7
+ = head2 X < blocks|control flow >
8
8
9
9
Like many languages, Perl6 uses C < blocks > delimited by C < { > and C < } >
10
10
to compartmentalize code. When a block stands alone as a statement,
@@ -48,7 +48,7 @@ You have to watch out for this in most languages anyway to prevent things
48
48
from getting accidentally commented out. Many of the examples below may
49
49
have unnecessary semicolons for clarity.
50
50
51
- = head2 do
51
+ = head2 X < do|control flow >
52
52
53
53
The simplest way to run a block where it cannot be a stand-alone statement
54
54
is by writing C < do > before it:
@@ -138,7 +138,7 @@ for it more strongly:
138
138
$_ = 1; if 42 -> $a { $_.say; $a.say } ; # says "1" then says "42"
139
139
$_ = 1; if 42 { $_.say; $^a.say } ; # says "1" then says "42"
140
140
141
- = head3 else/elsif
141
+ = head3 X < else/elsif|control flow,else elsif >
142
142
143
143
A compound conditional may be produced by following an C < if > conditional
144
144
with C < else > to provide an alternative block to run when the conditional
@@ -213,7 +213,7 @@ what the C<else> sees, since it was the last one tried:
213
213
214
214
if False { } elsif 0 { } else -> $a { $a.say } ; # says "0"
215
215
216
- = head3 unless
216
+ = head3 X < unless|control flow >
217
217
218
218
When you get sick of typing "if not (X)" you may use C < unless > to invert
219
219
the sense of a conditional statement. You cannot use C < else > or C < elsif >
@@ -238,7 +238,7 @@ two differences C<unless> works the same as L<if>:
238
238
Implementation note: Currently, Rakudo will say "1 Nil 3 0" for the last
239
239
example because it is not caught up to this part of the design yet.
240
240
241
- = head3 with, orwith, without
241
+ = head3 X < with, orwith, without|control flow,with orwith without >
242
242
243
243
The C < with > statement is like C < if > but tests for definedness rather than
244
244
truth. In addition, it topicalizes on the condition, much like C < given > :
@@ -264,7 +264,7 @@ There are also C<with> and C<without> statement modifiers:
264
264
return 42 with $answer;
265
265
.throw without $answer;
266
266
267
- = head2 for
267
+ = head2 X < for|control flow >
268
268
269
269
The C < for > loop iterates over a list.
270
270
@@ -310,7 +310,7 @@ for @cars <-> $_ {...}
310
310
311
311
= end code
312
312
313
- = head2 gather/take
313
+ = head2 X < gather/take|control flow,gather take >
314
314
315
315
C < gather > is a statement or block prefix that returns a L < sequence|/type/Seq >
316
316
of values. The values come from calls to C < take > in the dynamic scope of the C < gather > block.
@@ -360,7 +360,7 @@ from within C<gather>:
360
360
say weird(<a b c>, :direction<backward> ); # (c b a)
361
361
362
362
363
- = head2 given
363
+ = head2 X < given|control flow >
364
364
365
365
The C < given > statement is Perl 6's topicalizing keyword in a similar way that
366
366
C < switch > topicalizes in languages such as C. In other words, C < given >
@@ -382,7 +382,7 @@ This is a lot more understandable than:
382
382
{ .say; .Numeric; }(EXPR)
383
383
384
384
385
- = head3 default and when
385
+ = head3 X < default and when|control flow,default when >
386
386
387
387
A block containing a C < default > statement will be left immediately
388
388
when the sub-block after the C < default > statement is left. It is
@@ -418,7 +418,7 @@ C<when> statements. The following code says C<"Int"> not C<42>.
418
418
}
419
419
#-> Int
420
420
421
- = head3 proceed and succeed
421
+ = head3 X < proceed and succeed|control flow,proceed succeed >
422
422
423
423
Both C < proceed > and C < succeed > are meant to be used only from inside C < when >
424
424
or C < default > blocks.
@@ -480,7 +480,7 @@ specify a final value for the block.
480
480
}
481
481
#-> Int
482
482
483
- = head2 loop
483
+ = head2 X < loop|control flow >
484
484
485
485
The C < loop > statement is the C-style C < for > loop in disguise:
486
486
@@ -498,7 +498,7 @@ is equivalent to the C-ish idiom:
498
498
499
499
loop (;;) {...}
500
500
501
- = head2 while, until
501
+ = head2 X < while, until|control flow,while until >
502
502
503
503
The C < while > statement executes the block as long as its condition is
504
504
true. So
@@ -556,7 +556,7 @@ $x++ while $x < 12
556
556
557
557
Also see C < repeat/while > and C < repeat/until > below.
558
558
559
- = head2 repeat/while, repeat/until
559
+ = head2 X < repeat/while, repeat/until|control flow,repeat >
560
560
561
561
Perl 5 allows one to apply a statement modifier to a C < do > block such that
562
562
C-like constructs such as
@@ -580,11 +580,7 @@ This can also be written quite naturally with C<until>:
580
580
...
581
581
} until $x >= 10;
582
582
583
- = head2 while
584
-
585
- = comment TODO
586
-
587
- = head2 return
583
+ = head2 X < return|control flow >
588
584
589
585
= comment TODO
590
586
@@ -602,7 +598,7 @@ LINE: for $*IN.lines -> $line {
602
598
603
599
= end code
604
600
605
- = head2 next
601
+ = head2 X < next|control flow >
606
602
607
603
The C < next > command starts the next iteration of the loop. So the code
608
604
@@ -618,7 +614,7 @@ my @x = 1, 2, 3, 4, 5;
618
614
619
615
prints "1245".
620
616
621
- = head2 last
617
+ = head2 X < last|control flow >
622
618
623
619
The C < last > command immediately exits the loop in question.
624
620
@@ -634,7 +630,7 @@ for @x -> $x {
634
630
635
631
prints "12".
636
632
637
- = head2 redo
633
+ = head2 X < redo|control flow >
638
634
639
635
The C < redo > command restarts the loop block without evaluating the
640
636
conditional again.
@@ -645,7 +641,7 @@ conditional again.
645
641
646
642
= end code
647
643
648
- = head2 goto
644
+ = head2 X < goto|control flow >
649
645
650
646
= comment TODO
651
647
0 commit comments