所有Perl 6引用构造都有术语优先权吗?

bri*_*foy 9 operator-precedence perl6 raku

< >有长期的优先级.以下是文档中示例:

say <a b c>[1];
Run Code Online (Sandbox Code Playgroud)

我认为相同的优先级适用于所有引用运算符.这有效:

my $string = '5+8i';
my $number = <<$string>>;
say $number;
Run Code Online (Sandbox Code Playgroud)

这会插入$string并创建allomorphes(在本例中为ComplexStr):

(5+8i)
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试将其编入索引,就像文档中的示例一样,它不会编译:

my $string = '5+8i';
my $number = <<$string>>[0];
say $number;
Run Code Online (Sandbox Code Playgroud)

我不太确定Perl 6在这里发生了什么.也许它认为这是一个超级高手:

===SORRY!=== Error while compiling ...
Cannot use variable $number in declaration to initialize itself
at /Users/brian/Desktop/scratch.pl:6
------>     say $?number;
    expecting any of:
        statement end
        statement modifier
        statement modifier loop
    term
Run Code Online (Sandbox Code Playgroud)

我可以跳过变量:

my $string = '5+8i';
say <<$string>>[0];
Run Code Online (Sandbox Code Playgroud)

但这是一个不同的错误,无法找到收尾报价:

===SORRY!=== Error while compiling ...
Unable to parse expression in shell-quote words; couldn't find final '>>'
at /Users/brian/Desktop/scratch.pl:8
------> <BOL>?<EOL>
    expecting any of:
        statement end
        statement modifier
        statement modifier loop
Run Code Online (Sandbox Code Playgroud)

Eli*_*sen 7

我认为这保证了rakudobug电子邮件.我认为解析器在尝试将其解释为hyper(又名>>.method)时感到困惑.以下解决方法似乎证实了这一点:

my $string = '5+8i';
my $number = <<$string >>[0];  # note space before >>
say $number;
Run Code Online (Sandbox Code Playgroud)

为了满足你的强迫症,你可能还要先放一个空格$string.

是的,在Perl 6中,空格并非毫无意义.


bri*_*foy 7

Jonathan回答了RT#131695的答案.

>>[]是一个用于索引列表的后缀运算符,因此它会尝试使用它.这是预期的行为.很公平,虽然我认为解析器对于常规代码猴来说有点太聪明了.

  • 解析器并不聪明,因为它非常有文字意识.在某些时候,qq插值变量的规则变成了非常类似于"插入任何sigilled变量,加上可能跟随的任何后缀运算符,由postcircumfix终止".方便的时候你想做"$ foo.uc()"或其他什么.但也是思想家的来源.这最常引用我的HTML字符串,如"$ stuff <tag> ... </ tag>",其中"<tag>部分最终被解析为postcircumfix. (3认同)