ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
3.2.25 defineddefined This function returns a Boolean value saying whether You may also check to see whether arrays, hashes, or subroutines have been allocated any memory yet. Arrays and hashes are allocated when you first put something into them, whereas subroutines are allocated when a definition has been successfully parsed. Using defined on the predefined special variables is not guaranteed to produce intuitive results. 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 only tells you whether the value is defined, not whether the key has an entry in the hash table. It's possible to have an undefined scalar value for an existing hash key. Use exists to determine whether the hash key exists. In the next example we use the fact that some operations return the undefined value when you run out of data: print "$val\n" while defined($val = pop(@ary)); The same thing goes for error returns from system calls: die "Can't readlink $sym: $!" unless defined($value = readlink $sym); Since symbol tables for packages are stored as hashes (associative arrays), it's possible to check for the existence of a package like this: die "No XYZ package defined" unless defined %XYZ::; Finally, it's possible to avoid blowing up on nonexistent subroutines: sub saymaybe { if (defined &say) { say(@_); } else { warn "Can't say"; } } See also undef. |