jkeenan1 has asked for the wisdom of the Perl Monks concerning the following question:

As typed on a Windows command-line:

C:\>perl -c -e "use strict; {}; no strict \"refs\";"

Output:

-e syntax OK

Now delete the semicolon after the block:

C:\>perl -c -e "use strict; {} no strict \"refs\";"

Output:

String found where operator expected at -e line 1, near "no strict "re +fs"" (Do you need to predeclare no?) "no" not allowed in expression at -e line 1, near "} " syntax error at -e line 1, near "} no strict " -e had compilation errors.

I see what's happening, but was startled when this happened inside a script. Is an empty block indeed not a block, but a statement requiring a semicolon terminator?

Jim Keenan

Replies are listed 'Best First'.
Re: Is an empty block not a block?
by Roy Johnson (Monsignor) on Nov 18, 2005 at 22:03 UTC
    Any set of braces that isn't part of another expression (such as an if-block or a variable usage) are interpreted as either a hash ref or block of code. Perl uses heuristics to figure out which. If you find that Perl is guessing wrong, you can make it look more like what you want it to be.

    Generally speaking, an empty set is going to be treated as a hashref. If, for some unfathomable reason, you want a no-op block, stick a semicolon inside it:

    perl -we "use strict; {;} no strict 'refs';"
    Perl sees that you've got code (an empty statement) in there, so it must be a code block. No warning about Useless use of single ref constructor like you'd get if you used {}; instead. (I turned on warnings so you could see a difference.)

    Conversely, if you want to make Perl interpret what looks like a block of code to be a hashref, put it in parentheses or stick a plus on the front. (I personally dislike the + convention.)

    F:\Perl>perl -we "use strict; {qw(key val)}; no strict 'refs';" Useless use of a constant in void context at -e line 1. Useless use of a constant in void context at -e line 1. F:\Perl>perl -we "use strict; ({qw(key val)}); no strict 'refs';" Useless use of single ref constructor in void context at -e line 1
    The second example is half as useless. :-)

    Caution: Contents may have been coded under pressure.

      You could also prefix the hash with "scalar." That's especially clear.

      return scalar { ... };
Re: Is an empty block not a block?
by GrandFather (Saint) on Nov 18, 2005 at 21:49 UTC

    {} is an empty hash. Consider:

    use warnings; use strict; my $hashRef = {}; print "This is fine";

    then remove the variable and assignment.

    use warnings; use strict; {}; print "This is fine";
    </c>

    DWIM is Perl's answer to Gödel
      But if so, why is a semicolon necessary. It isn't if the hashref isn't empty as in:
      {blah, "blah"} print "This is fine\n";

      I don't see why being empty changes anything essential.
      Perhaps in the empty case, perl doesn't know what it is, and that's the problem...
      (I guess {blah, "blah"} is considered to be a hashref; I don't know what else it would be...)
      chas

        Actualy {blah, "blah"} is considered a block. See below for proof. ;). Perl is trying to be smart and guessing on what exactly {} is based on context. {} makes lots of since as a hash ref but very little as a block. {blah, "blah"}is usefull as a block while my $x = {blah, "blah"}; is most probalby a hashref and treated that way.

        C:\Perl\test>perl -e"{test,'blah'} print 'hello'"; hello C:\Perl\test>perl -e"{test,'blah', print '1'} print 'hello'"; 1hello

        ___________
        Eric Hodges $_='y==QAe=e?y==QG@>@?iy==QVq?f?=a@iG?=QQ=Q?9'; s/(.)/ord($1)-50/eigs;tr/6123457/- \/|\\\_\n/;print;
Re: Is an empty block not a block?
by Aristotle (Chancellor) on Nov 19, 2005 at 04:32 UTC

    People very nearly never want an empty block. People very frequently want an empty anonymous hash. So when perl sees an empty set of curlies, it assumes you meant an empty anonymous hash. This is a very reasonable guess in my opinion.

    Makeshifts last the longest.