Perl 6中有哪些重要空间?

时间:2017-08-02 02:10:15

标签: whitespace perl6

Perl 6通过在某些地方不允许空间而在其他地方做工作来清理其前身的一些奇怪案例。空间在哪里重要?有一个完整的参考资料会很高兴,因此我添加了一个社区wiki答案,我将根据您的回复答案进行更新。示例赞赏!

但是,还要记住Perl 6有unspace,因此您可以使用\使空白有效地不可见。例如,你不应该排除子程序名称和它的参数列表,但是如果没有空格,你可以:

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

3 个答案:

答案 0 :(得分:2)

一行结束时的一些结束曲线(以垂直空白结尾的空格)意味着一个语句分隔分号:

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

答案 1 :(得分:2)

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

哈希构造

> 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

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

> 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 :(得分: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^