perl-5.42.0
__CLASS__

Invoked within a method, or similar location, such as a field initializer expression, this token returns the name of the class of the invoking instance. Essentially it is equivalent to ref($self) except that it can additionally be used in a field initializer to gain access to class methods, before the instance is fully constructed.

このトークンは、method 内、または フィールド初期化式などのような場所で呼び出されると、 呼び出し側インスタンスのクラスの名前を返します。 基本的には ref($self) と等価ですが、インスタンスが完全に 構築される前に、フィールド初期化子で追加的に使って クラスメソッドにアクセスできる点が異なります。

    use feature 'class';

    class Example1 {
        field $f = __CLASS__->default_f;

        sub default_f { 10 }
    }

In a basic class, this value will be the same as __PACKAGE__. The distinction can be seen when a subclass is constructed; it will give the class name of the instance being constructed, rather than just the package name that the actual code belongs to.

基底クラスでは、この値は __PACKAGE__ と同じになります。 サブクラスが構築されると違いが見られます; 実際のコードが属するパッケージ名でなく、 構築されるインスタンスのクラス名が与えられます。

    class Example2 :isa(Example1) {
        sub default_f { 20 }
    }

    my $obj = Example2->new;
    # The $f field now has the value 20