Jul 22, 2009 11:00
###################
use warnings; use strict;
my %hash;
$hash{''}="dude, seriously?\n";
print $hash{''};
###################
Are you kidding me Perl?! You let me key a hash with the empty string?! WTF. This humble newb thinks that really should make an error. I don't want to accidentally key stuff with the empty string. Rrr.
Leave a comment
Comments 15
print "1:",$hash{''};
print "2:",$hash{""};
print "3:",$hash{q()};
print "4:",$hash{qq()};
returns:
1:dude, seriously?
2:dude, seriously?
3:dude, seriously?
4:dude, seriously?
Reply
it's so wroooooooong!
Reply
Reply
I'm not sure what problems this might cause, but I can imagine it bugging things up by not alerting me that I wrote stuff a bit wrong earlier when I was throwing stuff into said hash. I like using strict because it warns me when stuff is null or just weird. And this seems weird and null. yuck.
Reply
Reply
Reply
But allowing you to use such a key strikes me as pretty damn stupid.
Reply
Thanks for confirming my thoughts on that null key though. I'm still learning but it's nice to find I'm acquiring some judgment. :D
Reply
As another commenter noted, a hash is essentially a function that takes one argument. (In fact, a hash is very useful for caching the result of an expensive function.) And an empty string is certainly a valid function argument.
Reply
For instance I'm constantly running into problems with library functions that respond to empty lists by crashing and burning rather than doing nothing (which would be the expected behavior if you extrapolate from what they do with 2 elements, 1 element....)
I could see a problem if there was a very easy way to mistype something in order to get an empty string or something that coerces into an empty string. Coercion's one of those complicated semantics things.
Reply
Leave a comment