Re: Is { } an empty block or a bug in perl?
by Joost (Canon) on Jul 06, 2005 at 13:51 UTC
|
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.
| [reply] |
|
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
| [reply] |
|
I was thinking of things like this, where the assignment is done later:
sub gimme_my_data {
my $self = shift;
$self->{data} || {}
}
you could use a code block there too if you want to ( || do { ... }).
| [reply] [d/l] [select] |
|
|
|
|
{
;
}
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. | [reply] [d/l] [select] |
|
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?
| [reply] [d/l] [select] |
|
{ ; }
{ ; }
| [reply] [d/l] |
|
|
|
|
|
|
|
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. | [reply] |
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.
| [reply] [d/l] |
|
{} # 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.
| [reply] [d/l] [select] |
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. | [reply] [d/l] |