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
| [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] |
|
|
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
| [reply] |
|
|
|
|
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. | [reply] [d/l] [select] |
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? | [reply] [d/l] [select] |
|
|
{ ; }
{ ; }
| [reply] [d/l] |
|
|
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.
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
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. | [reply] [d/l] |
|
|
|
|