develooper Front page | perl.perl5.porters | Postings from March 2014

Re: [perl #121501] Empty code block {} causes syntax error (misparsedas anonymous hash constructor?) before 'sub'

Thread Previous | Thread Next
From:
Eric Brine
Date:
March 25, 2014 15:35
Subject:
Re: [perl #121501] Empty code block {} causes syntax error (misparsedas anonymous hash constructor?) before 'sub'
Message ID:
CALJW-qFdiQLms6Gyf5_=mN7_fpSijZkFtyCeNQ-ginmut9n53w@mail.gmail.com
On Tue, Mar 25, 2014 at 11:31 AM, Eric Brine <ikegami@adaelis.com> wrote:

> On Tue, Mar 25, 2014 at 10:48 AM, Ed Avis <eda@waniasset.com> wrote:
>
>> Hmm, you are right, C<{} my $x;> is also an error.
>>
>
> Also, C<< { } { } >>.
>
>
>> I think this is a bit fishy; in other C-like languages an empty block can
>> appear in any
>> place where a nonempty one can.  Could that become true for Perl too?
>>
>
> You seem to forget that { } is also the hash construction operator. { } is
> far more likely to be an empty hash constructor than an empty block. You
> can disambiguate by placing a ";" at the start of the block (C<< {; } >>).
> In the original scenario, you could avoid the problem by commenting the
> block by adding C<< if (0) >>.
>
> Perl could check if the "{ }" is at the start of statement, but any
> statement can be an expression.
>

What I meant:
Perl could check if the "{ }" is at the start of statement and make it a
block then, but that would break code because any statement can be the
start of an expression. e.g. C<< sub f {  {}  } >>.


> Check check for void context since context isn't know yet.
>

What I meant:
Perl can't check for void context since context isn't know yet.


Sounds like something that can't be "fixed".

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About