Perl中defined用来判断一个变量是不是undef,也就是知道有这个变量但不知道它是不是undef;exists用来判断一个hash中的键是否存在。键存在但有可能是undef。
#!/usr/bin/perl -w use strict; my $a; my $b=""; my $c=1; print "a ok\n" if defined $a; print "b ok\n" if defined $b; print "c ok\n" if defined $c; #print "a exists\n" if exists $a; my %hash = ( 'aa' => 'aaaa',); if(exists $hash{'aa'}){ print "aa exists\n"; }else{ print "aa doesn't exists\n"; } my $vv = undef; my %hash; $hash{111} = $vv; print (exists $hash{111} ? "ok\n" : "Oops\n"); print (defined $hash{111} ? "ok\n" : "Oops\n");