What's the difference between exists and defined?

后端 未结 5 2039
花落未央
花落未央 2020-12-07 14:24

What is the difference between

if (defined $hash{$key}) { }

and

if (exists $hash{$key}) { }

When do I kno

5条回答
  •  猫巷女王i
    2020-12-07 15:07

    This example shows the difference. In general defined works for any structure and exist is related to hashes.

    my %hash=("1"=>undef);
    print "exists:".exists($hash{"1"})."\n";
    print "defined:".defined($hash{"1"})."\n";
    

    The difference is small and not so obvious, so expect that people will mess with it.

提交回复
热议问题