【发布时间】:2018-04-22 08:09:22
【问题描述】:
我才刚开始学习课程,所以我不了解基础知识。
我想要一个使用对象属性构造regex的方法:
class TEST {
has Str $.str;
method reg {
return
rx/
<<
<[abc]> *
$!str
<!before foo>
/;
}
}
my $var = TEST.new(str => 'baz');
say $var.reg;
在尝试运行此程序时,我收到以下错误消息:
===SORRY!=== Error while compiling /home/evb/Desktop/p6/e.p6
Attribute $!str not available inside of a regex, since regexes are methods on Cursor.
Consider storing the attribute in a lexical, and using that in the regex.
at /home/evb/Desktop/p6/e.p6:11
------> <!before foo>⏏<EOL>
expecting any of:
infix stopper
那么,正确的做法是什么?
【问题讨论】:
-
您可能需要使用词法变量构建闭包。请参阅Method returning a regex in Perl 6? 了解更多信息
-
@HåkonHægland 谢谢。我宁愿使用
EVAL。 :) -
如果错误信息不清楚... 正则表达式是一种方法。像上面那样声明正则表达式相当于尝试将匿名方法添加到
Match对象。 (提及Cursor已过时——Cursor=:=Match)。所以$!str指的是Match中的一个属性——但Match没有属性。所以你得到一个编译时失败。
标签: class raku private-members