我有这个按预期工作的代码:
my @words = 'foo', 'bar';
my $text = 'barfoo';
for @words -> $to-regex {
$text ~~ m/ ($to-regex) {say "matched $0"}/;
}
它打印:
matched foo
matched bar
但是,如果我尝试在 for 循环中使用主题变量,如下所示:
for @words { # implicit "-> $_", AFAIK
$text ~~ m/ ($_) {say "matched $0"}/;
}
我明白了:
matched barfoo
matched barfoo
使用后缀的结果相同:
$text ~~ m/ ($_) {say "matched $0"}/ for @words; # implicit "-> $_", AFAIK
这是正则表达式中主题变量的特例吗?
它是否应该保存与之匹配的整个字符串?