perl-5.38.0
exists EXPR

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

ハッシュ要素を示す表現が与えられ、指定された要素が、ハッシュに存在すれば、 たとえ対応する値が未定義でも真を返します。

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

exists may also be called on array elements, but its behavior is much less obvious and is strongly tied to the use of delete on arrays.

exists は配列の要素に対しても呼び出せますが、その振る舞いははるかに 不明確で、配列に対する delete の使用と強く 結びついています。

WARNING: Calling exists on array values is strongly discouraged. The notion of deleting or checking the existence of Perl array elements is not conceptually coherent, and can lead to surprising behavior.

警告: 配列の値に対して exists を呼び出すことは強く 非推奨です。 Perl の配列要素を削除したり存在を調べたりする記法は概念的に一貫しておらず、 驚くべき振る舞いを引き起こすことがあります。

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

A hash or array element can be true only if it's defined and defined only if it 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, even if it is undefined. Mentioning a subroutine name for exists or defined does not count as declaring it. Note that a subroutine that does not exist may still be callable: its package may have an AUTOLOAD method that makes it spring into existence the first time that it is called; see perlsub.

引数としてサブルーチンの名前が指定された場合、 指定されたサブルーチンが宣言されていれば(たとえ未定義でも) 真を返します。 exists や defined のために言及されているサブルーチン名は 宣言としてのカウントに入りません。 存在しないサブルーチンでも呼び出し可能かもしれないことに注意してください: パッケージが AUTOLOAD メソッドを持っていて、最初に呼び出された時に 存在を作り出すかもしれません; perlsub を参照してください。

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

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

最終的な操作がハッシュや配列の key による検索または サブルーチン名である限りは、EXPR には任意の複雑な式を置くことができます:

    if (exists $ref->{A}->{B}->{$key})  { }
    if (exists $hash{A}{B}{$key})       { }

    if (exists $ref->{A}->{B}->[$ix])   { }
    if (exists $hash{A}{B}[$ix])        { }

    if (exists &{$ref->{A}{B}{$key}})   { }

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

最も深くネストした配列やハッシュの要素は、その存在をテストしただけでは 存在するようにはなりませんが、途中のものは存在するようになります。 従って $ref->{"A"}$ref->{"A"}->{"B"} は上記の $key の 存在をテストしたことによって存在するようになります。 これは、矢印演算子が使われるところでは、以下のようなものを含むどこででも 起こります。

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

Use of a subroutine call, rather than a subroutine name, as an argument to exists is an error.

exists の引数としてサブルーチン名でなくサブルーチン 呼び出しを使うと、エラーになります。

    exists ⊂    # OK
    exists &sub();  # Error