exists () and defined () do not seem to be applicable for the situation, they just see that the value for the given key is undefined, they do not check if the hash really has a key
Wrong. This is really what defined() does, but exists() does exactly what you want:
my %hash = ( key1 => 'value', key2 => undef, ); foreach my $key (qw(key1 key2 key3)) { print "\$hash{$key} exists: " . (exists $hash{$key} ? "yes" : "no") . "\n"; print "\$hash{$key} is defined: " . (defined $hash{$key} ? "yes" : "no") . "\n"; }
gives:
$ hash {key1} exists: yes
$ hash {key1} is defined: yes
$ hash {key2} exists: yes
$ hash {key2} is defined: no
$ hash {key3} exists: no
$ hash {key3} is defined: no
The documentation for these two functions is available on the command line in perldoc -f defined and perldoc -f exists (or read the documentation for all methods in perldoc perlfunc *). The official web documentation is here:
* Since you specifically mentioned RTFM, and you may not know about the places for Perl documentation, let me also point out that you can get the full index of all perldocs at perldoc perl or at http://perldoc.perl.org .
Ether
source share