Wednesday, January 8, 2020

A Quick Tutorial of Perl exists() Function

Perls 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 youll see this one returns false.Lets look at the same routine, but with a blank address key: Youll 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.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.