I have this code that works as expected:
my @words = 'foo', 'bar';
my $text = 'barfoo';
for @words -> $to-regex {
$text ~~ m/ ($to-regex) {say "matched $0"}/;
}
It prints:
matched foo
matched bar
However, if I try to use topic variable on the for loop, as in:
for @words { # implicit "-> $_", AFAIK
$text ~~ m/ ($_) {say "matched $0"}/;
}
I get this:
matched barfoo
matched barfoo
Same results using postfix for:
$text ~~ m/ ($_) {say "matched $0"}/ for @words; # implicit "-> $_", AFAIK
Is this a special case of the topic variable inside a regex?
Is it supposed to hold the whole string it is matching against?
The smart-match operator has 3 stages
$_
.ACCEPTS($_)
on that resultSo it isn't a special case for a regex, it is how ~~
always works.
for 1,2,3 {
$_.print;
'abc' ~~ $_.say
}
# 1abc
# 2abc
# 3abc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With