perl6语法动作:如果不使用$ /则无法做任何事情

lis*_*tor 6 readonly signature match perl6 assign

我写了一个测试程序,现在似乎如果我不使用$/方法签名因为我必须在方法中使用.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;
Run Code Online (Sandbox Code Playgroud)

结果如下:

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?
Run Code Online (Sandbox Code Playgroud)

sml*_*mls 6

1)如何make没有$/

make ...只是一个捷径$/.make(...).

如果要影响Match与存储的对象不同的对象$/,则必须直接使用方法表单,即在您的情况下$x.make($result).

2)何时以及为什么$/是只读的

默认情况下,$/绑定到正常的项容器(如声明的变量my),即不是只读的.因此,.match在例程中多次使用该方法应该没有任何问题.

只有当您$/在例程签名中明确声明为参数时,它$/才会直接绑定到Match传递给该例程的对象(未包装在项容器中),因此在例程中是只读的 - 因为这是正常的签名绑定工作.

您可以使用is copy特征覆盖正常的只读参数绑定,并强制$/成为例程中的可变项容器:

method foo ($/ is copy) { ... }
Run Code Online (Sandbox Code Playgroud)

这样,.match在例程内部使用将起作用,并将存储新Match对象$/.但是,您将无法再访问Match传递给例程的原始对象,因此无法对其进行影响make.因此,对于需要使用的操作方法.match,使用自己喜欢的自定义参数名称是可行的方法.