概要05提到Perl 6不会将变量插入到正则表达式中,但您可以将外部变量与模式相关联.据我所知,文档没有提到这个功能.我认为人们仍然希望以某种方式从字符串中建立一个模式,所以我很好奇这是如何工作的.
这是一个演示现在发生的事情的程序.我不知道这是应该发生的事情还是任何人的意图.我将一个变量插入一个模式.如果你看一下$r有.perl,你看变量名.然后,我应用模式并匹配.我改变了变量的值.现在模式不匹配.将其更改为可行的其他内容,并再次匹配:
my $target = 'abcdef';
my $n = 'abc';
my $r = rx/ ( <$n> ) /;
# the smart match like this doesn't return a Match object
# https://rt.perl.org/Ticket/Display.html?id=126969
put 'rx// directly: ',
$target ~~ $r
?? "Matched $0" !! 'Misssed';
# now, change $n. The same $r won't match.
$n = 'xyz';
put 'rx// directly: ',
$target ~~ $r
?? "Matched $0" !! 'Misssed';
# now, change back $n. The same $r …Run Code Online (Sandbox Code Playgroud)