1f2a19305Safresh1=head1 NAME 2f2a19305Safresh1 3f2a19305Safresh1perlclass - Perl class syntax reference 4f2a19305Safresh1 5f2a19305Safresh1=head1 SYNOPSIS 6f2a19305Safresh1 7f2a19305Safresh1 use v5.38; 8f2a19305Safresh1 use feature 'class'; 9f2a19305Safresh1 10f2a19305Safresh1 class My::Example 1.234 { 11f2a19305Safresh1 field $x; 12f2a19305Safresh1 13f2a19305Safresh1 ADJUST { 14f2a19305Safresh1 $x = "Hello, world"; 15f2a19305Safresh1 } 16f2a19305Safresh1 17f2a19305Safresh1 method print_message { 18f2a19305Safresh1 say $x; 19f2a19305Safresh1 } 20f2a19305Safresh1 } 21f2a19305Safresh1 22f2a19305Safresh1 My::Example->new->print_message; 23f2a19305Safresh1 24f2a19305Safresh1=head1 DESCRIPTION 25f2a19305Safresh1 26*5486feefSafresh1This document describes the syntax of Perl's C<class> feature, which provides 27*5486feefSafresh1native keywords for object-oriented programming. 28f2a19305Safresh1 29f2a19305Safresh1=head2 History 30f2a19305Safresh1 31f2a19305Safresh1Since Perl 5, support for objects revolved around the concept of I<blessing> 32*5486feefSafresh1references with a package name (see L<perlfunc/"bless REF,CLASSNAME">). Such a 33*5486feefSafresh1reference could then be used to call subroutines from the package it was 34*5486feefSafresh1blessed with (or any of its parents). This system, while bare-bones, was 35*5486feefSafresh1flexible enough to allow creation of multiple more advanced, community-driven 36*5486feefSafresh1systems for object orientation. For more information, see L<perlmod> and 37*5486feefSafresh1L<perlobj>. 38f2a19305Safresh1 39*5486feefSafresh1The C<class> feature is a core implementation of a class syntax that is similar 40*5486feefSafresh1to what one would find in other programming languages. It is not a wrapper 41*5486feefSafresh1around C<bless>, but a completely new system built right into the perl 42*5486feefSafresh1interpreter. 43f2a19305Safresh1 44f2a19305Safresh1=head1 KEYWORDS 45f2a19305Safresh1 46f2a19305Safresh1Enabling the C<class> feature allows the usage of the following new keywords in 47*5486feefSafresh1the current lexical scope: 48f2a19305Safresh1 49f2a19305Safresh1=head2 class 50f2a19305Safresh1 51f2a19305Safresh1 class NAME BLOCK 52f2a19305Safresh1 53f2a19305Safresh1 class NAME VERSION BLOCK 54f2a19305Safresh1 55*5486feefSafresh1 class NAME VERSION : ATTRIBUTES... BLOCK 56*5486feefSafresh1 57f2a19305Safresh1 class NAME; 58f2a19305Safresh1 59f2a19305Safresh1 class NAME VERSION; 60f2a19305Safresh1 61*5486feefSafresh1 class NAME VERSION : ATTRIBUTES...; 62*5486feefSafresh1 63*5486feefSafresh1The C<class> keyword declares a new package (see L<perlmod/Packages>) that is 64*5486feefSafresh1intended to be a class. All other keywords from the C<class> feature should be 65*5486feefSafresh1used within the scope of this declaration. 66f2a19305Safresh1 67f2a19305Safresh1 class WithVersion 1.000 { 68f2a19305Safresh1 # class definition goes here 69f2a19305Safresh1 } 70f2a19305Safresh1 71f2a19305Safresh1Classes can be declared in either block or statement syntax. If a block is 72f2a19305Safresh1used, the body of the block contains the implementation of the class. If the 73f2a19305Safresh1statement form is used, the remainder of the file is used up until the next 74f2a19305Safresh1C<class> or C<package> statement. 75f2a19305Safresh1 76*5486feefSafresh1A C<class> declaration can optionally have a version number, similar to the 77*5486feefSafresh1C<package> keyword. It can also optionally have attributes. If both are 78*5486feefSafresh1specified, the version number must come first, before the attributes. 79*5486feefSafresh1 80f2a19305Safresh1C<class> and C<package> declarations are similar, but classes automatically get 81*5486feefSafresh1a constructor named C<new> - you don't have to (and should not) write one. 82f2a19305Safresh1Additionally, in the class BLOCK you are allowed to declare fields and methods. 83f2a19305Safresh1 84f2a19305Safresh1=head2 field 85f2a19305Safresh1 86f2a19305Safresh1 field VARIABLE_NAME; 87f2a19305Safresh1 88f2a19305Safresh1 field VARIABLE_NAME = EXPR; 89f2a19305Safresh1 90f2a19305Safresh1 field VARIABLE_NAME : ATTRIBUTES; 91f2a19305Safresh1 92f2a19305Safresh1 field VARIABLE_NAME : ATTRIBUTES = EXPR; 93f2a19305Safresh1 94*5486feefSafresh1Fields are variables that are visible in the scope of the class - more 95*5486feefSafresh1specifically within L</method> and L<ADJUST|/Adjustment> blocks. Each class 96*5486feefSafresh1instance gets its own storage of fields, independent of other instances. 97f2a19305Safresh1 98f2a19305Safresh1A field behaves like a normal lexically scoped variable. It has a sigil and is 99f2a19305Safresh1private to the class (though creation of an accessor method will make it 100f2a19305Safresh1accessible from the outside). The main difference is that different instances 101f2a19305Safresh1access different values in the same scope. 102f2a19305Safresh1 103f2a19305Safresh1 class WithFields { 104f2a19305Safresh1 field $scalar = 42; 105f2a19305Safresh1 field @array = qw(this is just an array); 106f2a19305Safresh1 field %hash = (species => 'Martian', planet => 'Mars'); 107f2a19305Safresh1 } 108f2a19305Safresh1 109f2a19305Safresh1Fields may optionally have initializing expressions. If present, the expression 110f2a19305Safresh1will be evaluated within the constructor of each object instance. During each 111f2a19305Safresh1evaluation, the expression can use the value of any previously-set field, as 112*5486feefSafresh1well as any other variables in scope. 113f2a19305Safresh1 114f2a19305Safresh1 class WithACounter { 115f2a19305Safresh1 my $next_count = 1; 116f2a19305Safresh1 field $count = $next_count++; 117f2a19305Safresh1 } 118f2a19305Safresh1 119f2a19305Safresh1When combined with the C<:param> field attribute, the defaulting expression can 120f2a19305Safresh1use any of the C<=>, C<//=> or C<||=> operators. Expressions using C<=> will 121f2a19305Safresh1apply whenever the caller did not pass the corresponding parameter to the 122f2a19305Safresh1constructor at all. Expressions using C<//=> will also apply if the caller did 123f2a19305Safresh1pass the parameter but the value was undefined, and expressions using C<||=> 124f2a19305Safresh1will apply if the value was false. 125f2a19305Safresh1 126*5486feefSafresh1During a field initializing expression, the instance is not yet constructed 127*5486feefSafresh1and so the C<$self> lexical is not available. However, the special 128*5486feefSafresh1C<__CLASS__> token may be used to obtain the name of the class being 129*5486feefSafresh1constructed, for example in order to invoke class methods on it to help in 130*5486feefSafresh1constructing values for fields. 131*5486feefSafresh1 132*5486feefSafresh1 class WithCustomField { 133*5486feefSafresh1 use constant DEFAULT_X => 10; 134*5486feefSafresh1 field $x = __CLASS__->DEFAULT_X; 135*5486feefSafresh1 } 136*5486feefSafresh1 137*5486feefSafresh1This allows subclasses to override the method with different behaviour. 138*5486feefSafresh1 139*5486feefSafresh1 class DifferentCustomField :isa(WithCustomField) { 140*5486feefSafresh1 sub DEFAULT_X { rand > 0.5 ? 20 : 30 } 141*5486feefSafresh1 } 142*5486feefSafresh1 143*5486feefSafresh1When an instance of C<DifferentCustomField> is constructed, the C<__CLASS__> 144*5486feefSafresh1expression in the base will yield the correct class name, and so invoke this 145*5486feefSafresh1overridden method instead. 146*5486feefSafresh1 147f2a19305Safresh1=head2 method 148f2a19305Safresh1 149f2a19305Safresh1 method METHOD_NAME SIGNATURE BLOCK 150f2a19305Safresh1 151f2a19305Safresh1 method METHOD_NAME BLOCK 152f2a19305Safresh1 153f2a19305Safresh1 method SIGNATURE BLOCK 154f2a19305Safresh1 155f2a19305Safresh1 method BLOCK 156f2a19305Safresh1 157f2a19305Safresh1Methods are subroutines intended to be called in the context of class objects. 158f2a19305Safresh1 159f2a19305Safresh1A variable named C<$self> populated with the current object instance will 160f2a19305Safresh1automatically be created in the lexical scope of C<method>. 161f2a19305Safresh1 162f2a19305Safresh1Methods always act as if C<use feature 'signatures'> is in effect, but C<$self> 163f2a19305Safresh1will not appear in the arguments list as far as the signature is concerned. 164f2a19305Safresh1 165f2a19305Safresh1 class WithMethods { 166f2a19305Safresh1 field $greetings; 167f2a19305Safresh1 168f2a19305Safresh1 ADJUST { 169f2a19305Safresh1 $greetings = "Hello"; 170f2a19305Safresh1 } 171f2a19305Safresh1 172f2a19305Safresh1 method greet($name = "someone") { 173f2a19305Safresh1 say "$greetings, $name"; 174f2a19305Safresh1 } 175f2a19305Safresh1 } 176f2a19305Safresh1 177f2a19305Safresh1Just like regular subroutines, methods I<can> be anonymous: 178f2a19305Safresh1 179f2a19305Safresh1 class AnonMethodFactory { 180f2a19305Safresh1 181f2a19305Safresh1 method get_anon_method { 182f2a19305Safresh1 return method { 183f2a19305Safresh1 return 'this is an anonymous method'; 184f2a19305Safresh1 }; 185f2a19305Safresh1 } 186f2a19305Safresh1 } 187f2a19305Safresh1 188f2a19305Safresh1=head1 ATTRIBUTES 189f2a19305Safresh1 190f2a19305Safresh1Specific aspects of the keywords mentioned above are managed using 191f2a19305Safresh1I<attributes>. Attributes all start with a colon, and one or more of them can 192f2a19305Safresh1be appended after the item's name, separated by a space. 193f2a19305Safresh1 194f2a19305Safresh1=head2 Class attributes 195f2a19305Safresh1 196f2a19305Safresh1=head3 :isa 197f2a19305Safresh1 198f2a19305Safresh1Classes may inherit from B<one> superclass, by using the C<:isa> class 199f2a19305Safresh1attribute. 200f2a19305Safresh1 201f2a19305Safresh1 class Example::Base { ... } 202f2a19305Safresh1 203f2a19305Safresh1 class Example::Subclass :isa(Example::Base) { ... } 204f2a19305Safresh1 205f2a19305Safresh1Inherited methods are visible and may be invoked. Fields are always lexical 206f2a19305Safresh1and therefore not visible by inheritance. 207f2a19305Safresh1 208*5486feefSafresh1The C<:isa> attribute may request a minimum version of the base class. As with 209*5486feefSafresh1C<use MODULE VERSION>, if the actual version of the base class is too low, 210*5486feefSafresh1compilation will fail. 211f2a19305Safresh1 212f2a19305Safresh1 class Example::Subclass :isa(Example::Base 2.345) { ... } 213f2a19305Safresh1 214f2a19305Safresh1The C<:isa> attribute will attempt to C<require> the named module if it is not 215f2a19305Safresh1already loaded. 216f2a19305Safresh1 217f2a19305Safresh1=head2 Field attributes 218f2a19305Safresh1 219f2a19305Safresh1=head3 :param 220f2a19305Safresh1 221f2a19305Safresh1A scalar field with a C<:param> attribute will take its value from a named 222f2a19305Safresh1parameter passed to the constructor. By default the parameter will have the 223f2a19305Safresh1same name as the field (minus its leading C<$> sigil), but a different name 224f2a19305Safresh1can be specified in the attribute. 225f2a19305Safresh1 226f2a19305Safresh1 field $x :param; 227f2a19305Safresh1 field $y :param(the_y_value); 228f2a19305Safresh1 229*5486feefSafresh1If there is no defaulting expression, then the parameter is required by the 230f2a19305Safresh1constructor; the caller must pass it or an exception is thrown. With a 231f2a19305Safresh1defaulting expression this becomes optional. 232f2a19305Safresh1 233*5486feefSafresh1=head3 :reader 234*5486feefSafresh1 235*5486feefSafresh1A field with a C<:reader> attribute will generate a reader accessor method 236*5486feefSafresh1automatically. The generated method will have an empty (i.e. zero-argument) 237*5486feefSafresh1signature, and its body will simply return the value of the field variable. 238*5486feefSafresh1 239*5486feefSafresh1 field $s :reader; 240*5486feefSafresh1 241*5486feefSafresh1 # Equivalent to 242*5486feefSafresh1 field $s; 243*5486feefSafresh1 method s () { return $s; } 244*5486feefSafresh1 245*5486feefSafresh1By default the accessor method will have the same name as the field (minus the 246*5486feefSafresh1leading sigil), but a different name can be specified in the attribute's value. 247*5486feefSafresh1 248*5486feefSafresh1 field $x :reader(get_x); 249*5486feefSafresh1 250*5486feefSafresh1 # Generates a method 251*5486feefSafresh1 method get_x () { return $x; } 252*5486feefSafresh1 253*5486feefSafresh1Reader methods can be applied to non-scalar fields. When invoked in list 254*5486feefSafresh1context, they yield the contents of the field; in scalar context they yield 255*5486feefSafresh1the count of elements, as if the field variable had been placed in scalar 256*5486feefSafresh1context. 257*5486feefSafresh1 258*5486feefSafresh1 field @users :reader; 259*5486feefSafresh1 ... 260*5486feefSafresh1 261*5486feefSafresh1 scalar $instance->users; 262*5486feefSafresh1 263f2a19305Safresh1=head2 Method attributes 264f2a19305Safresh1 265f2a19305Safresh1None yet. 266f2a19305Safresh1 267f2a19305Safresh1=head1 OBJECT LIFECYCLE 268f2a19305Safresh1 269f2a19305Safresh1=head2 Construction 270f2a19305Safresh1 271f2a19305Safresh1Each object begins its life with a constructor call. The constructor is always 272f2a19305Safresh1named C<new> and is invoked like a method call on the class name: 273f2a19305Safresh1 274f2a19305Safresh1 my $object = My::Class->new(%arguments); 275f2a19305Safresh1 276*5486feefSafresh1During object construction, class fields are looked up in the C<%arguments> 277*5486feefSafresh1hash and populated where possible. 278f2a19305Safresh1 279f2a19305Safresh1=head2 Adjustment 280f2a19305Safresh1 281*5486feefSafresh1Object adjustment is a way to run arbitrary user-defined code during object 282*5486feefSafresh1construction. This is done by placing code in C<ADJUST> blocks. Every time an 283*5486feefSafresh1object is constructed, its C<ADJUST> blocks are executed (in the order in which 284*5486feefSafresh1they are declared). 285f2a19305Safresh1 286*5486feefSafresh1 class WellAdjusted { 287*5486feefSafresh1 field $x :param; 288*5486feefSafresh1 ADJUST { 289*5486feefSafresh1 say "Hello!"; 290*5486feefSafresh1 } 291*5486feefSafresh1 ADJUST { 292*5486feefSafresh1 say "x = $x"; 293*5486feefSafresh1 } 294*5486feefSafresh1 } 295*5486feefSafresh1 296*5486feefSafresh1 my $object = WellAdjusted->new(x => 42); 297*5486feefSafresh1 # Output: 298*5486feefSafresh1 # Hello! 299*5486feefSafresh1 # x = 42 300*5486feefSafresh1 301*5486feefSafresh1C<ADJUST> blocks are syntactically similar to L<C<BEGIN> or C<INIT> 302*5486feefSafresh1blocks|perlmod/BEGIN, UNITCHECK, CHECK, INIT and END>, which only run once. 303*5486feefSafresh1However, C<ADJUST> blocks, like methods, have access to C<$self> (a lexical 304*5486feefSafresh1variable holding the object being constructed) as well as all object fields 305*5486feefSafresh1created up to that point. 306f2a19305Safresh1 307f2a19305Safresh1=head2 Lifetime 308f2a19305Safresh1 309*5486feefSafresh1After the construction phase, the object is ready to be used. 310f2a19305Safresh1 311f2a19305Safresh1Using C<blessed> (C<Scalar::Util::blessed> or C<builtin::blessed>) on the 312f2a19305Safresh1object will return the name of the class, while C<reftype> 313f2a19305Safresh1(C<Scalar::Util::reftype> or C<builtin::reftype>) will return the string 314f2a19305Safresh1C<'OBJECT'>. 315f2a19305Safresh1 316f2a19305Safresh1=head2 Destruction 317f2a19305Safresh1 318*5486feefSafresh1An object is destroyed when the last reference to it goes away, just as with 319*5486feefSafresh1other data structures in Perl. 320f2a19305Safresh1 321f2a19305Safresh1=head1 TODO 322f2a19305Safresh1 323f2a19305Safresh1This feature is still experimental and very incomplete. The following list 324*5486feefSafresh1gives an overview of features still to be added or changed: 325f2a19305Safresh1 326f2a19305Safresh1=over 4 327f2a19305Safresh1 328f2a19305Safresh1=item * Roles 329f2a19305Safresh1 330f2a19305Safresh1Some syntax for declaring a role (likely a C<role> keyword), and for consuming 331f2a19305Safresh1a role into a class (likely a C<:does()> attribute). 332f2a19305Safresh1 333f2a19305Safresh1=item * Parameters to ADJUST blocks 334f2a19305Safresh1 335f2a19305Safresh1Some syntax for declaring that an C<ADJUST> block can consume named 336f2a19305Safresh1parameters, which become part of the class constructor's API. This might be 337f2a19305Safresh1inspired by a similar plan to add named arguments to subroutine signatures. 338f2a19305Safresh1 339f2a19305Safresh1 class X { 340f2a19305Safresh1 ADJUST (:$alpha, :$beta = 123) { 341f2a19305Safresh1 ... 342f2a19305Safresh1 } 343f2a19305Safresh1 } 344f2a19305Safresh1 345f2a19305Safresh1 my $obj = X->new(alpha => 456); 346f2a19305Safresh1 347f2a19305Safresh1=item * ADJUST blocks as true blocks 348f2a19305Safresh1 349*5486feefSafresh1Currently, every ADJUST block is wrapped in its own CV (subroutine) that gets 350*5486feefSafresh1invoked with the full ENTERSUB overhead. It should be possible to use the same 351*5486feefSafresh1mechanism that makes all field initializer expressions appear within the same 352*5486feefSafresh1CV on ADJUST blocks as well, merging them all into a single CV per class. This 353*5486feefSafresh1will make it faster to invoke if a class has more than one of them. 354f2a19305Safresh1 355*5486feefSafresh1=item * More accessor generator attributes 356f2a19305Safresh1 357*5486feefSafresh1Attributes to request that other kinds of accessor methods be generated for 358*5486feefSafresh1fields. Likely C<:writer>. 359f2a19305Safresh1 360f2a19305Safresh1 class X { 361*5486feefSafresh1 field $name :writer; 362f2a19305Safresh1 } 363f2a19305Safresh1 364f2a19305Safresh1Equivalent to 365f2a19305Safresh1 366f2a19305Safresh1 class X { 367f2a19305Safresh1 field $name; 368*5486feefSafresh1 method set_name ($new) { $name = $new; return $self; } 369f2a19305Safresh1 } 370f2a19305Safresh1 371f2a19305Safresh1=item * Metaprogramming 372f2a19305Safresh1 373f2a19305Safresh1An extension of the metaprogramming API (currently proposed by 374*5486feefSafresh1L<PPC0022|https://github.com/Perl/PPCs/pull/25>) which adds knowledge of 375f2a19305Safresh1classes, methods, fields, ADJUST blocks, and other such class-related details. 376f2a19305Safresh1 377f2a19305Safresh1=item * Extension Customisation 378f2a19305Safresh1 379f2a19305Safresh1Ways in which out-of-core modules can interact with the class system, 380f2a19305Safresh1including an ability for them to provide new class or field attributes. 381f2a19305Safresh1 382f2a19305Safresh1=back 383f2a19305Safresh1 384*5486feefSafresh1=head1 KNOWN BUGS 385*5486feefSafresh1 386*5486feefSafresh1The following bugs have been found in the experimental C<class> feature: 387*5486feefSafresh1 388*5486feefSafresh1=over 4 389*5486feefSafresh1 390*5486feefSafresh1=item * 391*5486feefSafresh1 392*5486feefSafresh1Since Perl v5.38, inheriting from a parent class which is declared in the same 393*5486feefSafresh1file and which hadn't already been sealed can cause a segmentation fault. 394*5486feefSafresh1[L<GH #20890|https://github.com/Perl/perl5/issues/20890>] 395*5486feefSafresh1 396*5486feefSafresh1=item * 397*5486feefSafresh1 398*5486feefSafresh1Since Perl v5.38 and with the experimental C<refaliasing> feature, trying to 399*5486feefSafresh1replace a field variable causes a segmentation fault. 400*5486feefSafresh1[L<GH #20947|https://github.com/Perl/perl5/issues/20947>] 401*5486feefSafresh1 402*5486feefSafresh1=item * 403*5486feefSafresh1 404*5486feefSafresh1Since Perl v5.38, it's possible to craft a class with leaky encapsulation, 405*5486feefSafresh1which can cause a segmentation fault. 406*5486feefSafresh1[L<GH #20956|https://github.com/Perl/perl5/issues/20956>] 407*5486feefSafresh1 408*5486feefSafresh1=item * 409*5486feefSafresh1 410*5486feefSafresh1In Perl v5.38, inheriting from a class would not always attempt to load the 411*5486feefSafresh1parent class (fixed in Perl v5.40). 412*5486feefSafresh1[L<GH #21332|https://github.com/Perl/perl5/issues/21332>] 413*5486feefSafresh1 414*5486feefSafresh1=back 415*5486feefSafresh1 416f2a19305Safresh1=head1 AUTHORS 417f2a19305Safresh1 418f2a19305Safresh1Paul Evans 419f2a19305Safresh1 420f2a19305Safresh1Bartosz Jarzyna 421f2a19305Safresh1 422f2a19305Safresh1=cut 423