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:32
Subject:
Re: [perl #121501] Empty code block {} causes syntax error (misparsedas anonymous hash constructor?) before 'sub'
Message ID:
CALJW-qEhNnG6uEcBOQduHuiHSaqwpT1qEgFxKMk15zm8ysCKcw@mail.gmail.com
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. Check check for void context since context
isn't know yet.

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