我想提取一個正則表達式的名字,但~~似乎想分配給一個不可變的容器。爲什麼這樣?我做錯了什麼?爲什麼perl6正則表達式~~試圖分配給不可變容器?
my $test= ' "DOE , JOHN" ';
grammar findReplace {
regex TOP { \s* <ptName> \s* }
regex ptName { <aName> }
regex aName { \" .+? \" }
}
class rsAct {
method TOP ($/) { make "last name is: " ~ $<ptName>.made; }
method ptName ($/) {
my $nameStr = $/.Str;
if $nameStr ~~ m/ \" (<alpha>+) .* \, .* \"/{
my $lastName = $/[0]; # I want $/[0] sub-string of outer $/
make $lastName;
}
}
}
my $m = findReplace.parse($test, actions => rsAct.new);
say $m.made;
,我得到的錯誤是這樣的:
Cannot assign to a readonly variable or a value
in method ptName at shit.pl line 13
in regex ptName at shit.pl line 5
in regex TOP at shit.pl line 4
in block <unit> at shit.pl line 20
我試圖讓外$ /匹配模式的子串;爲什麼會成爲一項任務?
謝謝你的幫助!
你應該在大部分時間使用'token'或'rule',而不是'regex' –