Invoked within amethod, 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 toref($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 20Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.
The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.