defined 
defined EXPR
definedThis function returns a Boolean value saying whether
EXPR is a defined value. Most data you deal
with is defined, but a scalar that contains no valid string, numeric, or
reference value is said to contain the undefined value, or undef for short. Initializing a scalar
variable to a particular value defines it, and it stays defined until
you assign an undefined value to it or explicitly call the undef function on that variable.
Many operations return undef
under exceptional conditions, such as at end-of-file, when using an
uninitialized variable’s value, an operating system error, etc. Since
undef is just one kind of false
value, a simple Boolean test does not distinguish between undef, numeric zero, the null string, and the
one-character string, “0”—all of
which are equally false. The defined
function lets you distinguish between an undefined null string and a
defined null string when using operators that might return a real null
string.
Here is a fragment that tests a scalar value from a hash:
print if defined $switch{D};When used on a hash element like this, defined tells only whether the value is
defined, not whether the key has an entry in the hash. It’s possible to
have a key whose value is undefined; the key itself, however, still
exists. Use exists to determine
whether the hash key exists.
In the next example, we exploit the convention that some operations ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access