Skip to content

Commit f50a6a3

Browse files
committed
Adds definition of methods and examples refs #2624
1 parent 3c88a9b commit f50a6a3

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

doc/Type/Label.pod6

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
77
class Label {}
88
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.
1113
1214
=begin code :skip-test<compile time error>
1315
USERS: # the label
@@ -21,10 +23,43 @@ for @users -> $u {
2123
say USERS.^name; # OUTPUT: «Label␤»
2224
=end code
2325
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.
2546
2647
=head1 Methods
2748
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+
2863
=head2 method next
2964
3065
Defined as:

0 commit comments

Comments
 (0)