perl6 语法操作:如果不使用 $/则无法进行任何操作 [英] perl6 grammar actions: unable to make anything if not using $/
问题描述
我写了一个测试程序,现在看来如果不使用$/
方法签名因为我必须在方法内部使用 .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
因为每个 .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(...)
的快捷方式.
1) How to make
without $/
make ...
is just a shortcut for $/.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)
.
默认情况下,$/
绑定到一个普通的 item 容器(就像一个用 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
trait 来覆盖普通的只读参数绑定,并强制 $/
成为例程内的可变项目容器:>
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
对象.但是,您将无法再访问传递给例程的原始 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屋!