来自perldoc -f exists:
print "Exists\n" if exists $hash{$key};
print "Defined\n" if defined $hash{$key};
print "True\n" if $hash{$key};
print "Exists\n" if exists $array[$index];
print "Defined\n" if defined $array[$index];
print "True\n" if $array[$index];
Run Code Online (Sandbox Code Playgroud)
散列或数组元素只有在定义时才为真,如果存在则定义,但反过来不一定适用.
my @keys_with_values = grep { defined $hash{$_} } keys %hash;
Run Code Online (Sandbox Code Playgroud)
重读你的问题,似乎你试图找出你的散列中的任何值是否未定义,在这种情况下,你可以说像
my @keys_without_values = grep { not defined $hash{$_} }, keys %hash;
if (@keys_without_values) {
print "the following keys do not have a value: ",
join(", ", @keys_without_values), "\n";
}
Run Code Online (Sandbox Code Playgroud)