in reply to Uninitialized errors when using 2 hashes.
At this point, defined $hash{'bar'} is false, but exists $hash{'bar'} evaluates to true. defined will signal if a key has a "defined" value. exists signals whether the key "exists".my %hash = (); $hash{'bar'} = undef;
The above code will evaluate to true when tested with defined or exists. One thing to remember is that there are four false values: '',0,'0', and undef. If you assign undef to a hash key the following if statements are equivalent:$hash{'bar'} = '';
If you assign any other false value to a hash key you get a different result:$hash{'bar'} = undef; if(defined $hash{'bar'}){print "false"} if($hash{'bar'}){print "false"}
The lesson, I guess, is when you are testing whether a value has been set to undef you should use defined;it will return false for the value undef, and true for all other values. When testing whether a key exists in a hash then you use exist. When testing whether a value is false or true you should just use the value itself.$hash{'bar'} = '0'; if(defined $hash{'bar'}){print "true") if($hash{'bar'}){print "false")
|
|---|