perl-5.38.0
defined EXPR
defined

Returns a Boolean value telling whether EXPR has a value other than the undefined value undef. If EXPR is not present, $_ is checked.

左辺値 EXPR が未定義値 undef 以外の値を持つか否かを示す、 ブール値を返します。 EXPR がない場合は、$_ がチェックされます。

Many operations return undef to indicate failure, end of file, system error, uninitialized variable, and other exceptional conditions. This function allows you to distinguish undef from other values. (A simple Boolean test will not distinguish among undef, zero, the empty string, and "0", which are all equally false.) Note that since undef is a valid scalar, its presence doesn't necessarily indicate an exceptional condition: pop returns undef when its argument is an empty array, or when the element to return happens to be undef.

多くの演算子が、EOF や未初期化変数、システムエラーといった、 例外的な条件で undef を返すようになっています。 この関数は、他の値と undef とを区別するために使えます。 (単純な真偽値テストでは、undef、0、"0" のいずれも偽を 返すので、区別することができません。) undef は有効なスカラ値なので、その存在が 必ずしも 例外的な状況を表すとは限らないということに注意してください: pop は引数が空の配列だったときに undef を 返しますが、あるいは 返すべき要素がたまたま undef だったのかもしれません。

You may also use defined(&func) to check whether subroutine func has ever been defined. The return value is unaffected by any forward declarations of func. A subroutine that is not defined 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.

defined(&func) とすることでサブルーチン func の存在を、 確かめることもできます。 返り値は func の前方定義には影響されません。 定義されていないサブルーチンも呼び出し可能です: 最初に呼び出されたときに存在するようにするための AUTOLOAD メソッドを持ったパッケージかもしれません; perlsub を参照してください。

Use of defined on aggregates (hashes and arrays) is no longer supported. It used to report whether memory for that aggregate had ever been allocated. You should instead use a simple test for size:

集合(ハッシュや配列)への defined の使用は もはや対応していません。 これはその集合にメモリが割り当てられたかを報告するのに用いられていました。 代わりにサイズに対する簡単なテストを使うべきです。

    if (@an_array) { print "has array elements\n" }
    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 latter purpose.

ハッシュの要素に対して用いると、value が定義されているか否かを 返すものであって、ハッシュに key が存在するか否かを返すのではありません。 この用途には、exists を使ってください。

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 defined and are then surprised to discover that the number 0 and "" (the zero-length string) are, in fact, defined values. For example, if you say

注意: 多くの人々が defined を使いすぎて、0""(空文字列) が実際のところ定義された値であることに驚くようです。 例えば、以下のように書くと:

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

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

パターンマッチングが成功し、$1 が定義されても、実際には 「なし」にマッチしています。 しかしこれは何にもマッチしていないわけではありません。 何かにはマッチしているのですが、たまたまそれが長さ 0 だっただけです。 これは非常に率直で正直なことです。 関数が未定義値を返すとき、正直な答えを返すことができないことを 告白しています。 ですので、あなたが自分がしようとしていることの完全性を確認するときにだけ defined を使うべきです。 その他の場合では、単に 0 または "" と比較するというのがあなたの 求めているものです。

See also undef, exists, ref.

undef, exists, ref も 参照してください。