Why is this Perl 6 feed operator a “bogus statement”?

前端 未结 1 1844
臣服心动
臣服心动 2021-01-12 05:50

I took this example from Day 10 – Feed operators of the Perl 6 2010 Advent Calendar with the slight change of .uc for the .ucfirst that\'s no longe

相关标签:
1条回答
  • 2021-01-12 05:55

    From the Perl 6 documentation on Separating Statements:

    A closing curly brace followed by a newline character implies a statement separator

    In other words, whenever the closing brace of a block is the last thing in a line (not counting whitespace and comments), then the parser implicitly inserts a semicolon after it.

    This allows us to write things like the following without a semicolon after the }:

    my @foo = @bar.map: {
        ...
    }
    

    The semicolon would be ugly there, and if you had first written the loop as for @bar { ... } and then decided to turn it into a map with assignment like this, adding the trailing semicolon would be annoying and easy to forget. So for the most part, this automatic statement termination after end-of-line blocks is helpful.

    However, feed operators feeding into blocks are one case (possibly the only one) where it trips people up.

    To prevent it from happening, insert \ (a.k.a. unspace) after the block, as you've already noted. The unspace makes the whitespace which includes the newline invisible to the parser, and thus the aforementioned newline-based parser rule won't be applied.

    0 讨论(0)
提交回复
热议问题