Labels are used in Raku to tag loops so that you can specify the specific one you want to jump to with statements such as last
. You can use it to jump out of loops and get to outer ones, instead of just exiting the current loop or going to the previous statement.
USERS: # the labelfor ->say USERS.^name; # OUTPUT: «Label»
Those labels are objects of type Label
, as shown in the last statement. Labels can be used in any loop construct, as long as they appear right before the loop statement.
my = 0;my = 0;my = '';A: while ++ < 2say ; # OUTPUT: «A1B1A1A2»
Putting them on the line before the loop or the same line is optional. Label
s must follow the syntax of ordinary identifiers, although traditionally we will use the latin alphabet in uppercase so that they stand out in the source. You can use, however, other alphabets like here:
駱駝道: while True# OUTPUT: «駱駝道»
Methods§
method name§
Not terribly useful, returns the name of the defined label:
A: while True
method file§
Returns the file the label is defined in.
method line§
Returns the line where the label has been defined.
method Str§
Converts to a string including the name, file and line it's been defined in.
method next§
method next(Label:)
Begin the next iteration of the loop associated with the label.
MY-LABEL:for 1..10# OUTPUT: «5 6 7 8 9 10 »
method redo§
method redo(Label:)
Repeat the same iteration of the loop associated with the label.
my = False;MY-LABEL:for 1..10# OUTPUT: «1 2 3 4 5 5 6 7 8 9 10 »
method last§
method last(Label:)
Terminate the execution of the loop associated with the label.
MY-LABEL:for 1..10# OUTPUT: «1 2 3 4 5 »