class Label

Tagged location in the source code

class Label {}

In Perl 6, you can give for example loops a label, and use it to control that loop (instead of the inner-most loop).

USERS:          # the label 
for @users -> $u {
    for $u.pets -> $pet {
        # usage of a label 
        next USERS if $pet.barks;
    }
    say "None of {$u}'s pets barks";
}
say USERS.^name;        # OUTPUT: «Label␤» 

Those label are objects of type Label.

Methods

method next

Defined as:

method next(Label:)

Begin the next iteration of the loop associated with the label.

MY-LABEL:
for 1..10 {
    next MY-LABEL if $_ < 5;
    print "$_ ";
}
 
# OUTPUT: «5 6 7 8 9 10 » 

method redo

Defined as:

method redo(Label:)

Repeat the same iteration of the loop associated with the label.

my $has-repeated = False;
 
MY-LABEL:
for 1..10 {
    print "$_ ";
    if $_ == 5 {
        LEAVE $has-repeated = True;
        redo MY-LABEL unless $has-repeated;
    }
}
 
# OUTPUT: «1 2 3 4 5 5 6 7 8 9 10 » 

method last

Defined as:

method last(Label:)

Terminate the execution of the loop associated with the label.

MY-LABEL:
for 1..10 {
    last MY-LABEL if $_ > 5;
    print "$_ ";
}
 
# OUTPUT: «1 2 3 4 5 » 

Type Graph

Type relations for Label
perl6-type-graph Label Label Any Any Label->Any Mu Mu Any->Mu

Stand-alone image: vector