#!/usr/bin/perl -w use strict; use Data::Dumper; my %hash = ('a' => 2, 'b' => 3, 'c' => undef); print Dumper (\%hash); if (exists ($hash{'c'}) ) {print "key c exists\n"} else {print "key c does not exist\n"}; print Dumper (\%hash); if ( defined ($hash{'c'}) ) {print "key c defined\n"} else {print "key c not defined\n"}; if (exists ($hash{'d'}) ) {print "key d exists\n"} else {print "key d does not exist\n"}; #note that undef,"",'' and 0 all evaluate to "false" #play with c value above and run this code #you can call defined($xxx) to figure out the difference #between a false value from "",'',0 and undef. if (my $x = $hash{'c'}) {print "c has value $x\n"} else {print "c has value,\"\",0 or undef\n"}; if (my $x = $hash{'b'}) {print "b has value $x\n"} else {print "b has no value\n"}; __END__ $VAR1 = { 'c' => undef, 'a' => 2, 'b' => 3 }; key c exists $VAR1 = { 'c' => undef, 'a' => 2, 'b' => 3 }; key c not defined key d does not exist c has no value b has value 3