2017-07-02 96 views
9

< >具有期限優先權。這裏的example from the docs是否所有Perl 6引用構造都具有期限優先權?

say <a b c>[1]; 

我想通了相同的優先級將適用於所有的報價運營商。這工作:

my $string = '5+8i'; 
my $number = <<$string>>; 
say $number; 

這插值$string並創建allomorphes(在這種情況下ComplexStr):

(5+8i) 

但是,如果我嘗試爲它像是從文檔的例子,它不編譯:

my $string = '5+8i'; 
my $number = <<$string>>[0]; 
say $number; 

我不太清楚Perl 6認爲這裏發生了什麼。也許是認爲它是一個hyperoperator:

===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 

我可以跳過變量:

my $string = '5+8i'; 
say <<$string>>[0]; 

但是,這是一個不同的錯誤不能找到收盤行情:

===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 
+1

另一個數據點:'<<"$string">> [0]'按預期工作 – Christoph

+0

確實。 '''只是另一種防止解析器進入'>>。'死衚衕的方法。 –

回答

7

我想這保證了一個rakudobug電子郵件。我認爲解析器會混淆試圖將其解釋爲hyper(又名>>.method)。以下解決方法似乎證實了這一點:

my $string = '5+8i'; 
my $number = <<$string >>[0]; # note space before >> 
say $number; 

爲了滿足你的強迫症,你很可能也$string前加一個空格。

是的,空白是不是在Perl意義6.

+0

https://rt.perl.org/Public/Bug/Display.html?id=131695 –

6

喬納森具有響應RT #131695答案。

>>[]是一個postfix operator索引列表,所以它試圖使用它。這是預期的行爲。雖然我認爲解析器對於普通代碼猴來說太聰明瞭,但是這還算公平。

+3

解析器並不像聰明的頭腦一樣聰明,在某些時候,變量的qq插值規則變成了「插入任何變量的變量,加上任何可能跟隨的後綴操作符,被postcircumfix終止」 。當你想做「$ foo.uc()」或者其他任何的時候方便,但也是thinkos的一個源代碼,這通常會在HTML字符串中出現,比如「$ stuff ...」,其中「」部分結束被解析爲postcircumfix。 –

相關問題