perl-5.38.0
ref EXPR
ref

Examines the value of EXPR, expecting it to be a reference, and returns a string giving information about the reference and the type of referent. If EXPR is not specified, $_ will be used.

Examines the value of リファレンスと想定される EXPR の値を調べて、 そのリファレンスとリファレンス先の型に関する情報を表す 文字列を返します。 EXPR が指定されていない場合、$_ が使われます。

If the operand is not a reference, then the empty string will be returned. An empty string will only be returned in this situation. ref is often useful to just test whether a value is a reference, which can be done by comparing the result to the empty string. It is a common mistake to use the result of ref directly as a truth value: this goes wrong because 0 (which is false) can be returned for a reference.

オペランドがリファレンスでない場合、空文字列が返されます。 空文字列はこの場合にのみ返されます。 結果を空文字列を比較することでできるので、 ref は単にある値がリファレンスかどうかを調べるのにしばしば有用です。 ref の結果を直接真の値として使うのは良くある誤りです: リファレンスの場合に (偽である) 0 が返されることがあるので、 これは誤りです。

If the operand is a reference to a blessed object, then the name of the class into which the referent is blessed will be returned. ref doesn't care what the physical type of the referent is; blessing takes precedence over such concerns. Beware that exact comparison of ref results against a class name doesn't perform a class membership test: a class's members also include objects blessed into subclasses, for which ref will return the name of the subclass. Also beware that class names can clash with the built-in type names (described below).

オペランドが bless されたオブジェクトへのリファレンスの場合、 リファレンス先が bless されているクラス名が返されます。 ref はリファレンス先の物理的な種類については気にしません; bless されているかがそのような関心より優先されます。 ref の結果とクラス名の正確な比較は、クラスの所属のテストを 実行しないことに注意してください: ref がサブクラスの名前を返す場合、 あるクラスのメンバはサブクラスに bless されているオブジェクトを 含んでいます。 クラス名は(後述する)組み込みの型名と衝突することにも注意してください。

If the operand is a reference to an unblessed object, then the return value indicates the type of object. If the unblessed referent is not a scalar, then the return value will be one of the strings ARRAY, HASH, CODE, FORMAT, or IO, indicating only which kind of object it is. If the unblessed referent is a scalar, then the return value will be one of the strings SCALAR, VSTRING, REF, GLOB, LVALUE, or REGEXP, depending on the kind of value the scalar currently has. But note that qr// scalars are created already blessed, so ref qr/.../ will likely return Regexp. Beware that these built-in type names can also be used as class names, so ref returning one of these names doesn't unambiguously indicate that the referent is of the kind to which the name refers.

オペランドが bless されていないオブジェクトへのリファレンスの場合、 返り値はオブジェクトの型を示します。 bless されていないリファレンス先がスカラではない場合、 返り値はオブジェクトの種類を示す、 ARRAY, HASH, CODE, FORMAT, IO のいずれかの文字列です。 bless されていないリファレンス先がスカラの場合、 返り値はそのスカラが現在保持している種類に依存して、 SCALAR, VSTRING, REF, GLOB, LVALUE, REGEXP の いずれかの文字列です。 しかし、qr// は既に bless されて作成されるので、 ref qr/.../ はおそらく Regexp を返すことに注意してください。 これらの組み込み型名はまたクラス名として使われることができるので、 ref がこれらの名前の一つを返すことは、 明らかにリファレンス先がその名前が示している種類のものであることを 示しているわけではないことに注意してください。

The ambiguity between built-in type names and class names significantly limits the utility of ref. For unambiguous information, use Scalar::Util::blessed() for information about blessing, and Scalar::Util::reftype() for information about physical types. Use the isa method for class membership tests, though one must be sure of blessedness before attempting a method call. Alternatively, the isa operator can test class membership without checking blessedness first.

組み込み型とクラス名の間の曖昧さは ref の有用性を大きく制限しています。 曖昧でない情報のためには、bless に関する情報については Scalar::Util::blessed() を、 物理的な型の情報については Scalar::Util::reftype() を使ってください。 クラスの所属メンバテストには the isa method を使ってください; 但し、メソッド呼び出しを試みる前に bless されていることを 確認しなければなりません。 あるいは、 isa operator は、 最初に bless されているかを確認することなくクラスメンバをテストできます。

See also perlref and perlobj.

perlrefperlobj も参照してください。