7

Perl 6 cleans up some odd cases in its predecessor by not allowing spaces in some places but also doing jobs in other places. Where does space matter? It would be nice to have a complete reference and as such I've added a community wiki answer that I'll update with your rep-earning answers. Examples appreciated!

But, also remember that Perl 6 has unspace, so you can use a \ to make whitespace effectively invisible. For example, you aren't supposed to air out the subroutine name and it's argument list, but with unspace you can:

sub-name    ( @arguments );  # not okay
sub-name\   ( @arguments );  # okay
brian d foy
  • 129,424
  • 31
  • 207
  • 592

3 Answers3

2

Some closing curlies at the end of a line (whitespace ending in vertical whitespace) implies a statement-separating semicolon:

From Why is this Perl 6 feed operator a “bogus statement”? I have this example where the } for the grep would be the end of statement because of the implied semicolon:

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;

Reduction operators don't allow spaces

I forgot where I found this example

my @a = [[<foo>],]; # Is that a reduction?

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

No space before postfix operators

$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

This isn't a problem for prefix operators:

my $x = 5;
say ++ $x;  # Works
say ++$x;   # Works
brian d foy
  • 129,424
  • 31
  • 207
  • 592
2

Whitespace prevents various paired symbols (e.g. {}, (), <>) from being interpreted as postfix operators

Hash constructs

> 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

Likewise, interpolating is broken by spaces and I don't know of a way to use unspace in an interpolating environment. But you can introduce space after an opening { when using {} to interpolate the result of code:

> 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

loop blocks

This loop works in Perl 5, but not in 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

But a space after the closing parentheses corrects this:

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

This used to catch me all the time, until I learned to that the parentheses are usually not needed for the conditional of control flow constructs (e.g. for, while, and if constructs).

To avoid this, simply leave out the parentheses:

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

But you still have to leave space before the brace:

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
Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
2

Whitespace and strings

Heredocs require handling spacing correctly

The placement of the ending pattern (here END) determines what space is significant in this heredoc:

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

put $letter;

When run gives us:

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

Not indenting END as far, results in more whitespace in the 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.

Whitespace inside a quoted string is significant (of course)

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

put $greeting;
put $salutation;
 Hello World!
Hello World!

Whitespace is required for literal quoting with Q if the delimiters are () or ''.

Example below is from 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'

But other delimiters can be used with or without the space:

Q^This works!^
Q ^so does this^
Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99