【问题标题】:What space is significant in Perl 6?Perl 6 中什么空间是重要的?
【发布时间】:2017-08-02 02:10:15
【问题描述】:

Perl 6 在其前身中清理了一些奇怪的情况,在某些地方不允许空格,但也在其他地方做工作。空间在哪里重要?有一个完整的参考会很好,因此我添加了一个社区 wiki 答案,我将使用您的重复收入答案进行更新。示例赞赏!

但是,还要记住 Perl 6 有 unspace,因此您可以使用 \ 使空白有效地不可见。例如,您不应该将子例程名称及其参数列表放空,但使用 unspace 您可以:

sub-name    ( @arguments );  # not okay
sub-name\   ( @arguments );  # okay

【问题讨论】:

标签: whitespace raku


【解决方案1】:

空格可防止各种配对符号(例如{}()<>)被解释为后缀运算符

散列结构

> my %a = (A=>1, B=>2, C=>3);
{A => 1, B => 2, C => 3}
> %a<A>;
1
> %a <A>;
===SORRY!=== Error while compiling:
Missing required term after infix
------> %a <A>⏏;
    expecting any of:
        prefix
        term
> %a\ <A>;
1

同样,插值被空格破坏,我不知道在插值环境中使用 unspace 的方法。但是在使用{} 插入代码结果时,您可以在打开{ 后引入空格:

> put "%a<A>";
1
> put "%a <A>";
%a <A>
> put "%a\ <A>";
%a <A>
> put "%a {'A'}"
%a A
> put "%a{<A>}"
1
> put "%a{ <A> }"
1

循环块

此循环在 Perl 5 中有效,但在 Perl 6 中无效:

for (1,2,3){
    print "counting $_!\n";
}
===SORRY!=== Error while compiling testing.p6
Missing block (whitespace needed before curlies taken as a hash subscript?)
at testing.p6:4
------> <BOL>⏏<EOL>
    expecting any of:
        block or pointy block

但右括号后的空格更正了这一点:

for (1,2,3) {
    print "counting $_!\n";
}
counting 1!
counting 2!
counting 3!

这一直困扰着我,直到我了解到控制流构造的条件通常不需要括号(例如 forwhileif 构造)。

为避免这种情况,只需省略括号:

for 1,2,3 {
    print "counting $_!\n";
}

但是你还是要在大括号前留出空格:

for 1,2,3{
    print "counting $_!\n";
}
===SORRY!=== Error while compiling testing.p6
Missing block (whitespace needed before curlies taken as a hash subscript?)
at testing.p6:4
------> <BOL>⏏<EOL>
    expecting any of:
        block or pointy block

【讨论】:

    【解决方案2】:

    空格和字符串

    Heredocs 要求正确处理间距

    结束模式的位置(这里是END)决定了这个heredoc中什么空间是重要的:

    my $letter = q:to/END/;
        Foo, bar, baz
          <- The preceding two spaces are included in the heredoc.
        END
    
    put $letter;
    

    当运行时给我们:

    Foo, bar, baz
      <- The preceding two spaces are included in the heredoc.
    

    目前不缩进END,会导致heredoc中出现更多空白:

    my $letter = q:to/END/;
        Foo, bar, baz
          <- The preceding four spaces are included in the heredoc.
      END
    
    put $letter;
    
      Foo, bar, baz
        <- The preceding four spaces are included in the heredoc.
    

    带引号的字符串中的空格很重要(当然)

    my $greeting = " Hello World! ";
    my $salutation = "Hello World!";
    
    put $greeting;
    put $salutation;
    
     Hello World!
    Hello World!
    

    如果分隔符为()'',则literal quoting with Q 需要空格。

    以下示例来自https://docs.perl6.org/language/quoting#Literal_strings:_Q

    Q'this will not work!'
    Q(this won't work either!)
    Q (this is fine, because of space after Q)
    Q 'and so is this'
    

    但其他分隔符可以带或不带空格:

    Q^This works!^
    Q ^so does this^
    

    【讨论】:

      【解决方案3】:

      行尾的一些右花括号(以垂直空格结尾的空格)表示语句分隔的分号:

      来自Why is this Perl 6 feed operator a “bogus statement”? 我有这个例子,其中 grep 的 } 将是语句的结尾,因为隐含的分号:

      my @rakudo-people = <scott patrick carl moritz jonathan jerry stephen>;
      @rakudo-people
          ==> grep { /at/ } \
          ==> map { .uc } ==> my @who-it's-at;
      say ~@who-it's-at;
      

      归约运算符不允许有空格

      我忘了我在哪里找到这个例子

      my @a = [[<foo>],]; # Is that a reduction?
      
      my @a = [[ <foo>],]; # That space certainly means it's not one?
      

      后缀运算符前没有空格

      $hash <key>; # Error, indexing uses postfix operators
      $hash<key>; # Works
      
      call-this-subroutine    ( @arguments );  # Error
      call-this-subroutine( @arguments ); # Works
      
      my $x = 5;
      say $x ++;  # Error
      say $x++;   # Works
      

      这对前缀运算符来说不是问题:

      my $x = 5;
      say ++ $x;  # Works
      say ++$x;   # Works
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-12
        • 1970-01-01
        • 1970-01-01
        • 2017-12-29
        • 1970-01-01
        • 2018-08-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多