UNIVERSAL - base class for ALL classes (blessed references)

目次


NAME 名前

UNIVERSAL - base class for ALL classes (blessed references)

UNIVERSAL - 全てのクラス(ブレスされたリファレンス)の基底クラス


SYNOPSIS 概要

    $is_io    = $fd->isa("IO::Handle");
    $is_io    = Class->isa("IO::Handle");

    $does_log = $obj->DOES("Logger");
    $does_log = Class->DOES("Logger");

    $sub      = $obj->can("print");
    $sub      = Class->can("print");

    $sub      = eval { $ref->can("fandango") };
    $ver      = $obj->VERSION;

    # but never do this!
    $is_io    = UNIVERSAL::isa($fd, "IO::Handle");
    $sub      = UNIVERSAL::can($obj, "print");

DESCRIPTION 説明

UNIVERSAL is the base class from which all blessed references inherit. See perlobj [CPAN].

UNIVERSAL は全てのブレスされたリファレンスが継承する基底クラスです, perlobj [CPAN] も参照してください.

UNIVERSAL provides the following methods:

UNIVERSAL は以下のメソッドを提供しています:

$obj->isa( TYPE )
CLASS->isa( TYPE )
eval { VAL->isa( TYPE ) }

Where

ここで

TYPE

is a package name

はパッケージ名

$obj

is a blessed reference or a string containing a package name

はブレスされたリファレンスかパッケージ名を含んだ文字列

CLASS

is a package name

はパッケージ名

VAL

is any of the above or an unblessed reference

上のいずれかかブレスされていないリファレンス

When used as an instance or class method ($obj->isa( TYPE )), isa returns true if $obj is blessed into package TYPE or inherits from package TYPE.

インスタンス若しくはクラスメソッド ($obj->isa( TYPE ))として 使うときは $obj がパッケージ TYPE 若しくはパッケージ TYPE の派生にブレスされているときに を返します.

When used as a class method (CLASS->isa( TYPE ), sometimes referred to as a static method), isa returns true if CLASS inherits from (or is itself) the name of the package TYPE or inherits from package TYPE.

クラスメソッド (CLASS->isa( TYPE ): スタティックメソッドとして 参照されることもあります)として使う時は CLASS がパッケージ名 TYPE 若しくはパッケージ TYPE の派生から派生している(若しくは そのもの)の時に を返します.

If you're not sure what you have (the VAL case), wrap the method call in an eval block to catch the exception if VAL is undefined.

もし調べたい対象が何であるのかはっきりしていないとき(VALのケース)では, VAL が未定義のときの例外を捕捉するために, メソッド呼び出しを eval で包む必要があります.

If you want to be sure that you're calling isa as a method, not a class, check the invocant with blessed from Scalar::Util [CPAN] first:

