Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Is { } an empty block or a bug in perl?

by cog (Parson)
on Jul 06, 2005 at 13:37 UTC ( [id://472794]=perlmeditation: print w/replies, xml ) Need Help??

If you come around something like

{}

you might think it is an empty block, but it isn't.

Try the following:

perl -e '{};{}' # works perl -e '{1}{}' # works perl -e '{}{1}' # works perl -e '{}{}' # doesn't work

Why? Because {} can be meant as an empty block or as an hashref, and to perl, those are two hashrefs.

perl apparently "uses fuzzy logic to guess whether you meant a {} to be a block or a hash-ref. It assigns points based on the surrounding code and the stuff inside the {} and then picks based on the resulting score" (this information courtesy of samtregar).

To see what happens for yourself, try this

perl -MO=Terse -e '{};{}'

This {} thing is actually a bug in perl. After all, you give it well written code and it complains about a syntax error.

Replies are listed 'Best First'.
Re: Is { } an empty block or a bug in perl?
by Joost (Canon) on Jul 06, 2005 at 13:51 UTC
      When you use empty hashrefs, are they by themselves, or are they at least assigned to something or used as an arguement to a subroutine? I cannot think of anytime that I've just put an empty structure dangling in the wind. If you do, could you provide an example? I'm genuinely curious.

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come

      I would argue that it is not well written code.

      This website is frequently the meeting place for showdowns about "show-off perl" vs "safe perl" programming.

      I often write an empty block as follows:

      { ; }
      usually for the following
      if ($condition_that_should_return_true) { ; # success! } elsif (...) . .

      If you are coding in a manner than can be interpreted a multiple number of ways depending on context, then you should either code within the context, or avoid expressing your statement when you're unsure of the context.

      I suggest you have a read of Code Complete. It may make you aware of some programming methodologies that will help you avoid unsafe programming.

      A {} by itself, I agree, but when you're given something like {}{} and somebody tells you it's valid code, do you think those are two empty blocks, one empty block and one hashref (in either order), or complain that it looks like two hashrefs and the code is wrong?
Re: Is { } an empty block or a bug in perl?
by itub (Priest) on Jul 06, 2005 at 17:48 UTC
    The fuzzy logic and workarounds for disambiguating between code blocks and hashrefs are documented to some extent in perlref |Making References, so I wouldn't call it a bug.
Re: Is { } an empty block or a bug in perl?
by ikegami (Patriarch) on Jul 06, 2005 at 18:27 UTC

    Would {}{} be
    1) a block followed by a hash constructor,
    2) a hash constructor followed by a block,
    3) a hash constructor followed by a hash constructor, or
    4) a block followed by a block?

    They are not equivalent. At least, they are not equivalent if found at the end of a sub. The syntax error forces the user to disambiguate.

      It could only be 1 or 4. An empty hashref that's not at the end of a block, file or list or in a complex expression should be terminated by a semi-colon (like any other (sub)expression). A block is terminated by its closing brace.

      My main objection is that it's silly to have a discrepancy between the current behaviour:

      {} # hashref
      and the proposed:
      {}{} # two blocks...
      So it's best to keep the current behaviour, ie. syntax error. I must say the current syntax error is actually pretty accurate too, so it won't be too hard to find the problem either.

Re: Is { } an empty block or a bug in perl?
by Anonymous Monk on Jul 07, 2005 at 09:52 UTC
    After all, you give it well written code and it complains about a syntax error.
    "Well written code"? It's a syntax error, so it isn't well written!

    We all know that { } is overloaded in Perl. It could be the start of a block, and it could be the start of a hashref - and often, context won't tell perl what to expect. So Perl has to guess based on what's inside the braces - but there's nothing inside to help perl, so it guesses "hashref".

    And it's not easy to fix - not without a lot of backtracking. What do you think:

    {foo => 1}{}
    should parse as? It's currently a syntax error (because perl thinks the first { } construct is a hashref), but perl could parse it as two blocks as well!

    Remember that, for speed reasons, perl uses yacc to parse Perl, and not a recursive decent parser.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://472794]
Approved by davidrw
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-23 09:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found