=over =item defined EXPR =item defined Returns a Boolean value telling whether EXPR has a value other than the undefined value C. If EXPR is not present, C<$_> will be checked. Many operations return C to indicate failure, end of file, system error, uninitialized variable, and other exceptional conditions. This function allows you to distinguish C from other values. (A simple Boolean test will not distinguish among C, zero, the empty string, and C<"0">, which are all equally false.) Note that since C is a valid scalar, its presence doesn't I indicate an exceptional condition: C returns C when its argument is an empty array, I when the element to return happens to be C. You may also use C to check whether a subroutine exists, by saying C without parentheses. On the other hand, use of C upon aggregates (hashes and arrays) is not guaranteed to produce intuitive results, and should probably be avoided. When used on a hash element, it tells you whether the value is defined, not whether the key exists in the hash. Use L 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 C, and then are 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, despite the fact that it matched "nothing". But it didn't really match nothing--rather, it matched something that happened to be C<0> 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 C only when you're 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. Currently, using C on an entire array or hash reports whether memory for that aggregate has ever been allocated. So an array you set to the empty list appears undefined initially, and one that once was full and that you then set to the empty list still appears defined. 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" } Using C on these, however, does clear their memory and then report them as not defined anymore, but you shouldn't do that unless you don't plan to use them again, because it saves time when you load them up again to have memory already ready to be filled. The normal way to free up space used by an aggregate is to assign the empty list. This counterintuitive behavior of C on aggregates may be changed, fixed, or broken in a future release of Perl. See also L, L, L. =back