Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Load in a module at compile timeCreate an immediate core dump (Berikutnya)
Miscellaneous functions

Test whether a value, variable, or function is defined

Daftar Isi

  • defined EXPR

  • defined

    Returns a Boolean value telling whether EXPR has a value other thanthe undefined value undef. If EXPR is not present, $_ ischecked.

    Many operations return undef to indicate failure, end of file,system error, uninitialized variable, and other exceptionalconditions. This function allows you to distinguish undef fromother values. (A simple Boolean test will not distinguish amongundef, zero, the empty string, and "0", which are all equallyfalse.) Note that since undef is a valid scalar, its presencedoesn't necessarily indicate an exceptional condition: popreturns undef when its argument is an empty array, or when theelement to return happens to be undef.

    You may also use defined(&func) to check whether subroutine &funchas ever been defined. The return value is unaffected by any forwarddeclarations of &func. A subroutine that is not definedmay still be callable: its package may have an AUTOLOAD method thatmakes it spring into existence the first time that it is called; seeperlsub.

    Use of defined on aggregates (hashes and arrays) is deprecated. Itused to report whether memory for that aggregate had ever beenallocated. This behavior may disappear in future versions of Perl.You should instead use a simple test for size:

    1. if (@an_array) { print "has array elements\n" }
    2. 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 exists for the latterpurpose.

    Examples:

    1. print if defined $switch{D};
    2. print "$val\n" while defined($val = pop(@ary));
    3. die "Can't readlink $sym: $!"
    4. unless defined($value = readlink $sym);
    5. sub foo { defined &$bar ? &$bar(@_) : die "No bar"; }
    6. $debugging = 0 unless defined $debugging;

    Note: Many folks tend to overuse defined and are then surprised todiscover that the number 0 and "" (the zero-length string) are, in fact,defined values. For example, if you say

    1. "ab" =~ /a(.*)b/;

    The pattern match succeeds and $1 is defined, although itmatched "nothing". It didn't really fail to match anything. Rather, itmatched something that happened to be zero characters long. This is allvery 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 youshould use defined only when questioning the integrity of whatyou're trying to do. At other times, a simple comparison to 0 or "" iswhat you want.

    See also undef, exists, ref.

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Load in a module at compile timeCreate an immediate core dump (Berikutnya)