=head1 NAME
perlclass - Perl class syntax reference
=head1 SYNOPSIS
use v5.38;
use feature 'class';
class My::Example 1.234 {
field $x;
ADJUST {
$x = "Hello, world";
}
method print_message {
say $x;
}
}
My::Example->new->print_message;
=head1 DESCRIPTION
This document describes the syntax of Perl's C feature, which provides
native keywords for object-oriented programming.
=head2 History
Since Perl 5, support for objects revolved around the concept of I
references with a package name (see L). Such a
reference could then be used to call subroutines from the package it was
blessed with (or any of its parents). This system, while bare-bones, was
flexible enough to allow creation of multiple more advanced, community-driven
systems for object orientation. For more information, see L and
L.
The C feature is a core implementation of a class syntax that is similar
to what one would find in other programming languages. It is not a wrapper
around C, but a completely new system built right into the perl
interpreter.
=head1 KEYWORDS
Enabling the C feature allows the usage of the following new keywords in
the current lexical scope:
=head2 class
class NAME BLOCK
class NAME VERSION BLOCK
class NAME VERSION : ATTRIBUTES... BLOCK
class NAME;
class NAME VERSION;
class NAME VERSION : ATTRIBUTES...;
The C keyword declares a new package (see L) that is
intended to be a class. All other keywords from the C feature should be
used within the scope of this declaration.
class WithVersion 1.000 {
# class definition goes here
}
Classes can be declared in either block or statement syntax. If a block is
used, the body of the block contains the implementation of the class. If the
statement form is used, the remainder of the current scope or file is used up
until the next C or C statement.
A C declaration can optionally have a version number, similar to the
C keyword. It can also optionally have attributes. If both are
specified, the version number must come first, before the attributes.
C and C declarations are similar, but classes automatically get
a constructor named C - you don't have to (and should not) write one.
Additionally, in the class BLOCK you are allowed to declare fields and methods.
=head2 field
field VARIABLE_NAME;
field VARIABLE_NAME = EXPR;
field VARIABLE_NAME : ATTRIBUTES;
field VARIABLE_NAME : ATTRIBUTES = EXPR;
Fields are variables that are visible in the scope of the class - more
specifically within L and L blocks. Each class
instance gets its own storage of fields, independent of other instances.
A field behaves like a normal lexically scoped variable. It has a sigil and is
private to the class (though creation of an accessor method will make it
accessible from the outside). The main difference is that different instances
access different values in the same scope.
class WithFields {
field $scalar = 42;
field @array = qw(this is just an array);
field %hash = (species => 'Martian', planet => 'Mars');
}
Fields may optionally have initializing expressions. If present, the expression
will be evaluated within the constructor of each object instance. During each
evaluation, the expression can use the value of any previously-set field, as
well as any other variables in scope.
class WithACounter {
my $next_count = 1;
field $count = $next_count++;
}
When combined with the C<:param> field attribute, the defaulting expression can
use any of the C, C/=> or C operators. Expressions using C will
apply whenever the caller did not pass the corresponding parameter to the
constructor at all. Expressions using C/=> will also apply if the caller did
pass the parameter but the value was undefined, and expressions using C
will apply if the value was false.
During a field initializing expression, the instance is not yet constructed
and so the C lexical is not available. However, the special
C<__class__> token may be used to obtain the name of the class being
constructed, for example in order to invoke class methods on it to help in
constructing values for fields.
class WithCustomField {
use constant DEFAULT_X => 10;
field $x = __CLASS__->DEFAULT_X;
}
This allows subclasses to override the method with different behaviour.
class DifferentCustomField :isa(WithCustomField) {
sub DEFAULT_X { rand > 0.5 ? 20 : 30 }
}
When an instance of C is constructed, the C<__class__>
expression in the base will yield the correct class name, and so invoke this
overridden method instead.
=head2 method
method METHOD_NAME SIGNATURE BLOCK
method METHOD_NAME BLOCK
method SIGNATURE BLOCK
method BLOCK
Methods are subroutines intended to be called in the context of class objects.
A variable named C populated with the current object instance will
automatically be created in the lexical scope of C.
Methods always act as if C