我写了一个测试程序,现在看来如果我不使用$/
方法签名,因为我必须在方法中使用 .match,所以我不能再做任何事情了。我做错了什么?
另一个问题是,如果.match
设置$/
,并且$/
是只读的,那么我不能在包含.match
语句的方法的签名中$/
,并且我不能在方法中有多个.match
,因为每个.match
都会尝试设置只读$/
。这将非常尴尬地编程。
下面是一个测试程序,里面只有一个.match
语句和结果:
测试程序:
grammar test {
regex TOP { <foo><bar> }
regex foo { :i s* foo s* }
regex bar { :i s bar s* }
}
class actTest {
method foo ($x) { # program fails if I use $/ in signature
print "1 "; say $x; # how to combine the 2 and show $x as match?
print "2 "; say $x.WHAT;
my $newStr = $x.Str;
print "3 "; say $newStr;
my $newMatch
= $newStr.match(/:i(f)(oo)/); # adverb cannot be outside?
print "4 "; say $newMatch.WHAT;
print "5 "; say $newMatch;
print "6 "; say $/;
my $oo = $newMatch[1].Str;
print "10 "; say $oo;
my $f = $newMatch[0].Str;
print "11 "; say $f;
my $result = $oo ~ $f;
print "12 "; say $result;
make $result; # now I cannot make anything; huh???
}
method TOP ($/) {
print "8 "; say $<bar>;
print "9 "; say $<foo>.made; # failed, method 'foo' makes nothing
make $<bar> ~ $<foo>.made;
}
}
my $m = test.parse("Foo bar", actions => actTest.new);
print "7 "; say $m;
结果是:
1 「Foo 」
2 (Match)
3 Foo
4 (Match)
5 「Foo」
0 => 「F」
1 => 「oo」
6 「Foo」
0 => 「F」
1 => 「oo」
10 oo
11 F
12 ooF
1 「Foo」
2 (Match)
3 Foo
4 (Match)
5 「Foo」
0 => 「F」
1 => 「oo」
6 「Foo」
0 => 「F」
1 => 「oo」
10 oo
11 F
12 ooF
8 「 bar」
9 (Any)
Use of uninitialized value of type Any in string context.
Methods .^name, .perl, .gist, or .say can be used to stringify it to
something meaningful.
in method TOP at matchTest.pl line 28
7 「Foo bar」
foo => 「Foo」
bar => 「 bar」
1)如何在没有$/
的情况下make
make ...
只是$/.make(...)
的捷径。
如果要影响与存储在$/
中的对象不同的Match
对象,则必须直接使用方法形式,即在您的情况下$x.make($result)
。
2)$/
何时以及为何是只读的
默认情况下,$/
绑定到一个普通的项目容器(如用my
声明的变量),即不是只读的。因此,在例程中多次使用.match
方法应该没有任何问题。
只有当您在例程签名中显式声明$/
参数时,$/
才会直接绑定到传递给该例程的Match
对象(而不是包装在项目容器中),因此在例程中是只读的 - 因为这就是普通签名绑定的工作方式。
您可以使用is copy
特征覆盖正常的只读参数绑定,并强制$/
成为例程中的可变项容器:
method foo ($/ is copy) { ... }
这样,在例程中使用.match
就可以了,并将新的Match
对象存储在$/
中。但是,您将无法再访问传递给例程的原始Match
对象,因此无法使用make
影响它。因此,对于需要使用.match
的操作方法,像您那样使用自定义参数名称是要走的路。