The defined operator in Perl checks if a value is defined (not undef). It's important to distinguish:
defined($x) will return true if $x is not equal to undef (regardless of the value, even if it's an empty string or 0).defined($hash{$key}) checks if the value of an element is defined, but does not guarantee that the key itself exists.defined($array[$i]) — similarly, an element may be defined, but the array may be longer or shorter.exists.Example:
my %h = (a => undef); if (exists $h{a}) { print "Key 'a' exists "; } if (defined $h{a}) { print "Key 'a' is defined "; } else { print "Key 'a' not defined "; }
Result:
Key 'a' exists
Key 'a' not defined
What is the difference between
exists $hash{$key}anddefined $hash{$key}and when will checking defined for a hash give unexpected results?
Answer: defined $hash{$key} checks the value, but if the key does not exist, it will yield an undefined value. If the key exists but its value is undef, defined will return false. But if the key doesn't exist — it will also return false. Therefore, always use exists to check for the existence of a key.
Example:
my %h = (foo => undef); if (defined $h{foo}) { ... } # false if (exists $h{foo}) { ... } # true
Story
In one project, a "readiness" flag was checked as
defined($status{$id}), and if the value was undef, the task was considered unprocessed. However, keys with undef were valid, and there was no exists check — as a result, some processed data was restarted.
Story
A programmer, importing data from a file, compared strings like this:
if (defined $line && $line ne ''). The problem: sometimes $line could be the string '0'. This string is not empty, but checking with defined (instead of checking ne '') led to the loss of these strings.
Story
In a large Perl script, working with hashes, the only check for the existence of an element was defined — as a result, bugs and missing data in reports occurred for keys with undef values.