exists
- exists EXPR
Given an expression that specifies an element of a hash, returns true if the specified element in the hash has ever been initialized, even if the corresponding value is undefined.
exists may also be called on array elements, but its behavior is much less obvious and is strongly tied to the use of delete on arrays.
WARNING: Calling exists on array values is strongly discouraged. The notion of deleting or checking the existence of Perl array elements is not conceptually coherent, and can lead to surprising behavior.
A hash or array element can be true only if it's defined and defined only if it exists, but the reverse doesn't necessarily hold true.
Given an expression that specifies the name of a subroutine, returns true if the specified subroutine has ever been declared, even if it is undefined. Mentioning a subroutine name for exists or defined does not count as declaring it. Note that a subroutine that does not exist may still be callable: its package may have an
AUTOLOAD
method that makes it spring into existence the first time that it is called; see perlsub.Note that the EXPR can be arbitrarily complicated as long as the final operation is a hash or array key lookup or subroutine name:
Although the most deeply nested array or hash element will not spring into existence just because its existence was tested, any intervening ones will. Thus
$ref->{"A"}
and$ref->{"A"}->{"B"}
will spring into existence due to the existence test for the$key
element above. This happens anywhere the arrow operator is used, including even here:This surprising autovivification in what does not at first--or even second--glance appear to be an lvalue context may be fixed in a future release.
Use of a subroutine call, rather than a subroutine name, as an argument to exists is an error.