You need to make sure you understand defined, exists, and false values. You can enter a value of undef for a given key in a hash.
my %hash = (); $hash{'bar'} = undef;
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".
$hash{'bar'} = '';
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'} = undef; if(defined $hash{'bar'}){print "false"} if($hash{'bar'}){print "false"}
If you assign any other false value to a hash key you get a different result:
$hash{'bar'} = '0'; if(defined $hash{'bar'}){print "true") 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.

In reply to Re: Uninitialized errors when using 2 hashes. by shevek
in thread Uninitialized errors when using 2 hashes. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.