A Quick Tutorial of Perl exists() Function

Portait of young hacker with computer screen reflecting in the man's glasses.
Erik Tham / Getty Images

Perl's exists() function is used to check whether an element in an array or hash exists. It can also be used to check for the existence of subroutines. exists will return true as long as the element has been initialized, and even if the element is undefined.

In the above example, we look at a hash of our contact Bob and his phone number. First, we check for the existence of the phone element, which is obviously returning true. Next, we check for an element that does not exist, address, and you'll see this one returns false.
Let's look at the same routine, but with a blank address key:

You'll see that this one returns true on the address, even though there is no actual value. Be careful relying on the logic of exists, and remember the distinction between exists and has value.