__CLASS__ - Perldoc Browser

You are viewing the version of this documentation from Perl 5.41.7. This is a development version of Perl.
#__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.

 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.

 class Example2 :isa(Example1) {
	sub default_f { 20 }
 }
 my $obj = Example2->new;
 # The $f field now has the value 20

AltStyle によって変換されたページ (->オリジナル) /