perl6语法操作:如果不使用$/,则无法进行任何操作 [英] perl6 grammar actions: unable to make anything if not using $/

查看:88
本文介绍了perl6语法操作:如果不使用$/,则无法进行任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个测试程序,现在看来,如果我不使用 $/方法签名,因为我必须在方法内部使用.match,因此我无法再做任何事情了.我做错了什么?

I wrote a test program, and now it seems that if I don't use $/ in a method signature because I have to use .match inside the method, I can no long make anything. What did I do wrong?

另一个问题是,如果 .match 设置 $/,并且 $/是只读的,那么我就不能拥有 .match 语句的方法签名中的> $/,并且由于每个 .match 将尝试设置只读的 $/.编程将很尴尬.

A further question is that if .match sets $/, and $/ is read-only, then I cannot have $/ in the signature of a method that contains a .match statement, and I cannot have more than one .match inside the method because each .match will try to set the read-only $/. This will be very awkward to program.

这是测试程序,其中仅包含一个 .match 语句,其结果为:

Here is the test program with only one .match statement inside and the results:

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).

If you want to affect a different Match object than the one stored in $/, you have to use the method form directly, i.e. in your case $x.make($result).

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

By default, $/ is bound to a normal item container (like a variable declared with my), i.e. not read-only. So there shouldn't be any problem with using the .match method multiple times in a routine.

仅当您在例程签名中显式声明 $/作为参数时, $/将直接绑定到 Match 传递给该例程的对象(不包装在项目容器中),因此在例程中将是只读的-因为这就是常规签名绑定的工作方式.

It's only when you explicitly declare $/ as a parameter in a routine signature, that $/ will be bound directly to the Match object passed to that routine (not wrapped in an item container), and will thus be read-only inside the routine – because that's how normal signature binding works.

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

You could use the is copy trait to override the normal read-only parameter binding, and force $/ to be a mutable item container inside the routine:

method foo ($/ is copy) { ... }

这样,可以在例程中使用 .match ,并且可以在 $/中存储新的 Match 对象.但是然后您将无法再访问传递给例程的原始对象,因此无法通过 make 对其进行影响.因此,对于需要使用 .match 的操作方法,使用像您一样的自定义参数名称是可行的方法.

This way, using .match inside the routine would work, and would store a new Match object in $/. But then you wouldn't have access to the original Match object passed to the routine anymore, and thus couldn't affect it with make. So for an action method that needs to use .match, using a custom parameter name like you did is the way to go.

这篇关于perl6语法操作:如果不使用$/,则无法进行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