File tree Expand file tree Collapse file tree 1 file changed +38
-3
lines changed Expand file tree Collapse file tree 1 file changed +38
-3
lines changed Original file line number Diff line number Diff line change 6
6
7
7
class Label {}
8
8
9
- In Perl 6, you can give for example loops a label, and use it to control that
10
- loop (instead of the inner-most loop).
9
+ Labels are used in Perl 6 to tag loops so that you can specify the one you want
10
+ to jump to with statements such as C < last > . You can use it to jump out of loops
11
+ and get to outer ones, instead of just exiting the current loop or going to the
12
+ statement before.
11
13
12
14
= begin code :skip-test<compile time error>
13
15
USERS: # the label
@@ -21,10 +23,43 @@ for @users -> $u {
21
23
say USERS.^name; # OUTPUT: «Label»
22
24
= end code
23
25
24
- Those label are objects of type C < Label > .
26
+ Those label are objects of type C < Label > , as shown in the last statement. Labels
27
+ can be used in any loop construct, as long as they appear right before the loop
28
+ statement.
29
+
30
+ = begin code
31
+ my $x = 0;
32
+ my $y = 0;
33
+ my $t = '';
34
+ A: while $x++ < 2 {
35
+ $t ~= "A$x";
36
+ B: while $y++ < 2 {
37
+ $t ~= "B$y";
38
+ redo A if $y++ == 1;
39
+ last A
40
+ }
41
+ }
42
+ say $t; # OUTPUT: «A1B1A1A2»
43
+ = end code
44
+
45
+ Putting them on the line before the loop or the same line is optional.
25
46
26
47
= head1 Methods
27
48
49
+ = head2 method name
50
+
51
+ Defined as:
52
+
53
+ method name()
54
+
55
+ Not terribly useful, returns the name of the defined label:
56
+
57
+ A: while True {
58
+ say A.name; # OUTPUT: «A»
59
+ last A;
60
+ }
61
+
62
+
28
63
= head2 method next
29
64
30
65
Defined as:
You can’t perform that action at this time.
0 commit comments