=over =item defined EXPR X<defined> X<undef> X<undefined> =item defined Returns a Boolean value telling whether EXPR has a value other than the undefined value L<C<undef>|/undef EXPR>. If EXPR is not present, L<C<$_>|perlvar/$_> is checked. Many operations return L<C<undef>|/undef EXPR> to indicate failure, end of file, system error, uninitialized variable, and other exceptional conditions. This function allows you to distinguish L<C<undef>|/undef EXPR> from other values. (A simple Boolean test will not distinguish among L<C<undef>|/undef EXPR>, zero, the empty string, and C<"0">, which are all equally false.) Note that since L<C<undef>|/undef EXPR> is a valid scalar, its presence doesn't I<necessarily> indicate an exceptional condition: L<C<pop>|/pop ARRAY> returns L<C<undef>|/undef EXPR> when its argument is an empty array, I<or> when the element to return happens to be L<C<undef>|/undef EXPR>. You may also use C<defined(&func)> to check whether subroutine C<func> has ever been defined. The return value is unaffected by any forward declarations of C<func>. A subroutine that is not defined may still be callable: its package may have an C<AUTOLOAD> method that makes it spring into existence the first time that it is called; see L<perlsub>. Use of L<C<defined>|/defined EXPR> on aggregates (hashes and arrays) is no longer supported. It used to report whether memory for that aggregate had ever been allocated. You should instead use a simple test for size: if (@an_array) { print "has array elements\n" } if (%a_hash) { print "has hash members\n" } When used on a hash element, it tells you whether the value is defined, not whether the key exists in the hash. Use L<C<exists>|/exists EXPR> for the latter purpose. Examples: print if defined $switch{D}; print "$val\n" while defined($val = pop(@ary)); die "Can't readlink $sym: $!" unless defined($value = readlink $sym); sub foo { defined &$bar ? $bar->(@_) : die "No bar"; } $debugging = 0 unless defined $debugging; Note: Many folks tend to overuse L<C<defined>|/defined EXPR> and are then surprised to discover that the number C<0> and C<""> (the zero-length string) are, in fact, defined values. For example, if you say "ab" =~ /a(.*)b/; The pattern match succeeds and C<$1> is defined, although it matched "nothing". It didn't really fail to match anything. Rather, it matched something that happened to be zero characters long. This is all very above-board and honest. When a function returns an undefined value, it's an admission that it couldn't give you an honest answer. So you should use L<C<defined>|/defined EXPR> only when questioning the integrity of what you're trying to do. At other times, a simple comparison to C<0> or C<""> is what you want. See also L<C<undef>|/undef EXPR>, L<C<exists>|/exists EXPR>, L<C<ref>|/ref EXPR>. =back