isa をクラスではなくメソッド呼び出しであることを保証したいのであれば 最初に Scalar::Util [CPAN] の blessed で呼び出しを確認します:

  use Scalar::Util 'blessed';

  if ( blessed( $obj ) && $obj->isa("Some::Class") {
      ...
  }
$obj->DOES( ROLE )
CLASS->DOES( ROLE )

DOES checks if the object or class performs the role ROLE. A role is a named group of specific behavior (often methods of particular names and signatures), similar to a class, but not necessarily a complete class by itself. For example, logging or serialization may be roles.

DOES はオブジェクト若しくはクラスが役割 ROLE として機能するかどうかを 調べます. 役割(role)とは特定の振る舞いに名前を付けたグループです( しばしば特定の名前やシグニチャを持ったメソッドです), クラスと似て いますが, それ自体が完全なクラスである必要はありません. 例えば, logging や serialization は役割になるでしょう.

DOES and isa are similar, in that if either is true, you know that the object or class on which you call the method can perform specific behavior. However, DOES is different from isa in that it does not care how the invocant performs the operations, merely that it does. (isa of course mandates an inheritance relationship. Other relationships include aggregation, delegation, and mocking.)

DOESisa はどちらも, 真であればメソッドを呼び出したオブジェクト 若しくはクラスが特定の振る舞いを行えるかどうかを知ることが 出来るという点で似ています. しかしながら, DOES は 呼び出し先が操作を どのように処理するかには気にしないで, 単にそれを行うということを調べるに すぎないという点で isa とは異なります. (isa はもちろん 関連の継承を委任しています. 他の関連には集約(aggregation), 委譲(delegation), mock が含まれます.)

By default, classes in Perl only perform the UNIVERSAL role. To mark that your own classes perform other roles, override DOES appropriately.

デフォルトでは, Perl のクラスは UNIVERSAL ロールとして機能するだけです. 自分のクラスで他のロールの機能をマークするには, DOES を適切に オーバーライドします.

There is a relationship between roles and classes, as each class implies the existence of a role of the same name. There is also a relationship between inheritance and roles, in that a subclass that inherits from an ancestor class implicitly performs any roles its parent performs. Thus you can use DOES in place of isa safely, as it will return true in all places where isa will return true (provided that any overridden DOES and isa methods behave appropriately).

ロールとクラスの間には, 各クラスは暗にその同じ名前のロールが存在するという 関連があります. 継承トロールには, 基底クラスから派生したサブクラスは 暗黙にその親の機能する任意のロールは機能するという点も関連があります. それ故に isa が真を返す全ての場所で DOES は真を返すので(オーバー ライドされた DOES 及び isa メソッドは適切に振る舞うため), 安全に isa の代わりに DOES を使うことが出来ます,

$obj->can( METHOD )
CLASS->can( METHOD )
eval { VAL->can( METHOD ) }

can checks if the object or class has a method called METHOD. If it does, then it returns a reference to the sub. If it does not, then it returns undef. This includes methods inherited or imported by $obj, CLASS, or VAL.

can はオブジェクト若しくはクラスが METHOD というメソッドを 持っているかを調べます. もし持っていれば関数リファレンスが返されます. もし持っていなければ undef が返されます. これには $obj, CLASS, 若しくは VAL で継承されている若しくはインポートされている メソッドが含まれます.

can cannot know whether an object will be able to provide a method through AUTOLOAD (unless the object's class has overriden can appropriately), so a return value of undef does not necessarily mean the object will not be able to handle the method call. To get around this some module authors use a forward declaration (see perlsub [CPAN]) for methods they will handle via AUTOLOAD. For such 'dummy' subs, can will still return a code reference, which, when called, will fall through to the AUTOLOAD. If no suitable AUTOLOAD is provided, calling the coderef will cause an error.

can はオブジェクトが(オブジェクトのクラスが can を適切にオーバー ライドしていないのであれば) AUTOLOAD を通してメソッドを提供可能かどうかは 知ることができません, そのため undef が返ってきてもオブジェクトが そのメソッド呼び出しを処理することができないとは限りません. これを 回避するにはモジュールの作者が AUTOLOAD を使って処理するメソッドに対して 前方宣言を使うことです(perlsub [CPAN]参照). そのような'ダミー'の関数は can はコードリファレンスを返しますが, それが呼び出された時には AUTOLOAD へとフォールスルーされます. もし適切な AUTOLOAD が提供されて いなければ, コードリファレンスの呼び出しはエラーになるでしょう.

You may call can as a class (static) method or an object method.

can はクラス(スタティック)メソッド, オブジェクトメソッドとして 呼び出すことができます.

Again, the same rule about having a valid invocant applies -- use an eval block or blessed if you need to be extra paranoid.

繰り返しますが, 有効な呼び出しのためには同じルールがつかわれます -- eval ブロックかよりこだわるのであれば blessed を使う必要が あります.

VERSION ( [ REQUIRE ] )

VERSION will return the value of the variable $VERSION in the package the object is blessed into. If REQUIRE is given then it will do a comparison and die if the package version is not greater than or equal to REQUIRE.

VERISON はオブジェクトがブレスされているパッケージの変数 $VERSION の値を返します. もし REQUIRE が与えられていた 場合には比較をおコアってパッケージのバージョンが REQUIRE 以上でなかった場合には die します.

VERSION can be called as either a class (static) method or an object method.

VERSION はクラス(スタティック)メソッド若しくはオブジェクトメソッド として呼び出すことができます.


EXPORTS エクスポート

None by default.

デフォルトでは何もエクスポートされません.

You may request the import of three functions (isa, can, and VERSION), however it is usually harmful to do so. Please don't do this in new code.

3つの関数(isa, can, そして VERSION)をインポートしたいかも しれませんが, それを行うことはたいてい有害なことです. 新しいコードでは それは行わないでください.

For example, previous versions of this documentation suggested using isa as a function to determine the type of a reference:

例えば, このドキュメントの以前のバージョンではリファレンスの 種類を調べるのに isa を関数として使うことを提案していました:

  use UNIVERSAL 'isa';

  $yes = isa $h, "HASH";
  $yes = isa "Foo", "Bar";

The problem is that this code will never call an overridden isa method in any class. Instead, use reftype from Scalar::Util [CPAN] for the first case:

この時, このコードは他のクラスでオーバーライドされた isa メソッドを呼び出すことがないという問題があります. 1つめのケースについては代わりに Scalar::Util [CPAN] の reftype を 使うようにしてください.

  use Scalar::Util 'reftype';

  $yes = reftype( $h ) eq "HASH";

and the method form of isa for the second:

そして2番目のケースについてはメソッド形式の isa を使うように してください.

  $yes = Foo->isa("Bar");

TRANSALTE TO JAPANESE 和訳

 山科 氷魚 (YAMASHINA Hio) <hio@hio.jp>

Origlnal distribution is perl VERSION 5.9.4. Translated at 2007-03-19.

原典: perl VERSION 5.9.4. 翻訳日: 2007-03-19.

UNIVERSAL - base class for ALL classes (blessed references)

索引

UNIVERSAL - base class for ALL classes (blessed references)