Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Retrieve the next key/value pa ...Retrieve list of indices from ... (Berikutnya)
Functions for real %HASHes

Test whether a hash key is present

Daftar Isi

  • exists EXPR

    Given an expression that specifies an element of a hash, returns true if thespecified element in the hash has ever been initialized, even if thecorresponding value is undefined.

    1. print "Exists\n" if exists $hash{$key};
    2. print "Defined\n" if defined $hash{$key};
    3. print "True\n" if $hash{$key};

    exists may also be called on array elements, but its behavior is much lessobvious and is strongly tied to the use of delete on arrays. Be awarethat calling exists on array values is deprecated and likely to be removed ina future version of Perl.

    1. print "Exists\n" if exists $array[$index];
    2. print "Defined\n" if defined $array[$index];
    3. print "True\n" if $array[$index];

    A hash or array element can be true only if it's defined and defined only ifit exists, but the reverse doesn't necessarily hold true.

    Given an expression that specifies the name of a subroutine,returns true if the specified subroutine has ever been declared, evenif it is undefined. Mentioning a subroutine name for exists or defineddoes not count as declaring it. Note that a subroutine that does notexist may still be callable: its package may have an AUTOLOADmethod that makes it spring into existence the first time that it iscalled; see perlsub.

    1. print "Exists\n" if exists &subroutine;
    2. print "Defined\n" if defined &subroutine;

    Note that the EXPR can be arbitrarily complicated as long as the finaloperation is a hash or array key lookup or subroutine name:

    1. if (exists $ref->{A}->{B}->{$key}) { }
    2. if (exists $hash{A}{B}{$key}) { }
    3. if (exists $ref->{A}->{B}->[$ix]) { }
    4. if (exists $hash{A}{B}[$ix]) { }
    5. if (exists &{$ref->{A}{B}{$key}}) { }

    Although the most deeply nested array or hash element will not spring intoexistence just because its existence was tested, any intervening ones will.Thus $ref->{"A"} and $ref->{"A"}->{"B"} will springinto existence due to the existence test for the $key element above.This happens anywhere the arrow operator is used, including even here:

    1. undef $ref;
    2. if (exists $ref->{"Some key"}) { }
    3. print $ref; # prints HASH(0x80d3d5c)

    This surprising autovivification in what does not at first--or evensecond--glance appear to be an lvalue context may be fixed in a futurerelease.

    Use of a subroutine call, rather than a subroutine name, as an argumentto exists() is an error.

    1. exists ⊂ # OK
    2. exists &sub(); # Error
 
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) Retrieve the next key/value pa ...Retrieve list of indices from ... (Berikutnya)