| Store | Cart

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

From: Father Chrysostomos via RT <perl...@perl.org>
Sun, 19 Oct 2014 17:09:29 -0700
On Sun Oct 19 16:28:18 2014, jkeenan wrote:
> On Tue Mar 25 04:23:54 2014, e...@waniasset.com wrote:> > This is a bug report for perl from e...@waniasset.com,> > generated with the help of perlbug 1.39 running under perl 5.18.2.> >> >> > -----------------------------------------------------------------> > [Please describe your issue here]> >> > An empty code BLOCK, as {}, causes an error immediately before> > a subroutine definition.> >> > % perl -E 'sub foo {}'> > % perl -E '{} sub foo {}'> > syntax error at -e line 1, near "} sub foo "> > Execution of -e aborted due to compilation errors.> >> > This is a nuisance, because it gets in the way of commenting out> > code.  Suppose you start off with the following bit of code> >> > {> >     my $temp = foo();> >     ++$temp;> >     say $temp;> > }> > sub bar { say 'bar' }> >> > and decide to comment out the lines of code in the BLOCK;> > then you get a mysterious syntax error.> >> > I understand that the Perl parser often has to make a judgement> > about whether braces are a BLOCK or an anonymous hash constructor.> > In this case, it is making the wrong call.  While it is never> > going to be able to guess perfectly in all cases, I suggest that> > this particular parser snafu be fixed.  It would not break> > compatibility to do so, because currently it is a syntax error.> >> > This ticket has had no further correspondence in six months.> Reviewing the ticket this evening, it appears that the OP's original> motion for new functionality, namely:> > > Please allow an empty BLOCK to occur before a subroutine> > definition, just as an empty BLOCK can occur elsewhere in a program.> >> > ... did not get a second.> > I recommend that we close this ticket.

Indeed, this is not a bug.  (I had not noticed the ticket till now.)  Empty blocks with nothing in them are not just allowed everywhere.  In many cases {} is actually a hash constructor, not a block.  That is why ‘{} sub’ (and ‘{} pretty_much_anything’) fails.  ‘{;}’ would work.

-- 

Father Chrysostomos


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=121501

Recent Messages in this Thread
James E Keenan via RT Oct 19, 2014 11:28 pm
James E Keenan via RT Oct 20, 2014 10:45 pm
Father Chrysostomos via RT Oct 20, 2014 12:09 am
Ed Avis via RT Oct 20, 2014 02:32 pm
Messages in this thread