Tech Tip: Loop Labels in Perl 6

The Tip

It is known that one can label loops (or arbitrary blocks) in Perl 5, using the syntax of MYLABEL: while (COND()) { BLOCK } or MYLABEL: for my $x (@array) { BLOCK } and then one can do last MYLABEL;, next MYLABEL;, or redo MYLABEL; (for more information see perldoc perlsyn). However, I was unable to find how to do something like that in Perl 6, despite some amount of searching. Luckily, the people on the Perl 6 helped me.

The answer is that in Perl 6, it's essentially not different:

MYLABEL: for (1 .. 100) -> $x
{
.
.
    last MYLABEL;
.
.
}

WHILE_LABEL: while $x < 24
{
.
.
    next WHILE_LABEL;
.
.
}

In Perl 6, one can also write MYLABEL.last; instead, but last MYLABEL; will still work. Note however that "last/redo/next" + label are not currently implemented in Rakudo Perl 6 (though you can still safely add labels to the loops).

Cheers!

Meta

I haven't blogged for a while in my blogs.perl.org blog, even though I've been meaning to. Anyway, I'm mostly fine, and have been working on some Project Euler challenges, some pull requests and patches to projects, and some activity on my own projects. Hopefully, I'll find the time to write something more substantial. Recently, I've contributed some extra examples to the Project Euler section of the perl6-examples repository. As a result, I was recently granted a perl6-GitHub-project-wide commit bit (by none other than TimToady), so now I can do all the MAYHEM and DESTRUCTION I want there (Muahahahahahahah!). Just kidding about that: all I'm planning to do for now is add more Perl 6 examples.

Leave a comment

About Shlomi Fish

user-pic An Israeli software developer, essayist, and writer, and an enthusiast of open/free software and cultural works. I've been working with Perl since 1996.