> On Aug 1, 2019, at 10:49 PM, William Michels via perl6-users
> <perl6-us...@perl.org> wrote:
>
> Hi Richard, I'm trying to figure out when the parentheses in 'lines()'
> can be dropped, and 'lines' used instead. Any pointers?
—snip—
Parens will be required when `lines` is a sub call (as opposed `.lines` method
call), and you have `if` or `for` to the left of `lines`, and an opening curly
brace to the right of `lines`.
I think this is because Perl 6's single-pass parser sees a subroutine followed
by a block, and since blocks *can* be an argument to a block (as in `my @a =
map { $_ + 42 }, @b;` where `map` gets the two arguments of Codeblock and
Array), the `lines` sub *does* get the block as its first argument. That leaves
no block for the `if` or `for`, which is clearly an error, that gets reported
to you as *two* errors because the parser recognizes the probable cause:
Function 'lines' needs parens to avoid gobbling block
Missing block (apparently claimed by 'lines')
From "Perl 6 Fundamentals: A Primer with Examples, Projects, and Case Studies"
By Moritz Lenz
( and also from
https://perlgeek.de/blog-en/perl-6/2017-004-book-perl6-review.html )
One case worth noting is that if you call a subroutine without
arguments as the block of an if condition or a for loop (or similar
constructs), you have to include the parentheses, because otherwise the block
is parsed as an argument to the function.
sub random-choice() {
Bool.pick;
}
# right way:
if random-choice() {
say 'You were lucky.';
}
# wrong way:
if random-choice {
say 'You were lucky.';
}
If you do happen to make this mistake, the Perl 6 compiler tries very
hard to detect it. In the example above, it says
Function 'random-choice' needs parens to avoid gobbling block
and when it tries to parse the block for the if-statement, it doesn't
find one:
Missing block (apparently claimed by 'random-choice')
—
Hope this helps,
Bruce Gray (Util of PerlMonks)