hughperl has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I want to find out if a key in a hash is defined. when I try the following code from http://perldoc.perl.org/functions/exists.html
print "Exists\n" if exists $hash{$key};
when i have;
use strict; use warnings; use diagnostics;
I get a "use of uninitialized value warning" I know some values are uninitialised, and wanted to use the above function to seperate them out. Is there a way to do this without being warned!? Many thanks for your thoughts.

Replies are listed 'Best First'.
Re: hash defined?
by Roy Johnson (Monsignor) on Mar 06, 2008 at 20:41 UTC
    print "Exists\n" if defined $key and exists $hash{$key};

    Caution: Contents may have been coded under pressure.
Re: hash defined?
by locked_user sundialsvc4 (Abbot) on Mar 06, 2008 at 22:28 UTC

    I simply use defined(). If the key does not exist in the hash, the result is undef. If the key does exist and its present value is undef, the result will be the same. “It's what I want” in either case:   although the cases are distinct, I don't have to care.

Re: hash defined?
by whakka (Hermit) on Mar 06, 2008 at 20:47 UTC
    I would say first if you want to see if a key in a hash is defined, use defined and not exists.

    However I don't get such a warning with your code whether the key is defined or not. Is that right?