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
{}
, ()
, <>
) from being interpreted as postfix operators> 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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With