Reputation: 575
I have something that I can do easily in Perl, but not in Raku without fiddling around with flag variables. Here's the Perl code:
#!/usr/bin/perl
MAIN_BLOCK: {
foreach $item (1 2 3 4 5) {
$item == 6 and last MAIN_BLOCK;
}
print "No items matched!\n";
}
The relevant difference here is that Perl will allow you to use last to exit from any labelled block. Raku will only do this if the block is a loop.
Is there a good way to do this? I feel like there should be a phaser for this, but haven't figured out how to do it without flag variables, which seem like they should be avoidable.
Thanks,
Upvotes: 7
Views: 200
Reputation: 9600
Raku supports similar control flow with given blocks.
Here's a fairly literal translation (i.e., not necessarily idiomatic Raku) from the Perl code you posted:
given * {
for ^6 -> $item {
succeed if $item == 6;
}
default { print "No items matched!\n"; }
}
edit: Oh, and for a less-literally-translated/more-idiomatic-Raku solution, well, TIMTOWTDI but I might go with return
ing from an anonymous sub
:
sub { for ^6 { return when 6 }
say "No items matched!" }()
(Of course, I suppose it's possible that the most Raku-ish way to solve do that doesn't involve any Raku syntax at all – but instead involves modifying one of Raku's braided languages to allow for
loops to take an else
block. But I'm not advising those sort of shenanigans!)
Upvotes: 6