Saturday, July 9, 2011

What's the difference between exists and defined

defined checks the value of the variable, exists checks if it has been
previously declared/initialized. If it exists, plain and simple.

E.g.:

$hash{$key} = undef;
# various return values:
exists  $hash{$key};  # true
defined $hash{$key};  # false
$hash{$key};          # false

$hash{$key} = 0;
# various return values:
exists  $hash{$key};  # true
defined $hash{$key};  # true
$hash{$key};          # false
exists $hash{$foo};   # false

No comments: