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

It makes a lot more sense for {} (update: by itself) to be an empty hashref, than an empty block. I use empty hashrefs all the time, while an empty block by itself is completely useless. And if {} is a hashref, {}{} is a syntax error.

  • Comment on Re: Is { } an empty block or a bug in perl?

Replies are listed 'Best First'.
Re^2: Is { } an empty block or a bug in perl?
by thor (Priest) on Jul 06, 2005 at 15:29 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

        After I wrote the initial reply this morning, I was thinking that it might be this sort of thing. I like to use an explicit return in my subs, so I guess I wouldn't run into this problem.

        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

Re^2: Is { } an empty block or a bug in perl?
by monarch (Priest) on Jul 07, 2005 at 04:19 UTC
    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.

Re^2: Is { } an empty block or a bug in perl?
by cog (Parson) on Jul 06, 2005 at 13:56 UTC
    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?
        warning: nerdy nitpick ahead :)

        I would argue that {;} is not an empty block. I think it's analagous to a set that contains the empty set as it's only element.

        but i don't think the compiler makes the distinction.. perl -MO=Terse -e 'if(1){;}' and perl -MO=Terse -e 'if(1){}' given identical output.
        it would would be useless

        That doesn't matter.

        despite what people would tell you

        That doesn't matter either.

        IMHO, it is valid code. Look at it this way:

        # block #1 { } # block #2 { }

        See? Two empty blocks waiting for code. It is valid.

        Just because no one in their perfect state of mind would use that doesn't make it invalid.