Dude, seriously?

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

riffalike July 22 2009, 18:08:44 UTC
On the plus side:

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

flamingnerd July 22 2009, 18:21:17 UTC
:P

it's so wroooooooong!

Reply

riffalike July 22 2009, 18:56:59 UTC
Under what circumstance are you envisioning it causing a problem? Are you validating inputs prior to insertion with something like if defined?

Reply

flamingnerd July 22 2009, 19:35:14 UTC
no I'm not verifying, but I might from now on. I'd have never thought to before!

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


spoonless July 23 2009, 05:38:54 UTC
heh... perl is a total slut, she'll let you do whatever you want to her... even if it's really dirty ;)

Reply

flamingnerd July 23 2009, 16:27:45 UTC
Hee! I like this interpretation.

Reply


feng_huang July 23 2009, 08:07:33 UTC
I think that part of the philosophy is that if the language tries to prevent you from doing stupid things, it will often prevent you from doing clever things, too.

But allowing you to use such a key strikes me as pretty damn stupid.

Reply

flamingnerd July 23 2009, 16:30:07 UTC
huh. That's an interesting way to look at it. I like the freedom to be clever. It just makes the coding so enjoyable.

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

flwyd July 31 2009, 05:51:48 UTC
I expect empty strings as a valid hash key on a regular basis. Null too. For instance, suppose you're building a hash of lists based on a different data structure. You want a map of locations to people. The list keyed by empty string (or null, depending on your convention) is all the people you don't have a location for.

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


zzyzx_xyzzy July 23 2009, 23:44:48 UTC
hmmm. A hashtable is just a way to build a function on the domain of strings; since "" is indeed a string I don't see why to limit the domain. I tend to prefer simple semantics with fewer exceptions.

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

Up