| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||
| 3 | # | ||||||||||||||
| 4 | # (C) Paul Evans, 2019-2025 -- leonerd@leonerd.org.uk | ||||||||||||||
| 5 | |||||||||||||||
| 6 | package Object::Pad 0.825; | ||||||||||||||
| 7 | |||||||||||||||
| 8 | 58 | ▶ | use v5.18; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 58 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 58 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 58 | ▶ | sub dl_load_flags { 0x01 } | ||||||||||||
|
Subroutine
dl_load_flags: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 58 | require DynaLoader; | |||||||||||||
| 16 | 58 | __PACKAGE__->DynaLoader::bootstrap( our $VERSION ); | |||||||||||||
| 17 | |||||||||||||||
| 18 | 58 | our $XSAPI_VERSION = "0.48"; | |||||||||||||
| 19 | |||||||||||||||
| 20 | # So that feature->import will work in `class` | ||||||||||||||
| 21 | 58 | require feature; | |||||||||||||
| 22 | 58 | ▶ | if( $] >= 5.020 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 23 | 58 | require experimental; | |||||||||||||
| 24 | 58 | ▶ | require indirect if $] < 5.031009; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | } | ||||||||||||||
| 26 | |||||||||||||||
| 27 | 58 | require mro; | |||||||||||||
| 28 | |||||||||||||||
| 29 | 58 | require Object::Pad::MOP::Class; | |||||||||||||
| 30 | |||||||||||||||
| 31 | =encoding UTF-8 | ||||||||||||||
| 32 | |||||||||||||||
| 33 | =for highlighter language=perl | ||||||||||||||
| 34 | |||||||||||||||
| 35 | =head1 NAME | ||||||||||||||
| 36 | |||||||||||||||
| 37 | C<Object::Pad> - a simple syntax for lexical field-based objects | ||||||||||||||
| 38 | |||||||||||||||
| 39 | =head1 SYNOPSIS | ||||||||||||||
| 40 | |||||||||||||||
| 41 | On perl version 5.26 onwards: | ||||||||||||||
| 42 | |||||||||||||||
| 43 | use v5.26; | ||||||||||||||
| 44 | use Object::Pad; | ||||||||||||||
| 45 | |||||||||||||||
| 46 | class Point { | ||||||||||||||
| 47 | field $x :param = 0; | ||||||||||||||
| 48 | field $y :param = 0; | ||||||||||||||
| 49 | |||||||||||||||
| 50 | method move ($dX, $dY) { | ||||||||||||||
| 51 | $x += $dX; | ||||||||||||||
| 52 | $y += $dY; | ||||||||||||||
| 53 | } | ||||||||||||||
| 54 | |||||||||||||||
| 55 | method describe () { | ||||||||||||||
| 56 | print "A point at ($x, $y)\n"; | ||||||||||||||
| 57 | } | ||||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | Point->new(x => 5, y => 10)->describe; | ||||||||||||||
| 61 | |||||||||||||||
| 62 | Or, for older perls that lack signatures: | ||||||||||||||
| 63 | |||||||||||||||
| 64 | use Object::Pad; | ||||||||||||||
| 65 | |||||||||||||||
| 66 | class Point { | ||||||||||||||
| 67 | field $x :param = 0; | ||||||||||||||
| 68 | field $y :param = 0; | ||||||||||||||
| 69 | |||||||||||||||
| 70 | method move { | ||||||||||||||
| 71 | my ($dX, $dY) = @_; | ||||||||||||||
| 72 | $x += $dX; | ||||||||||||||
| 73 | $y += $dY; | ||||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | method describe { | ||||||||||||||
| 77 | print "A point at ($x, $y)\n"; | ||||||||||||||
| 78 | } | ||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | Point->new(x => 5, y => 10)->describe; | ||||||||||||||
| 82 | |||||||||||||||
| 83 | =head1 DESCRIPTION | ||||||||||||||
| 84 | |||||||||||||||
| 85 | This module provides a simple syntax for creating object classes, which uses | ||||||||||||||
| 86 | private variables that look like lexicals as object member fields. | ||||||||||||||
| 87 | |||||||||||||||
| 88 | =head2 Relation to C<use feature 'class'> | ||||||||||||||
| 89 | |||||||||||||||
| 90 | Since its original creation, this module has evolved to become the test-bed | ||||||||||||||
| 91 | for refining the design of what a good class-based object system would look | ||||||||||||||
| 92 | like for Perl. While useful in its own right, it also acts as a preview of | ||||||||||||||
| 93 | what Perl's C<use feature 'class'> syntax will likely become. Being a CPAN | ||||||||||||||
| 94 | module, this can be iterated much faster than core Perl, which only gets one | ||||||||||||||
| 95 | major release per year. Getting the design right in this module first | ||||||||||||||
| 96 | therefore helps to reduce the amount of development time needed in core Perl. | ||||||||||||||
| 97 | |||||||||||||||
| 98 | Where features overlap, both this module and the C<class> feature added to | ||||||||||||||
| 99 | recent Perl versions should largely agree on syntax and semantics. This module | ||||||||||||||
| 100 | will likely always be ahead of core Perl in terms of exact abilties because | ||||||||||||||
| 101 | it is acting as a test-bed for these new ideas. However, where both | ||||||||||||||
| 102 | implementations support the same idea, they should in general use the same | ||||||||||||||
| 103 | syntax and semantics to provide it. | ||||||||||||||
| 104 | |||||||||||||||
| 105 | If you are interested in using this object system in new code in a way that | ||||||||||||||
| 106 | will be forward-compatible with the feature added directly in newer Perl | ||||||||||||||
| 107 | versions, while also supporting versions of Perl before this feature was | ||||||||||||||
| 108 | added, you may wish to use instead the module L<Feature::Compat::Class>. That | ||||||||||||||
| 109 | module enables the core Perl feature on the latest version of Perl, or uses | ||||||||||||||
| 110 | C<Object::Pad> to fill in the missing syntax and features on older Perl | ||||||||||||||
| 111 | versions. | ||||||||||||||
| 112 | |||||||||||||||
| 113 | =head2 Experimental Features | ||||||||||||||
| 114 | |||||||||||||||
| 115 | I<Since version 0.63.> | ||||||||||||||
| 116 | |||||||||||||||
| 117 | While most of this module has evolved into a stable state, recently-added | ||||||||||||||
| 118 | parts remain B<experimental> because the design is still evolving, and many | ||||||||||||||
| 119 | features and ideas have yet to implemented. Of these experimental features, | ||||||||||||||
| 120 | feel free to try them out in newly-developed code, but don't complain if a | ||||||||||||||
| 121 | later version is incompatible with your current code and you'll have to change | ||||||||||||||
| 122 | it. Once individual features are stable and no longer declared experimental, | ||||||||||||||
| 123 | they should have the same stability guarantees as any other long-term | ||||||||||||||
| 124 | supported CPAN module, or core Perl itself. | ||||||||||||||
| 125 | |||||||||||||||
| 126 | That all said, please do get in contact if you find the module overall useful. | ||||||||||||||
| 127 | The more feedback you provide in terms of what features you are using, what | ||||||||||||||
| 128 | you find works, and what doesn't, will help the ongoing development and | ||||||||||||||
| 129 | hopefully eventual stability of the design - both within this CPAN module and | ||||||||||||||
| 130 | as the wider experiment to provide it as a core Perl feature. See the | ||||||||||||||
| 131 | L</FEEDBACK> section. | ||||||||||||||
| 132 | |||||||||||||||
| 133 | Features of this module that are currently marked as experimental provoke | ||||||||||||||
| 134 | warnings in the C<experimental> category, unless silenced. You can silence | ||||||||||||||
| 135 | this with C<no warnings 'experimental'> but then that will silence every | ||||||||||||||
| 136 | experimental warning, which may hide others unintentionally. For a more | ||||||||||||||
| 137 | fine-grained approach you can instead use the import line for this module to | ||||||||||||||
| 138 | only silence the module's warnings selectively: | ||||||||||||||
| 139 | |||||||||||||||
| 140 | use Object::Pad ':experimental(mop)'; | ||||||||||||||
| 141 | |||||||||||||||
| 142 | use Object::Pad ':experimental(custom_field_attr)'; | ||||||||||||||
| 143 | |||||||||||||||
| 144 | use Object::Pad ':experimental(composed_adjust)'; | ||||||||||||||
| 145 | |||||||||||||||
| 146 | use Object::Pad ':experimental(inherit_field)'; | ||||||||||||||
| 147 | |||||||||||||||
| 148 | use Object::Pad ':experimental(apply_phaser)'; | ||||||||||||||
| 149 | |||||||||||||||
| 150 | use Object::Pad ':experimental(lexical_class)'; | ||||||||||||||
| 151 | |||||||||||||||
| 152 | use Object::Pad ':experimental(:all)'; # all of the above | ||||||||||||||
| 153 | |||||||||||||||
| 154 | I<Since version 0.64.> | ||||||||||||||
| 155 | |||||||||||||||
| 156 | Multiple experimental features can be enabled at once by giving multiple names | ||||||||||||||
| 157 | in the parens, separated by spaces: | ||||||||||||||
| 158 | |||||||||||||||
| 159 | use Object::Pad ':experimental(mop custom_field_attr)'; | ||||||||||||||
| 160 | |||||||||||||||
| 161 | I<Since version 0.810> attempting to request all of the experiments at once | ||||||||||||||
| 162 | by using an empty C<:experimental()> is currently accepted, but yields a | ||||||||||||||
| 163 | warning. This may be removed in future. | ||||||||||||||
| 164 | |||||||||||||||
| 165 | =head2 Automatic Construction | ||||||||||||||
| 166 | |||||||||||||||
| 167 | Classes are automatically provided with a constructor method, called C<new>, | ||||||||||||||
| 168 | which helps create the object instances. This may respond to passed arguments, | ||||||||||||||
| 169 | automatically assigning values of fields, and invoking other blocks of code | ||||||||||||||
| 170 | provided by the class. It proceeds in the following stages: | ||||||||||||||
| 171 | |||||||||||||||
| 172 | =head3 The BUILDARGS phase | ||||||||||||||
| 173 | |||||||||||||||
| 174 | If the class provides a C<BUILDARGS> class method, that is used to mangle the | ||||||||||||||
| 175 | list of arguments before the C<BUILD> blocks are called. Note this must be a | ||||||||||||||
| 176 | class method not an instance method (and so implemented using C<sub>). It | ||||||||||||||
| 177 | should perform any C<SUPER> chaining as may be required. | ||||||||||||||
| 178 | |||||||||||||||
| 179 | @args = $class->BUILDARGS( @_ ) | ||||||||||||||
| 180 | |||||||||||||||
| 181 | =head3 Field assignment | ||||||||||||||
| 182 | |||||||||||||||
| 183 | If any field in the class has the C<:param> attribute, then the constructor | ||||||||||||||
| 184 | will expect to receive its argmuents in an even-sized list of name/value | ||||||||||||||
| 185 | pairs. This applies even to fields inherited from the parent class or applied | ||||||||||||||
| 186 | roles. It is therefore a good idea to shape the parameters to the constructor | ||||||||||||||
| 187 | in this way in roles, and in classes if you intend your class to be extended. | ||||||||||||||
| 188 | |||||||||||||||
| 189 | The constructor will also check for required parameters (these are all the | ||||||||||||||
| 190 | parameters for fields that do not have default initialisation expressions). If | ||||||||||||||
| 191 | any of these are missing an exception is thrown. | ||||||||||||||
| 192 | |||||||||||||||
| 193 | =head3 The BUILD phase | ||||||||||||||
| 194 | |||||||||||||||
| 195 | As part of the construction process, every C<BUILD> phaser of every component | ||||||||||||||
| 196 | class will be invoked. Each will be passed in the list of arguments the | ||||||||||||||
| 197 | constructor was invoked with as arguments. Each class should perform its | ||||||||||||||
| 198 | required setup behaviour, but does not need to chain to the C<SUPER> class | ||||||||||||||
| 199 | first; this is handled automatically. | ||||||||||||||
| 200 | |||||||||||||||
| 201 | =head3 The ADJUST phase | ||||||||||||||
| 202 | |||||||||||||||
| 203 | Next, every C<ADJUST> phaser of every component class is invoked. This happens | ||||||||||||||
| 204 | after the fields are assigned their initial values and the C<BUILD> blocks | ||||||||||||||
| 205 | have been run. | ||||||||||||||
| 206 | |||||||||||||||
| 207 | =head3 The strict-checking phase | ||||||||||||||
| 208 | |||||||||||||||
| 209 | Finally, before the object is returned, if the L</:strict(params)> class | ||||||||||||||
| 210 | attribute is present, then the constructor will throw an exception if there | ||||||||||||||
| 211 | are any remaining named arguments left over after assigning them to fields as | ||||||||||||||
| 212 | per C<:param> declarations, and running any C<ADJUST> blocks. | ||||||||||||||
| 213 | |||||||||||||||
| 214 | =head1 KEYWORDS | ||||||||||||||
| 215 | |||||||||||||||
| 216 | =head2 class | ||||||||||||||
| 217 | |||||||||||||||
| 218 | class Name :ATTRS... { | ||||||||||||||
| 219 | ... | ||||||||||||||
| 220 | } | ||||||||||||||
| 221 | |||||||||||||||
| 222 | class Name :ATTRS...; | ||||||||||||||
| 223 | |||||||||||||||
| 224 | Behaves similarly to the C<package> keyword, but provides a package that | ||||||||||||||
| 225 | defines a new class. Such a class provides an automatic constructor method | ||||||||||||||
| 226 | called C<new>. | ||||||||||||||
| 227 | |||||||||||||||
| 228 | As with C<package>, an optional block may be provided. If so, the contents of | ||||||||||||||
| 229 | that block define the new class and the preceding package continues | ||||||||||||||
| 230 | afterwards. If not, it sets the class as the package context of following | ||||||||||||||
| 231 | keywords and definitions. | ||||||||||||||
| 232 | |||||||||||||||
| 233 | As with C<package>, an optional version declaration may be given. If so, this | ||||||||||||||
| 234 | sets the value of the package's C<$VERSION> variable. | ||||||||||||||
| 235 | |||||||||||||||
| 236 | class Name VERSION { ... } | ||||||||||||||
| 237 | |||||||||||||||
| 238 | class Name VERSION; | ||||||||||||||
| 239 | |||||||||||||||
| 240 | An optional list of attributes may be supplied in similar syntax as for subs | ||||||||||||||
| 241 | or lexical variables. (These are annotations about the class itself; the | ||||||||||||||
| 242 | concept should not be confused with per-object-instance data, which here is | ||||||||||||||
| 243 | called "fields"). | ||||||||||||||
| 244 | |||||||||||||||
| 245 | Whitespace is permitted within the value and is automatically trimmed, but as | ||||||||||||||
| 246 | standard Perl parsing rules, no space is permitted between the attribute's | ||||||||||||||
| 247 | name and the open parenthesis of its value: | ||||||||||||||
| 248 | |||||||||||||||
| 249 | :attr( value here ) # is permitted | ||||||||||||||
| 250 | :attr (value here) # not permitted | ||||||||||||||
| 251 | |||||||||||||||
| 252 | The following class attributes are supported: | ||||||||||||||
| 253 | |||||||||||||||
| 254 | =head3 :isa | ||||||||||||||
| 255 | |||||||||||||||
| 256 | :isa(CLASS) | ||||||||||||||
| 257 | |||||||||||||||
| 258 | :isa(CLASS CLASSVER) | ||||||||||||||
| 259 | |||||||||||||||
| 260 | I<Since version 0.57.> | ||||||||||||||
| 261 | |||||||||||||||
| 262 | Declares a superclass that this class extends. At most one superclass is | ||||||||||||||
| 263 | supported. | ||||||||||||||
| 264 | |||||||||||||||
| 265 | If the package providing the superclass does not exist, an attempt is made to | ||||||||||||||
| 266 | load it by code equivalent to | ||||||||||||||
| 267 | |||||||||||||||
| 268 | require CLASS; | ||||||||||||||
| 269 | |||||||||||||||
| 270 | and thus it must either already exist, or be locatable via the usual C<@INC> | ||||||||||||||
| 271 | mechanisms. | ||||||||||||||
| 272 | |||||||||||||||
| 273 | The superclass may or may not itself be implemented by C<Object::Pad>, but if | ||||||||||||||
| 274 | it is not then see L<SUBCLASSING CLASSIC PERL CLASSES> for further detail on | ||||||||||||||
| 275 | the semantics of how this operates. | ||||||||||||||
| 276 | |||||||||||||||
| 277 | An optional version check can also be supplied; it performs the equivalent of | ||||||||||||||
| 278 | |||||||||||||||
| 279 | BaseClass->VERSION( $ver ) | ||||||||||||||
| 280 | |||||||||||||||
| 281 | =head3 :does | ||||||||||||||
| 282 | |||||||||||||||
| 283 | :does(ROLE) | ||||||||||||||
| 284 | |||||||||||||||
| 285 | :does(ROLE ROLEVER) | ||||||||||||||
| 286 | |||||||||||||||
| 287 | I<Since version 0.57.> | ||||||||||||||
| 288 | |||||||||||||||
| 289 | Composes a role into the class; optionally requiring a version check on the | ||||||||||||||
| 290 | role package. | ||||||||||||||
| 291 | |||||||||||||||
| 292 | Multiple roles can be composed by using multiple C<:does> attributes, one per | ||||||||||||||
| 293 | role. | ||||||||||||||
| 294 | |||||||||||||||
| 295 | The package will be loaded in a similar way to how the L</:isa> attribute is | ||||||||||||||
| 296 | handled. | ||||||||||||||
| 297 | |||||||||||||||
| 298 | =head3 :abstract | ||||||||||||||
| 299 | |||||||||||||||
| 300 | :abstract | ||||||||||||||
| 301 | |||||||||||||||
| 302 | I<Since version 0.817.> | ||||||||||||||
| 303 | |||||||||||||||
| 304 | Declares that this class is I<abstract>. An abstract class is permitted to | ||||||||||||||
| 305 | define required methods (i.e. named methods without a body definition). | ||||||||||||||
| 306 | |||||||||||||||
| 307 | Instances may not be created of this class type directly. Instead, a subclass | ||||||||||||||
| 308 | of this class must be derived that provides method bodies for any of the named | ||||||||||||||
| 309 | required methods. Any subclass of an abstract class must either provide bodies | ||||||||||||||
| 310 | for all remaining required methods, or themselves be declared C<:abstract> as | ||||||||||||||
| 311 | well. | ||||||||||||||
| 312 | |||||||||||||||
| 313 | This provides a way in which a generic type of class can be created, that is | ||||||||||||||
| 314 | intended for multiple different specialisations to further define different | ||||||||||||||
| 315 | kinds of behaviour by providing bodies for those named methods. Being abstract | ||||||||||||||
| 316 | means that instances cannot be created of this as-yet-incomplete class type. | ||||||||||||||
| 317 | |||||||||||||||
| 318 | =head3 :lexical_new | ||||||||||||||
| 319 | |||||||||||||||
| 320 | :lexical_new | ||||||||||||||
| 321 | |||||||||||||||
| 322 | I<Since version 0.822.> | ||||||||||||||
| 323 | |||||||||||||||
| 324 | Causes the constructor for this class to be made available to the class body | ||||||||||||||
| 325 | lexically, and I<not> installed as a symbol in the package's symbol table. The | ||||||||||||||
| 326 | effect of this is that code inside the class can make use of it as a lexical | ||||||||||||||
| 327 | function, but it is not available to callers outside the class. In this way, | ||||||||||||||
| 328 | a wrapper method in the class itself is fully in control of how, and when, | ||||||||||||||
| 329 | instances of the class are constructed. | ||||||||||||||
| 330 | |||||||||||||||
| 331 | For example, a singleton class could be created by providing a wrapper | ||||||||||||||
| 332 | class method that lazily creates a single instance stored in a lexical | ||||||||||||||
| 333 | variable in the class, ensuring every call returns the same instance. | ||||||||||||||
| 334 | |||||||||||||||
| 335 | class A::Singleton :lexical_new { | ||||||||||||||
| 336 | my $instance; | ||||||||||||||
| 337 | |||||||||||||||
| 338 | method instance :common { $instance //= new(shift); } | ||||||||||||||
| 339 | |||||||||||||||
| 340 | ... | ||||||||||||||
| 341 | } | ||||||||||||||
| 342 | |||||||||||||||
| 343 | Note also that since methods are I<not> considered when resolving bareword | ||||||||||||||
| 344 | function call names, it is possible to provide a C<:common> class method | ||||||||||||||
| 345 | called C<new>, which can wrap the default provided constructor. This can be | ||||||||||||||
| 346 | useful for providing a constructor whose arguments are passed in some style | ||||||||||||||
| 347 | other than the usual name/value pairs. | ||||||||||||||
| 348 | |||||||||||||||
| 349 | For example, if a class only has one parameter it is sometimes common style | ||||||||||||||
| 350 | to simply pass that one value as the only constructor parameter. | ||||||||||||||
| 351 | |||||||||||||||
| 352 | class Path :lexical_new { | ||||||||||||||
| 353 | field $filename :param :reader; | ||||||||||||||
| 354 | |||||||||||||||
| 355 | method new :common ($filename) { | ||||||||||||||
| 356 | return &new( $class, filename => $filename ); | ||||||||||||||
| 357 | } | ||||||||||||||
| 358 | } | ||||||||||||||
| 359 | |||||||||||||||
| 360 | my $null = Path->new( "/dev/null" ); | ||||||||||||||
| 361 | |||||||||||||||
| 362 | Note carefully that to do this you must use C<method>. You cannot use C<sub> | ||||||||||||||
| 363 | here as that will redefine the content of the I<lexical> sub C<new>, rather | ||||||||||||||
| 364 | than creating a new package-named one. | ||||||||||||||
| 365 | |||||||||||||||
| 366 | =head3 :repr(TYPE) | ||||||||||||||
| 367 | |||||||||||||||
| 368 | Sets the representation type for instances of this class. Must be one of the | ||||||||||||||
| 369 | following values: | ||||||||||||||
| 370 | |||||||||||||||
| 371 | :repr(native) | ||||||||||||||
| 372 | |||||||||||||||
| 373 | The native representation. This is an opaque representation type whose | ||||||||||||||
| 374 | contents are not specified. It only works for classes whose entire inheritance | ||||||||||||||
| 375 | hierarchy is built only from classes based on C<Object::Pad>. | ||||||||||||||
| 376 | |||||||||||||||
| 377 | :repr(HASH) | ||||||||||||||
| 378 | |||||||||||||||
| 379 | The representation will be a blessed hash reference. The instance data will | ||||||||||||||
| 380 | be stored in an array referenced by a key called C<Object::Pad/slots>, which | ||||||||||||||
| 381 | is fairly unlikely to clash with existing storage on the instance. No other | ||||||||||||||
| 382 | keys will be used; they are available for implementions and subclasses to use. | ||||||||||||||
| 383 | The exact format of the value stored here is not specified and may change | ||||||||||||||
| 384 | between module versions, though it can be relied on to be well-behaved as some | ||||||||||||||
| 385 | kind of perl data structure for purposes of modules like L<Data::Dumper> or | ||||||||||||||
| 386 | serialisation into things like C<YAML> or C<JSON>. | ||||||||||||||
| 387 | |||||||||||||||
| 388 | :repr(keys) | ||||||||||||||
| 389 | |||||||||||||||
| 390 | I<Since version 0.803.> | ||||||||||||||
| 391 | |||||||||||||||
| 392 | The representation will be a blessed hash reference. The instance data will | ||||||||||||||
| 393 | be stored in individual keys of the hash, named after the class and the field | ||||||||||||||
| 394 | variable name, separated by a C</> symbol. Objects in this representation | ||||||||||||||
| 395 | should behave predictably with data printing modules like L<Data::Dumper> or | ||||||||||||||
| 396 | serialisation via C<YAML> or C<JSON>. | ||||||||||||||
| 397 | |||||||||||||||
| 398 | These two hash-based representation types may be useful when converting | ||||||||||||||
| 399 | existing classes into using C<Object::Pad> where there may be existing | ||||||||||||||
| 400 | subclasses of it that presume a blessed hash for their own use. | ||||||||||||||
| 401 | |||||||||||||||
| 402 | :repr(magic) | ||||||||||||||
| 403 | |||||||||||||||
| 404 | The representation will use MAGIC to apply the instance data in a way that is | ||||||||||||||
| 405 | invisible at the Perl level, and shouldn't get in the way of other things the | ||||||||||||||
| 406 | instance is doing even in XS modules. | ||||||||||||||
| 407 | |||||||||||||||
| 408 | This representation type is the only one that will work for subclassing | ||||||||||||||
| 409 | existing classes that do not use blessed hashes. | ||||||||||||||
| 410 | |||||||||||||||
| 411 | :repr(pvobj) | ||||||||||||||
| 412 | |||||||||||||||
| 413 | I<Since version 0.804.> | ||||||||||||||
| 414 | |||||||||||||||
| 415 | The representation will be the C<SVt_PVOBJ> type newly added to Perl, which | ||||||||||||||
| 416 | offers more efficient storage for object instances. This is only available on | ||||||||||||||
| 417 | Perl version 5.38.0 onwards. | ||||||||||||||
| 418 | |||||||||||||||
| 419 | This is also newly-added and may not be fully tested and reliable yet. Once it | ||||||||||||||
| 420 | has more real-world testing and has proven reliable it may become the default | ||||||||||||||
| 421 | instance representation on versions of Perl where it is available. | ||||||||||||||
| 422 | |||||||||||||||
| 423 | :repr(autoselect), :repr(default) | ||||||||||||||
| 424 | |||||||||||||||
| 425 | I<Since version 0.23.> | ||||||||||||||
| 426 | |||||||||||||||
| 427 | This representation will select one of the representations above depending on | ||||||||||||||
| 428 | what is best for the situation. Classes not derived from a non-C<Object::Pad> | ||||||||||||||
| 429 | base class will pick C<native>, and classes derived from non-C<Object::Pad> | ||||||||||||||
| 430 | bases will pick either the C<HASH> or C<magic> forms depending on whether the | ||||||||||||||
| 431 | instance is a blessed hash reference or some other kind. | ||||||||||||||
| 432 | |||||||||||||||
| 433 | This achieves the best combination of DWIM while still allowing the common | ||||||||||||||
| 434 | forms of hash reference to be inspected by C<Data::Dumper>, etc. This is the | ||||||||||||||
| 435 | default representation type, and does not have to be specifically requested. | ||||||||||||||
| 436 | |||||||||||||||
| 437 | =head3 :strict(params) | ||||||||||||||
| 438 | |||||||||||||||
| 439 | I<Since version 0.43.> | ||||||||||||||
| 440 | |||||||||||||||
| 441 | Can only be applied to classes that contain no C<BUILD> phasers. If set, then | ||||||||||||||
| 442 | the constructor will complain about any unrecognised named arguments passed to | ||||||||||||||
| 443 | it (i.e. names that do not correspond to the C<:param> of any defined field | ||||||||||||||
| 444 | and left unconsumed by any C<ADJUST> phaser). | ||||||||||||||
| 445 | |||||||||||||||
| 446 | Since C<BUILD> phasers can inspect the arguments arbitrarily, the presence of | ||||||||||||||
| 447 | any such phaser means the constructor cannot determine which named arguments | ||||||||||||||
| 448 | are not recognised. | ||||||||||||||
| 449 | |||||||||||||||
| 450 | This attribute is a temporary stepping-stone for compatibility with existing | ||||||||||||||
| 451 | code. It is recommended to enable this whenever possible, as a later version | ||||||||||||||
| 452 | of this module will likely perform this behaviour unconditionally whenever no | ||||||||||||||
| 453 | C<BUILD> phasers are present. | ||||||||||||||
| 454 | |||||||||||||||
| 455 | =head2 class (anon) | ||||||||||||||
| 456 | |||||||||||||||
| 457 | my $class = class :ATTRS... { ... }; | ||||||||||||||
| 458 | |||||||||||||||
| 459 | I<Since version 0.809.> | ||||||||||||||
| 460 | |||||||||||||||
| 461 | If a C<class> keyword is not followed by a package name, it creates an | ||||||||||||||
| 462 | anonymous class expression. This is an expression that yields a value suitable | ||||||||||||||
| 463 | to use as a constructor invocant for creating instances of that class, without | ||||||||||||||
| 464 | specifying what its package name will actually be. | ||||||||||||||
| 465 | |||||||||||||||
| 466 | This is useful for creating small one-off instances inline in expressions, | ||||||||||||||
| 467 | such as in unit tests. Since it still accepts the usual attributes and inner | ||||||||||||||
| 468 | body statements, it can be useful for creating one-off instances of roles, | ||||||||||||||
| 469 | with required methods being applied. | ||||||||||||||
| 470 | |||||||||||||||
| 471 | my $testobj = (class { | ||||||||||||||
| 472 | apply Role::Under::Test; | ||||||||||||||
| 473 | method required { return "a useful value"; } | ||||||||||||||
| 474 | })->new; | ||||||||||||||
| 475 | |||||||||||||||
| 476 | Due to limitations on how classes work in Perl, anonymous classes are still | ||||||||||||||
| 477 | backed by long-lived named classes in the global symbol table, unlike true | ||||||||||||||
| 478 | anonymous functions which can go out of scope and be reclaimed once no | ||||||||||||||
| 479 | references to them remain in existence. This means that anonymous classes will | ||||||||||||||
| 480 | retain references to any variables captured within them, even if the class | ||||||||||||||
| 481 | expression itself goes out of scope and any instances created by it no longer | ||||||||||||||
| 482 | remain. | ||||||||||||||
| 483 | |||||||||||||||
| 484 | =head2 role | ||||||||||||||
| 485 | |||||||||||||||
| 486 | role Name :ATTRS... { | ||||||||||||||
| 487 | ... | ||||||||||||||
| 488 | } | ||||||||||||||
| 489 | |||||||||||||||
| 490 | role Name :ATTRS...; | ||||||||||||||
| 491 | |||||||||||||||
| 492 | I<Since version 0.32.> | ||||||||||||||
| 493 | |||||||||||||||
| 494 | Similar to C<class>, but provides a package that defines a new role. A role | ||||||||||||||
| 495 | acts similar to a class in some respects, and differently in others. | ||||||||||||||
| 496 | |||||||||||||||
| 497 | Like a class, a role can have a version, and named methods. | ||||||||||||||
| 498 | |||||||||||||||
| 499 | role Name VERSION { | ||||||||||||||
| 500 | method a { ... } | ||||||||||||||
| 501 | method b { ... } | ||||||||||||||
| 502 | } | ||||||||||||||
| 503 | |||||||||||||||
| 504 | A role does not provide a constructor, and instances cannot directly be | ||||||||||||||
| 505 | constructed. A role cannot extend a class. | ||||||||||||||
| 506 | |||||||||||||||
| 507 | A role can declare that it requires methods of given names from any class that | ||||||||||||||
| 508 | implements the role. | ||||||||||||||
| 509 | |||||||||||||||
| 510 | role Name { | ||||||||||||||
| 511 | requires METHOD; | ||||||||||||||
| 512 | } | ||||||||||||||
| 513 | |||||||||||||||
| 514 | A role can provide instance fields. These are visible to any C<ADJUST> phasers | ||||||||||||||
| 515 | or methods provided by that role. | ||||||||||||||
| 516 | |||||||||||||||
| 517 | I<Since version 0.33.> | ||||||||||||||
| 518 | |||||||||||||||
| 519 | role Name { | ||||||||||||||
| 520 | field $f; | ||||||||||||||
| 521 | |||||||||||||||
| 522 | ADJUST { $f = "a value"; } | ||||||||||||||
| 523 | |||||||||||||||
| 524 | method f { return $f; } | ||||||||||||||
| 525 | } | ||||||||||||||
| 526 | |||||||||||||||
| 527 | I<Since version 0.57> a role can declare that it provides another role: | ||||||||||||||
| 528 | |||||||||||||||
| 529 | role Name :does(OTHERROLE) { ... } | ||||||||||||||
| 530 | role Name :does(OTHERROLE OTHERVER) { ... } | ||||||||||||||
| 531 | |||||||||||||||
| 532 | This will include all of the methods from the included role. Effectively this | ||||||||||||||
| 533 | means that applying the "outer" role to a class will imply applying the other | ||||||||||||||
| 534 | role as well. | ||||||||||||||
| 535 | |||||||||||||||
| 536 | The following role attributes are supported: | ||||||||||||||
| 537 | |||||||||||||||
| 538 | =head3 :compat(invokable) | ||||||||||||||
| 539 | |||||||||||||||
| 540 | I<Since version 0.35.> | ||||||||||||||
| 541 | |||||||||||||||
| 542 | Enables a form of backward-compatibility behaviour useful for gradually | ||||||||||||||
| 543 | upgrading existing code from classical Perl inheritance or mixins into using | ||||||||||||||
| 544 | roles. | ||||||||||||||
| 545 | |||||||||||||||
| 546 | Normally, methods of a role cannot be directly invoked and the role must be | ||||||||||||||
| 547 | applied to an L<Object::Pad>-based class in order to be used. This however | ||||||||||||||
| 548 | presents a problem when gradually upgrading existing code that already uses | ||||||||||||||
| 549 | techniques like roles, multiple inheritance or mixins when that code may be | ||||||||||||||
| 550 | split across multiple distributions, or for some other reason cannot be | ||||||||||||||
| 551 | upgraded all at once. Methods within a role that has the C<:compat(invokable)> | ||||||||||||||
| 552 | attribute applied to it may be directly invoked on any object instance. This | ||||||||||||||
| 553 | allows the creation of a role that can still provide code for existing classes | ||||||||||||||
| 554 | written in classical Perl that has not yet been rewritten to use | ||||||||||||||
| 555 | C<Object::Pad>. | ||||||||||||||
| 556 | |||||||||||||||
| 557 | The tradeoff is that a C<:compat(invokable)> role may not create field data | ||||||||||||||
| 558 | using the L</field> keyword. Whatever behaviours the role wishes to perform | ||||||||||||||
| 559 | must be provided only by calling other methods on C<$self>, or perhaps by | ||||||||||||||
| 560 | making assumptions about the representation type of instances. | ||||||||||||||
| 561 | |||||||||||||||
| 562 | It should be stressed again: This option is I<only> intended for gradual | ||||||||||||||
| 563 | upgrade of existing classical Perl code into using C<Object::Pad>. When all | ||||||||||||||
| 564 | existing code is using C<Object::Pad> then this attribute can be removed from | ||||||||||||||
| 565 | the role. | ||||||||||||||
| 566 | |||||||||||||||
| 567 | =head2 my class | ||||||||||||||
| 568 | |||||||||||||||
| 569 | my class Name :ATTRS... { ... } | ||||||||||||||
| 570 | |||||||||||||||
| 571 | I<Since version 0.819; experimental> | ||||||||||||||
| 572 | |||||||||||||||
| 573 | If a C<class> keyword is preceded by C<my> it creates a class that has lexical | ||||||||||||||
| 574 | visiblity, rather than being available globally via a fully-qualified name in | ||||||||||||||
| 575 | the symbol table. This is useful for creating internal helper classes within | ||||||||||||||
| 576 | modules, such as for returning complex structures, or simply to use internally | ||||||||||||||
| 577 | without being visible to the caller. As the class itself is only visible | ||||||||||||||
| 578 | lexically, callers in other scopes are unable to see it to create new | ||||||||||||||
| 579 | instances of it. | ||||||||||||||
| 580 | |||||||||||||||
| 581 | class My::Module; | ||||||||||||||
| 582 | |||||||||||||||
| 583 | my class HelperStructure | ||||||||||||||
| 584 | { | ||||||||||||||
| 585 | field $name :param :reader; | ||||||||||||||
| 586 | field $value :param :reader; | ||||||||||||||
| 587 | } | ||||||||||||||
| 588 | |||||||||||||||
| 589 | method get_thing () | ||||||||||||||
| 590 | { | ||||||||||||||
| 591 | return HelperStructure->new( | ||||||||||||||
| 592 | name => "the name", | ||||||||||||||
| 593 | value => "the value", | ||||||||||||||
| 594 | ); | ||||||||||||||
| 595 | } | ||||||||||||||
| 596 | |||||||||||||||
| 597 | B<Note:> the above is technically a lie. Current versions of perl do not | ||||||||||||||
| 598 | support truely anonymous packages to be used as classes, so even these | ||||||||||||||
| 599 | "lexical" classes are in fact named on the symbol table. However, they are | ||||||||||||||
| 600 | given a name that is syntactically-impossible for regular code to create or | ||||||||||||||
| 601 | use, and a const-returning lexical function is created with the correct | ||||||||||||||
| 602 | lexical name, which returns the true name of the class, so that the | ||||||||||||||
| 603 | C<< ->new >> method can still be called on it. | ||||||||||||||
| 604 | |||||||||||||||
| 605 | As a result of this limitation, there are a number of operations that lexical | ||||||||||||||
| 606 | classes do not currently support - such as subclassing. It is hoped that later | ||||||||||||||
| 607 | versions of either this module, or perl itself, will be able to expand on | ||||||||||||||
| 608 | these abilities. For now, this feature remains an experimental best-effort | ||||||||||||||
| 609 | basis. | ||||||||||||||
| 610 | |||||||||||||||
| 611 | Due to the way this lexical function works, it can stand in for the class name | ||||||||||||||
| 612 | when used with the C<< ->isa >> method. Note carefully here, that the lexical | ||||||||||||||
| 613 | class is used directly as a bareword and not quoted. | ||||||||||||||
| 614 | |||||||||||||||
| 615 | if( $obj->isa( HelperStructure ) ) { ... } | ||||||||||||||
| 616 | |||||||||||||||
| 617 | Additionally, as the right-hand side operand of the C<isa> operator (in Perl | ||||||||||||||
| 618 | version 5.32 or later) does not need quoting, it can be used there directly: | ||||||||||||||
| 619 | |||||||||||||||
| 620 | if( $obj isa HelperStructure ) { ... } | ||||||||||||||
| 621 | |||||||||||||||
| 622 | =head2 inherit | ||||||||||||||
| 623 | |||||||||||||||
| 624 | inherit Classname; | ||||||||||||||
| 625 | inherit Classname VER; | ||||||||||||||
| 626 | |||||||||||||||
| 627 | inherit Classname LIST...; | ||||||||||||||
| 628 | inherit Classname VER LIST...; | ||||||||||||||
| 629 | |||||||||||||||
| 630 | I<Since version 0.807.> | ||||||||||||||
| 631 | |||||||||||||||
| 632 | Declares a superclass that this class extends. At most one superclass is | ||||||||||||||
| 633 | supported. If present, this declaration must come before any methods or fields | ||||||||||||||
| 634 | are declared, or any roles applied. (Other compile-time declarations such as | ||||||||||||||
| 635 | C<use> statements that import utility functions or other behaviours may be | ||||||||||||||
| 636 | permitted before this, however, provided that they do not interact with the | ||||||||||||||
| 637 | class structure in any way). | ||||||||||||||
| 638 | |||||||||||||||
| 639 | This is a newer form of the C<:isa> attribute intended to be more flexible if | ||||||||||||||
| 640 | import arguments or other features are added at a later time. | ||||||||||||||
| 641 | |||||||||||||||
| 642 | If the package providing the superclass does not exist, an attempt is made to | ||||||||||||||
| 643 | load it by code equivalent to | ||||||||||||||
| 644 | |||||||||||||||
| 645 | require Classname; | ||||||||||||||
| 646 | |||||||||||||||
| 647 | and thus it must either already exist, or be locatable via the usual C<@INC> | ||||||||||||||
| 648 | mechanisms. | ||||||||||||||
| 649 | |||||||||||||||
| 650 | An optional version check can also be supplied; it performs the equivalent of | ||||||||||||||
| 651 | |||||||||||||||
| 652 | Classname->VERSION( $ver ) | ||||||||||||||
| 653 | |||||||||||||||
| 654 | Experimentally I<since version 0.807>, an optional list of arguments can also | ||||||||||||||
| 655 | be provided, in similar syntax to those in a C<use> statement. Currently this | ||||||||||||||
| 656 | list of arguments must be names of fields to be inherited. Only fields in the | ||||||||||||||
| 657 | base class that are annotated with the C<:inheritable> attribute may be | ||||||||||||||
| 658 | inherited. Once a field is inherited, methods and other expressions in the | ||||||||||||||
| 659 | class body can use that field identically to any fields defined by that class | ||||||||||||||
| 660 | itself. | ||||||||||||||
| 661 | |||||||||||||||
| 662 | class Class1 { | ||||||||||||||
| 663 | field $x :inheritable = 123; | ||||||||||||||
| 664 | } | ||||||||||||||
| 665 | |||||||||||||||
| 666 | class Class2 { | ||||||||||||||
| 667 | inherit Class1 '$x'; | ||||||||||||||
| 668 | field $y = 456; | ||||||||||||||
| 669 | method describe { say "Class2(x=$x,y=$y)" } | ||||||||||||||
| 670 | } | ||||||||||||||
| 671 | |||||||||||||||
| 672 | Class2->new->describe; | ||||||||||||||
| 673 | |||||||||||||||
| 674 | =head2 apply | ||||||||||||||
| 675 | |||||||||||||||
| 676 | apply Rolename; | ||||||||||||||
| 677 | |||||||||||||||
| 678 | apply Rolename VER; | ||||||||||||||
| 679 | |||||||||||||||
| 680 | I<Since version 0.807.> | ||||||||||||||
| 681 | |||||||||||||||
| 682 | Composes a role into the class; optionally requiring a version check on the | ||||||||||||||
| 683 | role package. This is a newer form of the C<:does> attribute intended to be | ||||||||||||||
| 684 | more flexible if import arguments or other features are added at a later time. | ||||||||||||||
| 685 | |||||||||||||||
| 686 | Multiple roles can be composed by using multiple C<:does> attributes, one per | ||||||||||||||
| 687 | role. | ||||||||||||||
| 688 | |||||||||||||||
| 689 | C<apply> statements can be freely mixed with other statements inside the body | ||||||||||||||
| 690 | of the class. In particular, an C<apply> statement that adds fields or methods | ||||||||||||||
| 691 | may appear before or after the class has defined some of its own. It is not | ||||||||||||||
| 692 | required that they appear first. | ||||||||||||||
| 693 | |||||||||||||||
| 694 | =head2 field | ||||||||||||||
| 695 | |||||||||||||||
| 696 | field $var; | ||||||||||||||
| 697 | field @var; | ||||||||||||||
| 698 | field %var; | ||||||||||||||
| 699 | |||||||||||||||
| 700 | field $var :ATTR ATTR...; | ||||||||||||||
| 701 | |||||||||||||||
| 702 | field $var = EXPR; | ||||||||||||||
| 703 | |||||||||||||||
| 704 | field $var //= EXPR; | ||||||||||||||
| 705 | field $var ||= EXPR; | ||||||||||||||
| 706 | |||||||||||||||
| 707 | field $var { BLOCK } | ||||||||||||||
| 708 | |||||||||||||||
| 709 | I<Since version 0.66.> | ||||||||||||||
| 710 | |||||||||||||||
| 711 | Declares that the instances of the class or role have a member field of the | ||||||||||||||
| 712 | given name. This member field will be accessible as a lexical variable within | ||||||||||||||
| 713 | any C<method> declarations and C<ADJUST> phasers in the class. | ||||||||||||||
| 714 | |||||||||||||||
| 715 | Array and hash members are permitted and behave as expected; you do not need | ||||||||||||||
| 716 | to store references to anonymous arrays or hashes. | ||||||||||||||
| 717 | |||||||||||||||
| 718 | Member fields are private to a class or role. They are not visible to users of | ||||||||||||||
| 719 | the class, nor inherited by subclasses nor any class that a role is applied | ||||||||||||||
| 720 | to. In order to provide access to them a class may wish to use L</method> to | ||||||||||||||
| 721 | create an accessor, or use the attributes such as L</:reader> to get one | ||||||||||||||
| 722 | generated. | ||||||||||||||
| 723 | |||||||||||||||
| 724 | The following field attributes are supported: | ||||||||||||||
| 725 | |||||||||||||||
| 726 | =head3 :reader, :reader(NAME) | ||||||||||||||
| 727 | |||||||||||||||
| 728 | I<Since version 0.27.> | ||||||||||||||
| 729 | |||||||||||||||
| 730 | Generates a reader method to return the current value of the field. If no name | ||||||||||||||
| 731 | is given, the name of the field is used. A single prefix character C<_> will | ||||||||||||||
| 732 | be removed if present. | ||||||||||||||
| 733 | |||||||||||||||
| 734 | field $x :reader; | ||||||||||||||
| 735 | |||||||||||||||
| 736 | # equivalent to | ||||||||||||||
| 737 | field $x; method x { return $x } | ||||||||||||||
| 738 | |||||||||||||||
| 739 | I<Since version 0.55> these are permitted on any field type, but prior | ||||||||||||||
| 740 | versions only allowed them on scalar fields. The reader method behaves | ||||||||||||||
| 741 | identically to how a lexical variable would behave in the same context; namely | ||||||||||||||
| 742 | returning a list of values from an array or key/value pairs from a hash when | ||||||||||||||
| 743 | in list context, or the number of items or keys when in scalar context. | ||||||||||||||
| 744 | |||||||||||||||
| 745 | field @items :reader; | ||||||||||||||
| 746 | |||||||||||||||
| 747 | foreach my $item ( $obj->items ) { ... } # iterates the list of items | ||||||||||||||
| 748 | |||||||||||||||
| 749 | my $count = $obj->items; # yields count of items | ||||||||||||||
| 750 | |||||||||||||||
| 751 | =head3 :writer, :writer(NAME) | ||||||||||||||
| 752 | |||||||||||||||
| 753 | I<Since version 0.27.> | ||||||||||||||
| 754 | |||||||||||||||
| 755 | Generates a writer method to set a new value of the field from its arguments. | ||||||||||||||
| 756 | If no name is given, the name of the field is used prefixed by C<set_>. A | ||||||||||||||
| 757 | single prefix character C<_> will be removed if present. | ||||||||||||||
| 758 | |||||||||||||||
| 759 | field $x :writer; | ||||||||||||||
| 760 | |||||||||||||||
| 761 | # equivalent to | ||||||||||||||
| 762 | field $x; method set_x { $x = shift; return $self } | ||||||||||||||
| 763 | |||||||||||||||
| 764 | I<Since version 0.28> a generated writer method will return the object | ||||||||||||||
| 765 | invocant itself, allowing a chaining style. | ||||||||||||||
| 766 | |||||||||||||||
| 767 | $obj->set_x("x") | ||||||||||||||
| 768 | ->set_y("y") | ||||||||||||||
| 769 | ->set_z("z"); | ||||||||||||||
| 770 | |||||||||||||||
| 771 | I<Since version 0.55> these are permitted on any field type, but prior | ||||||||||||||
| 772 | versions only allowed them on scalar fields. On arrays or hashes, the writer | ||||||||||||||
| 773 | method takes a list of values to be assigned into the field, completely | ||||||||||||||
| 774 | replacing any values previously there. | ||||||||||||||
| 775 | |||||||||||||||
| 776 | =head3 :mutator, :mutator(NAME) | ||||||||||||||
| 777 | |||||||||||||||
| 778 | I<Since version 0.27.> | ||||||||||||||
| 779 | |||||||||||||||
| 780 | Generates an lvalue mutator method to return or set the value of the field. | ||||||||||||||
| 781 | These are only permitted for scalar fields. If no name is given, the name of | ||||||||||||||
| 782 | the field is used. A single prefix character C<_> will be removed if present. | ||||||||||||||
| 783 | |||||||||||||||
| 784 | field $x :mutator; | ||||||||||||||
| 785 | |||||||||||||||
| 786 | # equivalent to | ||||||||||||||
| 787 | field $x; method x :lvalue { $x } | ||||||||||||||
| 788 | |||||||||||||||
| 789 | I<Since version 0.28> all of these generated accessor methods will include | ||||||||||||||
| 790 | argument checking similar to that used by subroutine signatures, to ensure the | ||||||||||||||
| 791 | correct number of arguments are passed - usually zero, but exactly one in the | ||||||||||||||
| 792 | case of a C<:writer> method. | ||||||||||||||
| 793 | |||||||||||||||
| 794 | =head3 :accessor, :accessor(NAME) | ||||||||||||||
| 795 | |||||||||||||||
| 796 | I<Since version 0.53.> | ||||||||||||||
| 797 | |||||||||||||||
| 798 | Generates a combined reader-writer accessor method to set or return the value | ||||||||||||||
| 799 | of the field. These are only permitted for scalar fields. If no name is given, | ||||||||||||||
| 800 | the name of the field is used. A prefix character C<_> will be removed if | ||||||||||||||
| 801 | present. | ||||||||||||||
| 802 | |||||||||||||||
| 803 | This method takes either zero or one additional arguments. If an argument is | ||||||||||||||
| 804 | passed, the value of the field is set from this argument (even if it is | ||||||||||||||
| 805 | C<undef>). If no argument is passed (i.e. C<scalar @_> is false) then the | ||||||||||||||
| 806 | field is not modified. In either case, the value of the field is then | ||||||||||||||
| 807 | returned. | ||||||||||||||
| 808 | |||||||||||||||
| 809 | field $x :accessor; | ||||||||||||||
| 810 | |||||||||||||||
| 811 | # equivalent to | ||||||||||||||
| 812 | field $x; | ||||||||||||||
| 813 | |||||||||||||||
| 814 | method x { | ||||||||||||||
| 815 | $x = shift if @_; | ||||||||||||||
| 816 | return $x; | ||||||||||||||
| 817 | } | ||||||||||||||
| 818 | |||||||||||||||
| 819 | =head3 :weak | ||||||||||||||
| 820 | |||||||||||||||
| 821 | I<Since version 0.44.> | ||||||||||||||
| 822 | |||||||||||||||
| 823 | Generated code which sets the value of this field will weaken it if it | ||||||||||||||
| 824 | contains a reference. This applies to within the constructor if C<:param> is | ||||||||||||||
| 825 | given, and to a C<:writer> accessor method. Note that this I<only> applies to | ||||||||||||||
| 826 | automatically generated code; not normal code written in regular method | ||||||||||||||
| 827 | bodies. If you assign into the field variable you must remember to call | ||||||||||||||
| 828 | C<Scalar::Util::weaken> (or C<builtin::weaken> on Perl 5.36 or above) | ||||||||||||||
| 829 | yourself. | ||||||||||||||
| 830 | |||||||||||||||
| 831 | =head3 :param, :param(NAME) | ||||||||||||||
| 832 | |||||||||||||||
| 833 | I<Since version 0.41.> | ||||||||||||||
| 834 | |||||||||||||||
| 835 | Sets this field to be initialised automatically in the generated constructor. | ||||||||||||||
| 836 | This is only permitted on scalar fields. If no name is given, the name of the | ||||||||||||||
| 837 | field is used. A single prefix character C<_> will be removed if present. | ||||||||||||||
| 838 | |||||||||||||||
| 839 | Any field that has C<:param> but does not have a default initialisation | ||||||||||||||
| 840 | expression or block becomes a required argument to the constructor. Attempting | ||||||||||||||
| 841 | to invoke the constructor without a named argument for this will throw an | ||||||||||||||
| 842 | exception. In order to make a parameter optional, make sure to give it a | ||||||||||||||
| 843 | default expression - even if that expression is C<undef>: | ||||||||||||||
| 844 | |||||||||||||||
| 845 | field $x :param; # this is required | ||||||||||||||
| 846 | field $z :param = undef; # this is optional | ||||||||||||||
| 847 | |||||||||||||||
| 848 | Any field that has a C<:param> and an initialisation block will only run the | ||||||||||||||
| 849 | code in the block if required by the constructor. If a named parameter is | ||||||||||||||
| 850 | passed to the constructor for this field, then its code block will not be | ||||||||||||||
| 851 | executed. | ||||||||||||||
| 852 | |||||||||||||||
| 853 | Values for fields are assigned by the constructor before any C<BUILD> phasers | ||||||||||||||
| 854 | are invoked. | ||||||||||||||
| 855 | |||||||||||||||
| 856 | =head3 :inheritable | ||||||||||||||
| 857 | |||||||||||||||
| 858 | Experimentally I<since version 0.807> fields may be optionally inherited when | ||||||||||||||
| 859 | deriving a subclass from another. Not every field is allowed to be inherited. | ||||||||||||||
| 860 | This attribute marks a field as being available for subclasses to inherit. | ||||||||||||||
| 861 | |||||||||||||||
| 862 | =head3 Field Initialiser Expressions | ||||||||||||||
| 863 | |||||||||||||||
| 864 | I<Since version 0.54> a deferred statement block is also permitted, on any | ||||||||||||||
| 865 | field variable type. This permits code to be executed as part of the instance | ||||||||||||||
| 866 | constructor, rather than running just once when the class is set up. Code in a | ||||||||||||||
| 867 | field initialisation block is roughly equivalent to being placed in a C<BUILD> | ||||||||||||||
| 868 | or C<ADJUST> phaser. | ||||||||||||||
| 869 | |||||||||||||||
| 870 | I<Since version 0.73> this may also be written as a plain expression | ||||||||||||||
| 871 | introduced by an equals symbol (C<=>). This is equivalent to using a block. | ||||||||||||||
| 872 | Note carefully: the equals symbol is part of the C<field> syntax; it is I<not> | ||||||||||||||
| 873 | simply a runtime assignment operator that happens once at the time the class | ||||||||||||||
| 874 | is declared. Just like the block form describe above, the expression is | ||||||||||||||
| 875 | evaluated during the constructor of every instance. | ||||||||||||||
| 876 | |||||||||||||||
| 877 | I<Since version 0.74> this expression may also be written using a defined-or | ||||||||||||||
| 878 | or logical-or assignment operator (C<//=> or C<||=>). In these case, the | ||||||||||||||
| 879 | default expression will be evaluated and assigned if the caller did not pass a | ||||||||||||||
| 880 | value to the constructor at all, or if the value passed was undef (for C<//=>) | ||||||||||||||
| 881 | or false (for C<||=>). For most scalar parameters, where C<undef> is not a | ||||||||||||||
| 882 | valid value, you probably wanted to use C<//=> to assign defaults. | ||||||||||||||
| 883 | |||||||||||||||
| 884 | class Action { | ||||||||||||||
| 885 | field $timeout :param //= 20; | ||||||||||||||
| 886 | ... | ||||||||||||||
| 887 | } | ||||||||||||||
| 888 | |||||||||||||||
| 889 | # The default of 20 will apply here too | ||||||||||||||
| 890 | my $act = Action->new( timeout => $opts{timeout} ); | ||||||||||||||
| 891 | |||||||||||||||
| 892 | Note that C<$self> is specifically I<not> visible during an initialiser | ||||||||||||||
| 893 | expression. This is because the object is not yet fully constructed, so it | ||||||||||||||
| 894 | would be dangerous to allow access to it while in this state. However, the | ||||||||||||||
| 895 | C<__CLASS__> keyword is available, so initialiser expressions can make use of | ||||||||||||||
| 896 | class-based dispatch to invoke class-level methods to help provide values. | ||||||||||||||
| 897 | |||||||||||||||
| 898 | Field initialier expressions were originally experimental, but I<since version | ||||||||||||||
| 899 | 0.800> no longer emit experimental warnings. | ||||||||||||||
| 900 | |||||||||||||||
| 901 | I<Since version 0.806> fields already declared in a class are visible during | ||||||||||||||
| 902 | the initialisation expression of later fields, and their assigned value can be | ||||||||||||||
| 903 | used here. If the earlier field had a C<:param> declaration, it will have been | ||||||||||||||
| 904 | assigned from the value passed to the constructor. Note however that all | ||||||||||||||
| 905 | C<ADJUST> phasers happen I<after> field initialisation expressions, so any | ||||||||||||||
| 906 | modified values set in such phasers will not be visible at this time. | ||||||||||||||
| 907 | |||||||||||||||
| 908 | Control flow that attempts to leave a field initialiser expression or block is | ||||||||||||||
| 909 | not permitted. This includes any C<return> expression, any C<next/last/redo> | ||||||||||||||
| 910 | outside of a loop, with a dynamically-calculated label expression, or with a | ||||||||||||||
| 911 | label that it doesn't appear in. C<goto> statements are also currently | ||||||||||||||
| 912 | forbidden, though known-safe ones may be permitted in future. | ||||||||||||||
| 913 | |||||||||||||||
| 914 | Loop control expressions that are known at compiletime to affect a loop that | ||||||||||||||
| 915 | they appear within are permitted. | ||||||||||||||
| 916 | |||||||||||||||
| 917 | field $x { foreach(@list) { next; } } # this is fine | ||||||||||||||
| 918 | |||||||||||||||
| 919 | field $x { LOOP: while(1) { last LOOP; } } # this is fine too | ||||||||||||||
| 920 | |||||||||||||||
| 921 | =head2 has | ||||||||||||||
| 922 | |||||||||||||||
| 923 | I<Since version 0.813> this keyword is no longer recognised. | ||||||||||||||
| 924 | |||||||||||||||
| 925 | It used to be an earlier version of what is now the L</field> keyword. | ||||||||||||||
| 926 | |||||||||||||||
| 927 | has $var; | ||||||||||||||
| 928 | has @var; | ||||||||||||||
| 929 | has %var; | ||||||||||||||
| 930 | |||||||||||||||
| 931 | has $var = EXPR; | ||||||||||||||
| 932 | |||||||||||||||
| 933 | has $var { BLOCK } | ||||||||||||||
| 934 | |||||||||||||||
| 935 | Because of the one-shot immediate nature of these initialisation expressions | ||||||||||||||
| 936 | (and a bunch of other reasons), the keyword was removed. | ||||||||||||||
| 937 | |||||||||||||||
| 938 | If you need to evaluate an expression exactly once during the class | ||||||||||||||
| 939 | declaration and assign its now-constant value to every instace, store it in a | ||||||||||||||
| 940 | regular C<my> variable instead: | ||||||||||||||
| 941 | |||||||||||||||
| 942 | my $default_var = EXPR; | ||||||||||||||
| 943 | field $var = $default_var; | ||||||||||||||
| 944 | |||||||||||||||
| 945 | =head2 method | ||||||||||||||
| 946 | |||||||||||||||
| 947 | method NAME { | ||||||||||||||
| 948 | ... | ||||||||||||||
| 949 | } | ||||||||||||||
| 950 | |||||||||||||||
| 951 | method NAME (SIGNATURE) { | ||||||||||||||
| 952 | ... | ||||||||||||||
| 953 | } | ||||||||||||||
| 954 | |||||||||||||||
| 955 | method NAME :ATTRS... { | ||||||||||||||
| 956 | ... | ||||||||||||||
| 957 | } | ||||||||||||||
| 958 | |||||||||||||||
| 959 | method NAME; | ||||||||||||||
| 960 | |||||||||||||||
| 961 | Declares a new named method. This behaves similarly to the C<sub> keyword, | ||||||||||||||
| 962 | except that within the body of the method all of the member fields are also | ||||||||||||||
| 963 | accessible. In addition, the method body will have a lexical called C<$self> | ||||||||||||||
| 964 | which contains the invocant object directly; it will already have been shifted | ||||||||||||||
| 965 | from the C<@_> array. | ||||||||||||||
| 966 | |||||||||||||||
| 967 | If the method has no body and is given simply as a name, this declares a | ||||||||||||||
| 968 | I<required> method for a role or abstract class. Such a method must be | ||||||||||||||
| 969 | provided by any class that wishes to be non-abstract. It will be a compiletime | ||||||||||||||
| 970 | error to apply the role to a class, or derive from an abstract class, that | ||||||||||||||
| 971 | does not provide this. | ||||||||||||||
| 972 | |||||||||||||||
| 973 | The C<signatures> feature is automatically enabled for method declarations. In | ||||||||||||||
| 974 | this case the signature does not have to account for the invocant instance; | ||||||||||||||
| 975 | that is handled directly. | ||||||||||||||
| 976 | |||||||||||||||
| 977 | method m ($one, $two) { | ||||||||||||||
| 978 | say "$self invokes method on one=$one two=$two"; | ||||||||||||||
| 979 | } | ||||||||||||||
| 980 | |||||||||||||||
| 981 | ... | ||||||||||||||
| 982 | $obj->m(1, 2); | ||||||||||||||
| 983 | |||||||||||||||
| 984 | A list of attributes may be supplied as for C<sub>. The most useful of these | ||||||||||||||
| 985 | is C<:lvalue>, allowing easy creation of read-write accessors for fields (but | ||||||||||||||
| 986 | see also the C<:reader>, C<:writer> and C<:mutator> field attributes). | ||||||||||||||
| 987 | |||||||||||||||
| 988 | class Counter { | ||||||||||||||
| 989 | field $count; | ||||||||||||||
| 990 | |||||||||||||||
| 991 | method count :lvalue { $count } | ||||||||||||||
| 992 | } | ||||||||||||||
| 993 | |||||||||||||||
| 994 | my $c = Counter->new; | ||||||||||||||
| 995 | $c->count++; | ||||||||||||||
| 996 | |||||||||||||||
| 997 | Every method automatically gets the C<:method> attribute applied, which | ||||||||||||||
| 998 | suppresses warnings about ambiguous calls resolved to core functions if the | ||||||||||||||
| 999 | name of a method matches a core function. | ||||||||||||||
| 1000 | |||||||||||||||
| 1001 | The following additional attributes are recognised by C<Object::Pad> directly: | ||||||||||||||
| 1002 | |||||||||||||||
| 1003 | =head3 :override | ||||||||||||||
| 1004 | |||||||||||||||
| 1005 | I<Since version 0.29.> | ||||||||||||||
| 1006 | |||||||||||||||
| 1007 | Marks that this method expects to override another of the same name from a | ||||||||||||||
| 1008 | superclass. It is an error at compiletime if the superclass does not provide | ||||||||||||||
| 1009 | such a method. | ||||||||||||||
| 1010 | |||||||||||||||
| 1011 | =head3 :common | ||||||||||||||
| 1012 | |||||||||||||||
| 1013 | I<Since version 0.62.> | ||||||||||||||
| 1014 | |||||||||||||||
| 1015 | Marks that this method is a class-common method, instead of a regular instance | ||||||||||||||
| 1016 | method. A class-common method may be invoked on class names instead of | ||||||||||||||
| 1017 | instances. Within the method body there is a lexical C<$class> available, | ||||||||||||||
| 1018 | rather than C<$self>. Because it is not associated with a particular object | ||||||||||||||
| 1019 | instance, a class-common method cannot see instance fields. | ||||||||||||||
| 1020 | |||||||||||||||
| 1021 | =head2 method (lexical) | ||||||||||||||
| 1022 | |||||||||||||||
| 1023 | method $var { ... } | ||||||||||||||
| 1024 | |||||||||||||||
| 1025 | method $var :ATTRS... (SIGNATURE) { ... } | ||||||||||||||
| 1026 | |||||||||||||||
| 1027 | I<Since version 0.59.> | ||||||||||||||
| 1028 | |||||||||||||||
| 1029 | Declares a new lexical method. Lexical methods are not visible via the package | ||||||||||||||
| 1030 | namespace, but instead are stored directly in a lexical variable (with the | ||||||||||||||
| 1031 | same scoping rules as regular C<my> variables). These can be invoked by | ||||||||||||||
| 1032 | subsequent method code in the same block by using C<< $self->$var(...) >> | ||||||||||||||
| 1033 | method call syntax. | ||||||||||||||
| 1034 | |||||||||||||||
| 1035 | class WithPrivate { | ||||||||||||||
| 1036 | field $var; | ||||||||||||||
| 1037 | |||||||||||||||
| 1038 | # Lexical methods can still see instance fields as normal | ||||||||||||||
| 1039 | method $inc_var { $var++; say "Var was incremented"; } | ||||||||||||||
| 1040 | method $dec_var { $var--; say "Var was decremented"; } | ||||||||||||||
| 1041 | |||||||||||||||
| 1042 | method bump { | ||||||||||||||
| 1043 | $self->$inc_var; | ||||||||||||||
| 1044 | say "In the middle"; | ||||||||||||||
| 1045 | $self->$dec_var; | ||||||||||||||
| 1046 | } | ||||||||||||||
| 1047 | } | ||||||||||||||
| 1048 | |||||||||||||||
| 1049 | my $obj = WithPrivate->new; | ||||||||||||||
| 1050 | |||||||||||||||
| 1051 | $obj->bump; | ||||||||||||||
| 1052 | |||||||||||||||
| 1053 | # Neither $inc_var nor $dec_var are visible here | ||||||||||||||
| 1054 | |||||||||||||||
| 1055 | This effectively provides the ability to define B<private> methods, as they | ||||||||||||||
| 1056 | are inaccessible from outside the block that defines the class. In addition, | ||||||||||||||
| 1057 | there is no chance of a name collision because lexical variables in different | ||||||||||||||
| 1058 | scopes are independent, even if they share the same name. This is particularly | ||||||||||||||
| 1059 | useful in roles or abstract base classes, to create internal helper methods | ||||||||||||||
| 1060 | without letting those methods be visible to callers, or risking their names | ||||||||||||||
| 1061 | colliding with other named methods defined on the consuming class. | ||||||||||||||
| 1062 | |||||||||||||||
| 1063 | =head2 my method | ||||||||||||||
| 1064 | |||||||||||||||
| 1065 | my method NAME { ... } | ||||||||||||||
| 1066 | |||||||||||||||
| 1067 | I<Since version 0.814> lexical method declarations are supported using the | ||||||||||||||
| 1068 | C<my> keyword prefix. These become available as lexical functions, rather than | ||||||||||||||
| 1069 | being stored in the class package. As a result, they are not available by | ||||||||||||||
| 1070 | named method resolution, package C<< ->can >> lookup, or via the MOP. These | ||||||||||||||
| 1071 | are a convenient alternative to the syntax given above, where the method is | ||||||||||||||
| 1072 | stored anonymously via a lexical variable. | ||||||||||||||
| 1073 | |||||||||||||||
| 1074 | Since lexical methods are not visible to named method resolution, they must be | ||||||||||||||
| 1075 | invoked by function-call syntax, remembering to pass in the invocant as the | ||||||||||||||
| 1076 | first argument: | ||||||||||||||
| 1077 | |||||||||||||||
| 1078 | my method inner { ... } | ||||||||||||||
| 1079 | |||||||||||||||
| 1080 | method outer { | ||||||||||||||
| 1081 | inner($self, @args); | ||||||||||||||
| 1082 | } | ||||||||||||||
| 1083 | |||||||||||||||
| 1084 | =head2 BUILD | ||||||||||||||
| 1085 | |||||||||||||||
| 1086 | BUILD { | ||||||||||||||
| 1087 | ... | ||||||||||||||
| 1088 | } | ||||||||||||||
| 1089 | |||||||||||||||
| 1090 | BUILD (SIGNATURE) { | ||||||||||||||
| 1091 | ... | ||||||||||||||
| 1092 | } | ||||||||||||||
| 1093 | |||||||||||||||
| 1094 | I<Since version 0.27.> | ||||||||||||||
| 1095 | |||||||||||||||
| 1096 | Declares a builder phaser for this component class. A builder phaser may use | ||||||||||||||
| 1097 | subroutine signature syntax, as for methods, to assist in unpacking its | ||||||||||||||
| 1098 | arguments. A build phaser is not a subroutine and thus is not permitted to use | ||||||||||||||
| 1099 | subroutine attributes (for example C<:lvalue>). | ||||||||||||||
| 1100 | |||||||||||||||
| 1101 | Note that a C<BUILD> phaser is a named phaser block and not a method. Attempts | ||||||||||||||
| 1102 | to create a method named C<BUILD> (i.e. with syntax C<method BUILD {...}>) | ||||||||||||||
| 1103 | will fail with a compiletime error, to avoid this confusion. | ||||||||||||||
| 1104 | |||||||||||||||
| 1105 | =head2 ADJUST | ||||||||||||||
| 1106 | |||||||||||||||
| 1107 | ADJUST { | ||||||||||||||
| 1108 | ... | ||||||||||||||
| 1109 | } | ||||||||||||||
| 1110 | |||||||||||||||
| 1111 | I<Since version 0.43.> | ||||||||||||||
| 1112 | |||||||||||||||
| 1113 | Declares an adjust phaser for this component class. This block of code runs | ||||||||||||||
| 1114 | within the constructor, after any C<BUILD> phasers and automatic field value | ||||||||||||||
| 1115 | assignment. It can make any final adjustments to the instance (such as | ||||||||||||||
| 1116 | initialising fields from calculated values). | ||||||||||||||
| 1117 | |||||||||||||||
| 1118 | An adjust phaser is not a subroutine and thus is not permitted to use | ||||||||||||||
| 1119 | subroutine attributes (except see below). Note that an C<ADJUST> phaser is a | ||||||||||||||
| 1120 | named phaser block and not a method; it does not use the C<sub> or C<method> | ||||||||||||||
| 1121 | keyword. But, like with C<method>, the member fields are accessible within the | ||||||||||||||
| 1122 | code body, as is the special C<$self> lexical. | ||||||||||||||
| 1123 | |||||||||||||||
| 1124 | Currently, an C<ADJUST> phaser receives a reference to the hash containing the | ||||||||||||||
| 1125 | current constructor arguments, as per L</ADJUSTPARAMS> (see below). This was | ||||||||||||||
| 1126 | added in version 0.66 but will be removed again as it conflicts with the more | ||||||||||||||
| 1127 | flexible and generally nicer named-parameter C<ADJUST :params> syntax | ||||||||||||||
| 1128 | (see below). Such uses should be considered deprecated. A warning will be | ||||||||||||||
| 1129 | printed to indicate this whenever an C<ADJUST> phaser uses a signature. This | ||||||||||||||
| 1130 | warning can be quieted by using C<ADJUSTPARAMS> instead. Additionally, a | ||||||||||||||
| 1131 | warning may be printed on code that attempts to access the params hashref via | ||||||||||||||
| 1132 | the C<@_> array. | ||||||||||||||
| 1133 | |||||||||||||||
| 1134 | I<Since version 0.801> in a future version of this module, C<ADJUST> phasers | ||||||||||||||
| 1135 | may be implemented as true blocks and will not permit out-of-block control | ||||||||||||||
| 1136 | flow. At present, they are implemented as one full CV per block, but a warning | ||||||||||||||
| 1137 | is emitted if out-of-block control flow is attempted. | ||||||||||||||
| 1138 | |||||||||||||||
| 1139 | ADJUST { | ||||||||||||||
| 1140 | return; | ||||||||||||||
| 1141 | } | ||||||||||||||
| 1142 | |||||||||||||||
| 1143 | Using return to leave an ADJUST phaser is discouraged and will be removed | ||||||||||||||
| 1144 | in a later version at FILE line LINE. | ||||||||||||||
| 1145 | |||||||||||||||
| 1146 | I<Since version 0.805> an experimental feature can be enabled that puts all | ||||||||||||||
| 1147 | the C<ADJUST> phasers into a single CV, rather than creating one CV for every | ||||||||||||||
| 1148 | phaser. This is currently being tested for stability, and may become the | ||||||||||||||
| 1149 | default behaviour in a future version. For now it must be requested specially: | ||||||||||||||
| 1150 | |||||||||||||||
| 1151 | use Object::Pad ':experimental(composed_adjust)'; | ||||||||||||||
| 1152 | |||||||||||||||
| 1153 | =head2 ADJUST :params | ||||||||||||||
| 1154 | |||||||||||||||
| 1155 | ADJUST :params ( :$var1, :$var2, ... ) { | ||||||||||||||
| 1156 | ... | ||||||||||||||
| 1157 | } | ||||||||||||||
| 1158 | |||||||||||||||
| 1159 | ADJUST :params ( :$var1, :$var2, ..., %varN ) { | ||||||||||||||
| 1160 | ... | ||||||||||||||
| 1161 | } | ||||||||||||||
| 1162 | |||||||||||||||
| 1163 | I<Since version 0.70; non-experimental since version 0.805.> | ||||||||||||||
| 1164 | |||||||||||||||
| 1165 | An C<ADJUST> phaser can marked with a C<:params> attribute, meaning that it | ||||||||||||||
| 1166 | consumes additional constructor parameters by assigning them into lexical | ||||||||||||||
| 1167 | variables. | ||||||||||||||
| 1168 | |||||||||||||||
| 1169 | Before the phaser itself, a list of lexical variables are introduced, inside | ||||||||||||||
| 1170 | parentheses. The name of each one is preceded by a colon, and consumes a | ||||||||||||||
| 1171 | constructor parameter of the same name. These parameters are considered | ||||||||||||||
| 1172 | "consumed" for the purposes of a C<:strict(params)> check. | ||||||||||||||
| 1173 | |||||||||||||||
| 1174 | A named parameter may be provided with default expression, which is evaluated | ||||||||||||||
| 1175 | if no matching named argument is provided to the constructor. As with fields, | ||||||||||||||
| 1176 | if a named parameter has no defaulting expression it becomes a required | ||||||||||||||
| 1177 | argument to the constructor; an exception is thrown by the constructor if it | ||||||||||||||
| 1178 | absent. | ||||||||||||||
| 1179 | |||||||||||||||
| 1180 | For example, | ||||||||||||||
| 1181 | |||||||||||||||
| 1182 | ADJUST :params ( :$x, :$y = "default", :$z ) { ... } | ||||||||||||||
| 1183 | |||||||||||||||
| 1184 | Note here that C<x> and C<z> are required parameters for the constructor of a | ||||||||||||||
| 1185 | class containing this phaser, but C<y> is an optional parameter whose value | ||||||||||||||
| 1186 | will be filled in by the expression if not provided. Because these parameters | ||||||||||||||
| 1187 | are named and not positional, there is no ordering constraint; required and | ||||||||||||||
| 1188 | optional parameters can be freely mixed. | ||||||||||||||
| 1189 | |||||||||||||||
| 1190 | Optional parameters can also use the C<//=> and C<||=> operators to provide a | ||||||||||||||
| 1191 | default expression. In these cases, the default will be applied if the caller | ||||||||||||||
| 1192 | did not provide the named argument at all, or if the provided value was not | ||||||||||||||
| 1193 | defined (for C<//=>) or not true (for C<||=>). | ||||||||||||||
| 1194 | |||||||||||||||
| 1195 | ADJUST :params ( :$name //= "unnamed" ) { ... } | ||||||||||||||
| 1196 | |||||||||||||||
| 1197 | Like with subroutine signature parameters, every declared named parameter is | ||||||||||||||
| 1198 | visible to the defaulting expression of all the later ones. This permits | ||||||||||||||
| 1199 | values to be calculated based on other ones. For example, | ||||||||||||||
| 1200 | |||||||||||||||
| 1201 | ADJUST :params ( :$thing = undef, :$things = [ $thing ] ) { | ||||||||||||||
| 1202 | # Here, @$things is a list of values | ||||||||||||||
| 1203 | } | ||||||||||||||
| 1204 | |||||||||||||||
| 1205 | This permits the caller to pass a list of values via an array reference in | ||||||||||||||
| 1206 | the C<things> parameter, or a single value in C<thing>. | ||||||||||||||
| 1207 | |||||||||||||||
| 1208 | The final element may be a regular hash variable. This requests that all | ||||||||||||||
| 1209 | remaining named parameters are made available inside it. The code in the | ||||||||||||||
| 1210 | phaser should C<delete> from this hash any parameters it wishes to consume, as | ||||||||||||||
| 1211 | with the earlier case above. | ||||||||||||||
| 1212 | |||||||||||||||
| 1213 | It is I<unspecified> whether named fields or parameters for subclasses yet to | ||||||||||||||
| 1214 | be processed are visible to hashes of earlier superclasses. In the current | ||||||||||||||
| 1215 | implementation they are, but code should not rely on this fact. | ||||||||||||||
| 1216 | |||||||||||||||
| 1217 | Note also that there must be a space between the C<:params> attribute and the | ||||||||||||||
| 1218 | parentheses holding the named parameters. If this space is not present, perl | ||||||||||||||
| 1219 | will parse the parentheses as if they are the value to the C<:params()> | ||||||||||||||
| 1220 | attribute, and this will fail to parse as intended. As with other attributes | ||||||||||||||
| 1221 | and subroutine signatures, this whitespace B<is> significant. | ||||||||||||||
| 1222 | |||||||||||||||
| 1223 | (This notation is borrowed from a plan to add named parameter support to | ||||||||||||||
| 1224 | perl's subroutine signature syntax). | ||||||||||||||
| 1225 | |||||||||||||||
| 1226 | =head2 ADJUSTPARAMS | ||||||||||||||
| 1227 | |||||||||||||||
| 1228 | I<Since version 0.51.> | ||||||||||||||
| 1229 | |||||||||||||||
| 1230 | ADJUSTPARAMS ( $params ) { # on perl 5.26 onwards | ||||||||||||||
| 1231 | ... | ||||||||||||||
| 1232 | } | ||||||||||||||
| 1233 | |||||||||||||||
| 1234 | ADJUST { | ||||||||||||||
| 1235 | my $params = shift; | ||||||||||||||
| 1236 | ... | ||||||||||||||
| 1237 | } | ||||||||||||||
| 1238 | |||||||||||||||
| 1239 | A variant of an C<ADJUST> phaser that receives a reference to the hash | ||||||||||||||
| 1240 | containing the current constructor parameters. This hash will not contain any | ||||||||||||||
| 1241 | constructor parameters already consumed by L</:param> declarations on any | ||||||||||||||
| 1242 | fields, but only the leftovers once those are processed. | ||||||||||||||
| 1243 | |||||||||||||||
| 1244 | The code in the phaser should C<delete> from this hash any parameters it | ||||||||||||||
| 1245 | wishes to consume. Once all the C<ADJUST> phasers have run, any remaining keys | ||||||||||||||
| 1246 | in the hash will be considered errors, subject to the L</:strict(params)> | ||||||||||||||
| 1247 | check. | ||||||||||||||
| 1248 | |||||||||||||||
| 1249 | =head2 APPLY | ||||||||||||||
| 1250 | |||||||||||||||
| 1251 | I<Experimental. Since version 0.815.> | ||||||||||||||
| 1252 | |||||||||||||||
| 1253 | APPLY ( $class_mop ) { # on perl 5.26 onwards | ||||||||||||||
| 1254 | ... | ||||||||||||||
| 1255 | } | ||||||||||||||
| 1256 | |||||||||||||||
| 1257 | APPLY { | ||||||||||||||
| 1258 | my $class_mop = shift; | ||||||||||||||
| 1259 | ... | ||||||||||||||
| 1260 | } | ||||||||||||||
| 1261 | |||||||||||||||
| 1262 | Only valid within a C<role> definition. Declares a block of code that will be | ||||||||||||||
| 1263 | run at compile-time whenever the role is applied to a class. Each time it is | ||||||||||||||
| 1264 | applied to a new class, the code will be invoked. It receives as an argument | ||||||||||||||
| 1265 | a L<Object::Pad::MOP::Class> instance representing the class to which the role | ||||||||||||||
| 1266 | is currently being applied. | ||||||||||||||
| 1267 | |||||||||||||||
| 1268 | The eventual intent is that the presence of any of these phaser blocks will | ||||||||||||||
| 1269 | I<replace> the current implicit behaviour of applying a role, though currently | ||||||||||||||
| 1270 | they run in addition to it. This is part of an ongoing experiment whose | ||||||||||||||
| 1271 | details will change over time. | ||||||||||||||
| 1272 | |||||||||||||||
| 1273 | =head2 __CLASS__ | ||||||||||||||
| 1274 | |||||||||||||||
| 1275 | my $classname = __CLASS__; | ||||||||||||||
| 1276 | |||||||||||||||
| 1277 | I<Since version 0.72.> | ||||||||||||||
| 1278 | |||||||||||||||
| 1279 | Only valid within the body (or signature) of a C<method>, an C<ADJUST> phaser, | ||||||||||||||
| 1280 | or the initialising expression of a C<field>. Yields the class name of the | ||||||||||||||
| 1281 | instance that the method, block or expression is invoked on. | ||||||||||||||
| 1282 | |||||||||||||||
| 1283 | This is similar to the core perl C<__PACKAGE__> constant, except that it cares | ||||||||||||||
| 1284 | about the dynamic class of the actual instance, not the static class the code | ||||||||||||||
| 1285 | belongs to. When invoked by a subclass instance that inherited code from its | ||||||||||||||
| 1286 | superclass it yields the name of the class of the instance regardless of which | ||||||||||||||
| 1287 | class defined the code. | ||||||||||||||
| 1288 | |||||||||||||||
| 1289 | For example, | ||||||||||||||
| 1290 | |||||||||||||||
| 1291 | class BaseClass { | ||||||||||||||
| 1292 | ADJUST { say "Constructing an instance of " . __CLASS__; } | ||||||||||||||
| 1293 | } | ||||||||||||||
| 1294 | |||||||||||||||
| 1295 | class DerivedClass :isa(BaseClass) { } | ||||||||||||||
| 1296 | |||||||||||||||
| 1297 | my $obj = DerivedClass->new; | ||||||||||||||
| 1298 | |||||||||||||||
| 1299 | Will produce the following output | ||||||||||||||
| 1300 | |||||||||||||||
| 1301 | Constructing an instance of DerivedClass | ||||||||||||||
| 1302 | |||||||||||||||
| 1303 | This is particularly useful in field initialisers for invoking (constant) | ||||||||||||||
| 1304 | methods on the invoking class to provide default values for fields. This way a | ||||||||||||||
| 1305 | subclass could provide a different value. | ||||||||||||||
| 1306 | |||||||||||||||
| 1307 | class Timer { | ||||||||||||||
| 1308 | use constant DEFAULT_DURATION => 60; | ||||||||||||||
| 1309 | field $duration = __CLASS__->DEFAULT_DURATION; | ||||||||||||||
| 1310 | } | ||||||||||||||
| 1311 | |||||||||||||||
| 1312 | class ThreeMinuteTimer :isa(Timer) { | ||||||||||||||
| 1313 | use constant DEFAULT_DURATION => 3 * 60; | ||||||||||||||
| 1314 | } | ||||||||||||||
| 1315 | |||||||||||||||
| 1316 | =head2 requires | ||||||||||||||
| 1317 | |||||||||||||||
| 1318 | requires NAME; | ||||||||||||||
| 1319 | |||||||||||||||
| 1320 | Declares that this role requires a method of the given name from any class | ||||||||||||||
| 1321 | that implements it. It is an error at compiletime if the implementing class | ||||||||||||||
| 1322 | does not provide such a method. | ||||||||||||||
| 1323 | |||||||||||||||
| 1324 | This form of declaring a required method is now vaguely discouraged, in favour | ||||||||||||||
| 1325 | of the bodyless C<method> form described above. | ||||||||||||||
| 1326 | |||||||||||||||
| 1327 | =head1 CREPT FEATURES | ||||||||||||||
| 1328 | |||||||||||||||
| 1329 | While not strictly part of being an object system, this module has | ||||||||||||||
| 1330 | nevertheless gained a number of behaviours by feature creep, as they have been | ||||||||||||||
| 1331 | found useful. | ||||||||||||||
| 1332 | |||||||||||||||
| 1333 | =head2 Implied Pragmata | ||||||||||||||
| 1334 | |||||||||||||||
| 1335 | In order to encourage users to write clean, modern code, the body of the | ||||||||||||||
| 1336 | C<class> block B<might> act as if the following pragmata are in effect: | ||||||||||||||
| 1337 | |||||||||||||||
| 1338 | use strict; | ||||||||||||||
| 1339 | use warnings; | ||||||||||||||
| 1340 | no indirect ':fatal'; # or no feature 'indirect' on perl 5.32 onwards | ||||||||||||||
| 1341 | use feature 'signatures'; | ||||||||||||||
| 1342 | |||||||||||||||
| 1343 | This behaviour was designed early around the original "line-0" version of the | ||||||||||||||
| 1344 | Perl 7 plan, which has subsequently been found to be a bad design and | ||||||||||||||
| 1345 | abandoned. That leaves this module in an unfortunate situation, because its | ||||||||||||||
| 1346 | behaviour here does not match the plans for core perl; where the | ||||||||||||||
| 1347 | recently-added C<class> keyword does none of this, although the C<method> | ||||||||||||||
| 1348 | keyword always behaves as if signatures were enabled anyway. | ||||||||||||||
| 1349 | |||||||||||||||
| 1350 | If you import this module with a module version number of I<0.821> or higher, | ||||||||||||||
| 1351 | this behaviour will not happen. This will be the default behaviour in future | ||||||||||||||
| 1352 | versions, and matches how the core perl feature works. (Though C<method> will | ||||||||||||||
| 1353 | still always parse as if the C<signatures> feature is enabled.) In particular, | ||||||||||||||
| 1354 | this no longer applies a catch-all C<use warnings>, meaning that any prior | ||||||||||||||
| 1355 | declarations of C<use experimental> or C<no warnings 'experimental::...'> will | ||||||||||||||
| 1356 | not be overridden. | ||||||||||||||
| 1357 | |||||||||||||||
| 1358 | However, in order to not upset older code that may have relied on these | ||||||||||||||
| 1359 | pragmata being present, module imports without such a version number will | ||||||||||||||
| 1360 | still enable them. This ensures that code not yet updated for the version | ||||||||||||||
| 1361 | I<0.821> semantics is not affected. | ||||||||||||||
| 1362 | |||||||||||||||
| 1363 | In addition, if you import this module with a module version number of | ||||||||||||||
| 1364 | I<0.800> or higher (but less than I<0.821>) it will emit a warning if you | ||||||||||||||
| 1365 | forget to enable C<strict> and C<warnings> before using the C<class> or | ||||||||||||||
| 1366 | C<role> keywords: | ||||||||||||||
| 1367 | |||||||||||||||
| 1368 | use Object::Pad 0.800; | ||||||||||||||
| 1369 | |||||||||||||||
| 1370 | class X { ... } | ||||||||||||||
| 1371 | |||||||||||||||
| 1372 | Z<> | ||||||||||||||
| 1373 | |||||||||||||||
| 1374 | =for highlighter | ||||||||||||||
| 1375 | |||||||||||||||
| 1376 | class keyword enabled 'use strict' but this will be removed in a later version at FILE line 3. | ||||||||||||||
| 1377 | class keyword enabled 'use warnings' but this will be removed in a later version at FILE line 3. | ||||||||||||||
| 1378 | |||||||||||||||
| 1379 | =for highlighter language=perl | ||||||||||||||
| 1380 | |||||||||||||||
| 1381 | For newly-written code in a modern style, remember that a handy way to enable | ||||||||||||||
| 1382 | the C<strict> and C<warnings> pragmata for the entire file at once is to use | ||||||||||||||
| 1383 | the C<use VERSION> syntax at the top of the file. A requested version of | ||||||||||||||
| 1384 | I<v5.36> or later will already perform all of the pragmata listed above. | ||||||||||||||
| 1385 | |||||||||||||||
| 1386 | use v5.36; | ||||||||||||||
| 1387 | |||||||||||||||
| 1388 | =head2 Yield True | ||||||||||||||
| 1389 | |||||||||||||||
| 1390 | B<The following behaviour is likely to be removed in a later version of this | ||||||||||||||
| 1391 | module.> | ||||||||||||||
| 1392 | |||||||||||||||
| 1393 | A C<class> statement or block will yield a true boolean value. This means that | ||||||||||||||
| 1394 | it can be used directly inside a F<.pm> file, avoiding the need to explicitly | ||||||||||||||
| 1395 | yield a true value from the end of it. | ||||||||||||||
| 1396 | |||||||||||||||
| 1397 | As with the implied pragmata above, this behaviour has also been found to be a | ||||||||||||||
| 1398 | bad design and will likely be removed soon. For now it is suggested not to | ||||||||||||||
| 1399 | rely on it and instead either use the new C<module_true> feature already part | ||||||||||||||
| 1400 | of the C<use v5.38> pragma, or on older perls simply remember to put an | ||||||||||||||
| 1401 | explicit true value at the end of the file. | ||||||||||||||
| 1402 | |||||||||||||||
| 1403 | =head1 SUBCLASSING CLASSIC PERL CLASSES | ||||||||||||||
| 1404 | |||||||||||||||
| 1405 | There are a number of details specific to the case of deriving an | ||||||||||||||
| 1406 | C<Object::Pad> class from an existing classic Perl class that is not | ||||||||||||||
| 1407 | implemented using C<Object::Pad>. | ||||||||||||||
| 1408 | |||||||||||||||
| 1409 | =head2 Storage of Instance Data | ||||||||||||||
| 1410 | |||||||||||||||
| 1411 | Instances will pick either the C<:repr(HASH)> or C<:repr(magic)> storage type. | ||||||||||||||
| 1412 | |||||||||||||||
| 1413 | =head2 Object State During Methods Invoked By Superclass Constructor | ||||||||||||||
| 1414 | |||||||||||||||
| 1415 | It is common in classic Perl OO style to invoke methods on C<$self> during | ||||||||||||||
| 1416 | the constructor. This is supported here since C<Object::Pad> version 0.19. | ||||||||||||||
| 1417 | Note however that any methods invoked by the superclass constructor may not | ||||||||||||||
| 1418 | see the object in a fully consistent state. (This fact is not specific to | ||||||||||||||
| 1419 | using C<Object::Pad> and would happen in classic Perl OO as well). The field | ||||||||||||||
| 1420 | initialisers will have been invoked but the C<BUILD> and C<ADJUST> phasers | ||||||||||||||
| 1421 | will not. | ||||||||||||||
| 1422 | |||||||||||||||
| 1423 | For example; in the following | ||||||||||||||
| 1424 | |||||||||||||||
| 1425 | package ClassicPerlBaseClass { | ||||||||||||||
| 1426 | sub new { | ||||||||||||||
| 1427 | my $self = bless {}, shift; | ||||||||||||||
| 1428 | say "Value seen by superconstructor is ", $self->get_value; | ||||||||||||||
| 1429 | return $self; | ||||||||||||||
| 1430 | } | ||||||||||||||
| 1431 | sub get_value { return "A" } | ||||||||||||||
| 1432 | } | ||||||||||||||
| 1433 | |||||||||||||||
| 1434 | class DerivedClass :isa(ClassicPerlBaseClass) { | ||||||||||||||
| 1435 | field $_value = "B"; | ||||||||||||||
| 1436 | ADJUST { | ||||||||||||||
| 1437 | $_value = "C"; | ||||||||||||||
| 1438 | } | ||||||||||||||
| 1439 | method get_value { return $_value } | ||||||||||||||
| 1440 | } | ||||||||||||||
| 1441 | |||||||||||||||
| 1442 | my $obj = DerivedClass->new; | ||||||||||||||
| 1443 | say "Value seen by user is ", $obj->get_value; | ||||||||||||||
| 1444 | |||||||||||||||
| 1445 | Until the C<ClassicPerlBaseClass::new> superconstructor has returned the | ||||||||||||||
| 1446 | C<ADJUST> phaser will not have been invoked. The C<$_value> field will still | ||||||||||||||
| 1447 | exist, but its value will be C<B> during the superconstructor. After the | ||||||||||||||
| 1448 | superconstructor, the C<BUILD> and C<ADJUST> phasers are invoked before the | ||||||||||||||
| 1449 | completed object is returned to the user. The result will therefore be: | ||||||||||||||
| 1450 | |||||||||||||||
| 1451 | =for highlighter | ||||||||||||||
| 1452 | |||||||||||||||
| 1453 | Value seen by superconstructor is B | ||||||||||||||
| 1454 | Value seen by user is C | ||||||||||||||
| 1455 | |||||||||||||||
| 1456 | =for highlighter language=perl | ||||||||||||||
| 1457 | |||||||||||||||
| 1458 | =head1 STYLE SUGGESTIONS | ||||||||||||||
| 1459 | |||||||||||||||
| 1460 | While in no way required, the following suggestions of code style should be | ||||||||||||||
| 1461 | noted in order to establish a set of best practices, and encourage consistency | ||||||||||||||
| 1462 | of code which uses this module. | ||||||||||||||
| 1463 | |||||||||||||||
| 1464 | =head2 $VERSION declaration | ||||||||||||||
| 1465 | |||||||||||||||
| 1466 | While it would be nice for CPAN and other toolchain modules to parse the | ||||||||||||||
| 1467 | embedded version declarations in C<class> statements, the current state at | ||||||||||||||
| 1468 | time of writing (June 2020) is that none of them actually do. As such, it will | ||||||||||||||
| 1469 | still be necessary to make a once-per-file C<$VERSION> declaration in syntax | ||||||||||||||
| 1470 | those modules can parse. | ||||||||||||||
| 1471 | |||||||||||||||
| 1472 | Further note that these modules will also not parse the C<class> declaration, | ||||||||||||||
| 1473 | so you will have to duplicate this with a C<package> declaration as well as a | ||||||||||||||
| 1474 | C<class> keyword. This does involve repeating the package name, so is slightly | ||||||||||||||
| 1475 | undesirable. | ||||||||||||||
| 1476 | |||||||||||||||
| 1477 | It is hoped that eventually upstream toolchain modules will be adapted to | ||||||||||||||
| 1478 | accept the C<class> syntax as being sufficient to declare a package and set | ||||||||||||||
| 1479 | its version. | ||||||||||||||
| 1480 | |||||||||||||||
| 1481 | See also | ||||||||||||||
| 1482 | |||||||||||||||
| 1483 | =over 2 | ||||||||||||||
| 1484 | |||||||||||||||
| 1485 | =item * | ||||||||||||||
| 1486 | |||||||||||||||
| 1487 | L<https://github.com/Perl-Toolchain-Gang/Module-Metadata/issues/33> | ||||||||||||||
| 1488 | |||||||||||||||
| 1489 | =back | ||||||||||||||
| 1490 | |||||||||||||||
| 1491 | =head2 File Layout | ||||||||||||||
| 1492 | |||||||||||||||
| 1493 | Begin the file with a C<use Object::Pad> line; ideally including a | ||||||||||||||
| 1494 | minimum-required version. This should be followed by the toplevel C<package> | ||||||||||||||
| 1495 | and C<class> declarations for the file. As it is at toplevel there is no need | ||||||||||||||
| 1496 | to use the block notation; it can be a unit class. | ||||||||||||||
| 1497 | |||||||||||||||
| 1498 | There is no need to C<use strict> or apply other usual pragmata; these will | ||||||||||||||
| 1499 | be implied by the C<class> keyword. | ||||||||||||||
| 1500 | |||||||||||||||
| 1501 | use Object::Pad 0.16; | ||||||||||||||
| 1502 | |||||||||||||||
| 1503 | package My::Classname 1.23; | ||||||||||||||
| 1504 | class My::Classname; | ||||||||||||||
| 1505 | |||||||||||||||
| 1506 | # other use statements | ||||||||||||||
| 1507 | |||||||||||||||
| 1508 | # field, methods, etc.. can go here | ||||||||||||||
| 1509 | |||||||||||||||
| 1510 | =head2 Field Names | ||||||||||||||
| 1511 | |||||||||||||||
| 1512 | Field names should follow similar rules to regular lexical variables in code - | ||||||||||||||
| 1513 | lowercase, name components separated by underscores. For tiny examples such as | ||||||||||||||
| 1514 | "dumb record" structures this may be sufficient. | ||||||||||||||
| 1515 | |||||||||||||||
| 1516 | class Tag { | ||||||||||||||
| 1517 | field $name :mutator; | ||||||||||||||
| 1518 | field $value :mutator; | ||||||||||||||
| 1519 | } | ||||||||||||||
| 1520 | |||||||||||||||
| 1521 | In larger examples with lots of non-trivial method bodies, it can get | ||||||||||||||
| 1522 | confusing to remember where the field variables come from (because we no | ||||||||||||||
| 1523 | longer have the C<< $self->{ ... } >> visual clue). In these cases it is | ||||||||||||||
| 1524 | suggested to prefix the field names with a leading underscore, to make them | ||||||||||||||
| 1525 | more visually distinct. | ||||||||||||||
| 1526 | |||||||||||||||
| 1527 | class Spudger { | ||||||||||||||
| 1528 | field $_grapefruit; | ||||||||||||||
| 1529 | |||||||||||||||
| 1530 | ... | ||||||||||||||
| 1531 | |||||||||||||||
| 1532 | method mangle { | ||||||||||||||
| 1533 | $_grapefruit->peel; # The leading underscore reminds us this is a field | ||||||||||||||
| 1534 | } | ||||||||||||||
| 1535 | } | ||||||||||||||
| 1536 | |||||||||||||||
| 1537 | =cut | ||||||||||||||
| 1538 | |||||||||||||||
| 1539 | sub VERSION | ||||||||||||||
| 1540 | 56 | ▶ | { | ||||||||||||
|
Subroutine
VERSION: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||
| 1541 | 56 | my $pkg = shift; | |||||||||||||
| 1542 | |||||||||||||||
| 1543 | 56 | my $ret = $pkg->SUPER::VERSION( @_ ); | |||||||||||||
| 1544 | |||||||||||||||
| 1545 | 56 | ▶ | if( @_ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 1546 | 56 | my $ver = version->parse( @_ ); | |||||||||||||
| 1547 | |||||||||||||||
| 1548 | # Only bother to store it if it's >= v0.800 | ||||||||||||||
| 1549 | 56 | ▶ | $^H{"Object::Pad/imported-version"} = $ver->numify if $ver ge v0.800; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 1550 | } | ||||||||||||||
| 1551 | |||||||||||||||
| 1552 | 56 | return $ret; | |||||||||||||
| 1553 | } | ||||||||||||||
| 1554 | |||||||||||||||
| 1555 | sub import | ||||||||||||||
| 1556 | 60 | ▶ | { | ||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||
| 1557 | 60 | my $class = shift; | |||||||||||||
| 1558 | 60 | my $caller = caller; | |||||||||||||
| 1559 | |||||||||||||||
| 1560 | 60 | $class->import_into( $caller, @_ ); | |||||||||||||
| 1561 | } | ||||||||||||||
| 1562 | |||||||||||||||
| 1563 | sub _import_experimental | ||||||||||||||
| 1564 | 65 | ▶ | { | ||||||||||||
|
Subroutine
_import_experimental: called CC=11 SCAR=26.9
| |||||||||||||||
| 1565 | 65 | shift; | |||||||||||||
| 1566 | 65 | my ( $syms, @experiments ) = @_; | |||||||||||||
| 1567 | |||||||||||||||
| 1568 | 65 | my %enabled; | |||||||||||||
| 1569 | |||||||||||||||
| 1570 | 65 | my $i = 0; | |||||||||||||
| 1571 | 65 | ▶ | while( $i < @$syms ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 1572 | 22 | my $sym = $syms->[$i]; | |||||||||||||
| 1573 | |||||||||||||||
| 1574 | 22 | ▶ | if( $sym eq ":experimental" ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 1575 | 0 | carp "Enabling all Object::Pad experiments with an unqualified :experimental"; | |||||||||||||
| 1576 | 0 | $enabled{$_}++ for @experiments; | |||||||||||||
| 1577 | } | ||||||||||||||
| 1578 | elsif( $sym =~ m/^:experimental\((.*)\)$/ ) { | ||||||||||||||
| 1579 | 22 | foreach my $tag ( split m/\s+/, $1 =~ s/^\s+|\s+$//gr ) { | |||||||||||||
| 1580 | 25 | ▶ | if( $tag eq ":all" ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 1581 | 0 | $enabled{$_}++ for @experiments; | |||||||||||||
| 1582 | } | ||||||||||||||
| 1583 | else { | ||||||||||||||
| 1584 | 25 | $enabled{$tag}++; | |||||||||||||
| 1585 | } | ||||||||||||||
| 1586 | } | ||||||||||||||
| 1587 | } | ||||||||||||||
| 1588 | else { | ||||||||||||||
| 1589 | 0 | $i++; | |||||||||||||
| 1590 | 0 | next; | |||||||||||||
| 1591 | } | ||||||||||||||
| 1592 | |||||||||||||||
| 1593 | 22 | splice @$syms, $i, 1, (); | |||||||||||||
| 1594 | } | ||||||||||||||
| 1595 | |||||||||||||||
| 1596 | 65 | foreach ( @experiments ) { | |||||||||||||
| 1597 | 513 | ▶ | $^H{"Object::Pad/experimental($_)"}++ if delete $enabled{$_}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 1598 | } | ||||||||||||||
| 1599 | |||||||||||||||
| 1600 | 0 | ▶ | croak "Unrecognised :experimental features @{[ keys %enabled ]}" if keys %enabled; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 1601 | } | ||||||||||||||
| 1602 | |||||||||||||||
| 1603 | sub _import_configuration | ||||||||||||||
| 1604 | 64 | ▶ | { | ||||||||||||
|
Subroutine
_import_configuration: called CC=5 SCAR=27.8
| |||||||||||||||
| 1605 | 64 | shift; | |||||||||||||
| 1606 | 64 | my ( $syms ) = @_; | |||||||||||||
| 1607 | |||||||||||||||
| 1608 | # Undocumented options, purely to support Feature::Compat::Class adjusting | ||||||||||||||
| 1609 | # the behaviour to closer match core's use feature 'class' | ||||||||||||||
| 1610 | |||||||||||||||
| 1611 | 64 | my $i = 0; | |||||||||||||
| 1612 | 64 | ▶ | while( $i < @$syms ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 1613 | 0 | my $sym = $syms->[$i]; | |||||||||||||
| 1614 | |||||||||||||||
| 1615 | 0 | ▶ | if( $sym =~ m/^:config\((.*)\)$/ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 1616 | 0 | foreach my $opt ( split m/\s+/, $1 =~ s/^\s+|\s+$//gr ) { | |||||||||||||
| 1617 | 0 | ▶ | if( $opt =~ m/^(only_class_attrs|only_field_attrs)=(.*)$/ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 1618 | # Store an entire sub-hash inside the hints hash. This won't | ||||||||||||||
| 1619 | # survive squashing into a COP for runtime but we only need it | ||||||||||||||
| 1620 | # during compile so that's OK | ||||||||||||||
| 1621 | 0 | my ( $name, $attrs ) = ( $1, $2 ); | |||||||||||||
| 1622 | 0 | $^H{"Object::Pad/configure($name)"} = { map { $_ => 1 } split m/,/, $attrs }; | |||||||||||||
| 1623 | } | ||||||||||||||
| 1624 | else { | ||||||||||||||
| 1625 | 0 | $^H{"Object::Pad/configure($opt)"}++ | |||||||||||||
| 1626 | } | ||||||||||||||
| 1627 | } | ||||||||||||||
| 1628 | } | ||||||||||||||
| 1629 | else { | ||||||||||||||
| 1630 | 0 | $i++; | |||||||||||||
| 1631 | 0 | next; | |||||||||||||
| 1632 | } | ||||||||||||||
| 1633 | |||||||||||||||
| 1634 | 0 | splice @$syms, $i, 1, (); | |||||||||||||
| 1635 | } | ||||||||||||||
| 1636 | } | ||||||||||||||
| 1637 | |||||||||||||||
| 1638 | sub import_into | ||||||||||||||
| 1639 | 64 | ▶ | { | ||||||||||||
|
Subroutine
import_into: called CC=6 SCAR=19.3
Pod
undocumented
| |||||||||||||||
| 1640 | 64 | my $class = shift; | |||||||||||||
| 1641 | 64 | my $caller = shift; | |||||||||||||
| 1642 | |||||||||||||||
| 1643 | 64 | $class->_import_experimental( \@_, qw( init_expr mop custom_field_attr adjust_params composed_adjust inherit_field apply_phaser lexical_class ) ); | |||||||||||||
| 1644 | |||||||||||||||
| 1645 | 64 | $class->_import_configuration( \@_ ); | |||||||||||||
| 1646 | |||||||||||||||
| 1647 | 0 | my %syms = map { $_ => 1 } @_; | |||||||||||||
| 1648 | |||||||||||||||
| 1649 | # Default imports | ||||||||||||||
| 1650 | 64 | ▶ | unless( %syms ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 1651 | 64 | $syms{$_}++ for qw( class role inherit apply method field has requires BUILD ADJUST APPLY ); | |||||||||||||
| 1652 | } | ||||||||||||||
| 1653 | |||||||||||||||
| 1654 | 64 | ▶ | delete $syms{$_} and $^H{"Object::Pad/$_"}++ for qw( class role inherit apply method field has requires BUILD ADJUST APPLY ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 1655 | |||||||||||||||
| 1656 | 0 | ▶ | croak "Unrecognised import symbols @{[ keys %syms ]}" if keys %syms; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 1657 | } | ||||||||||||||
| 1658 | |||||||||||||||
| 1659 | # The universal base-class methods | ||||||||||||||
| 1660 | |||||||||||||||
| 1661 | sub Object::Pad::UNIVERSAL::BUILDARGS | ||||||||||||||
| 1662 | 1 | ▶ | { | ||||||||||||
|
Subroutine
BUILDARGS: called CC=1 SCAR=0.0
| |||||||||||||||
| 1663 | 1 | shift; # $class | |||||||||||||
| 1664 | 1 | return @_; | |||||||||||||
| 1665 | } | ||||||||||||||
| 1666 | |||||||||||||||
| 1667 | # Back-compat wrapper | ||||||||||||||
| 1668 | sub Object::Pad::MOP::SlotAttr::register | ||||||||||||||
| 1669 | 0 | ▶ | { | ||||||||||||
|
Subroutine
register: not called CC=1 SCAR=6.9
| |||||||||||||||
| 1670 | 0 | shift; # $class | |||||||||||||
| 1671 | 0 | croak "Object::Pad::MOP::SlotAttr->register is now removed; use Object::Pad::MOP::FieldAttr->register instead"; | |||||||||||||
| 1672 | } | ||||||||||||||
| 1673 | |||||||||||||||
| 1674 | =head1 WITH OTHER MODULES | ||||||||||||||
| 1675 | |||||||||||||||
| 1676 | =head2 Syntax::Keyword::Dynamically | ||||||||||||||
| 1677 | |||||||||||||||
| 1678 | A cross-module integration test asserts that C<dynamically> works correctly | ||||||||||||||
| 1679 | on object instance fields: | ||||||||||||||
| 1680 | |||||||||||||||
| 1681 | use Object::Pad; | ||||||||||||||
| 1682 | use Syntax::Keyword::Dynamically; | ||||||||||||||
| 1683 | |||||||||||||||
| 1684 | class Container { | ||||||||||||||
| 1685 | field $value = 1; | ||||||||||||||
| 1686 | |||||||||||||||
| 1687 | method example { | ||||||||||||||
| 1688 | dynamically $value = 2; | ||||||||||||||
| 1689 | ,.. | ||||||||||||||
| 1690 | # value is restored to 1 on return from this method | ||||||||||||||
| 1691 | } | ||||||||||||||
| 1692 | } | ||||||||||||||
| 1693 | |||||||||||||||
| 1694 | =head2 Future::AsyncAwait | ||||||||||||||
| 1695 | |||||||||||||||
| 1696 | As of L<Future::AsyncAwait> version 0.38 and L<Object::Pad> version 0.15, both | ||||||||||||||
| 1697 | modules now use L<XS::Parse::Sublike> to parse blocks of code. Because of this | ||||||||||||||
| 1698 | the two modules can operate together and allow class methods to be written as | ||||||||||||||
| 1699 | async subs which await expressions: | ||||||||||||||
| 1700 | |||||||||||||||
| 1701 | use Future::AsyncAwait; | ||||||||||||||
| 1702 | use Object::Pad; | ||||||||||||||
| 1703 | |||||||||||||||
| 1704 | class Example | ||||||||||||||
| 1705 | { | ||||||||||||||
| 1706 | async method perform ($block) | ||||||||||||||
| 1707 | { | ||||||||||||||
| 1708 | say "$self is performing code"; | ||||||||||||||
| 1709 | await $block->(); | ||||||||||||||
| 1710 | say "code finished"; | ||||||||||||||
| 1711 | } | ||||||||||||||
| 1712 | } | ||||||||||||||
| 1713 | |||||||||||||||
| 1714 | These three modules combine; there is additionally a cross-module test to | ||||||||||||||
| 1715 | ensure that object instance fields can be C<dynamically> set during a | ||||||||||||||
| 1716 | suspended C<async method>. | ||||||||||||||
| 1717 | |||||||||||||||
| 1718 | =head2 Devel::MAT | ||||||||||||||
| 1719 | |||||||||||||||
| 1720 | When using L<Devel::MAT> to help analyse or debug memory issues with programs | ||||||||||||||
| 1721 | that use C<Object::Pad>, you will likely want to additionally install the | ||||||||||||||
| 1722 | module L<Devel::MAT::Tool::Object::Pad>. This will provide new commands and | ||||||||||||||
| 1723 | extend existing ones to better assist with analysing details related to | ||||||||||||||
| 1724 | C<Object::Pad> classes and instances of them. | ||||||||||||||
| 1725 | |||||||||||||||
| 1726 | =for highlighter | ||||||||||||||
| 1727 | |||||||||||||||
| 1728 | pmat> fields 0x55d7c173d4b8 | ||||||||||||||
| 1729 | The field AV ARRAY(3)=NativeClass at 0x55d7c173d4b8 | ||||||||||||||
| 1730 | Ix Field Value | ||||||||||||||
| 1731 | 0 $sfield SCALAR(UV) at 0x55d7c173d938 = 123 | ||||||||||||||
| 1732 | ... | ||||||||||||||
| 1733 | |||||||||||||||
| 1734 | pmat> identify 0x55d7c17606d8 | ||||||||||||||
| 1735 | REF() at 0x55d7c17606d8 is: | ||||||||||||||
| 1736 | └─the %hfield field of ARRAY(3)=NativeClass at 0x55d7c173d4b8, which is: | ||||||||||||||
| 1737 | ... | ||||||||||||||
| 1738 | |||||||||||||||
| 1739 | =for highlighter language=perl | ||||||||||||||
| 1740 | |||||||||||||||
| 1741 | =head1 DESIGN TODOs | ||||||||||||||
| 1742 | |||||||||||||||
| 1743 | The following points are details about the design of pad field-based object | ||||||||||||||
| 1744 | systems in general: | ||||||||||||||
| 1745 | |||||||||||||||
| 1746 | =over 4 | ||||||||||||||
| 1747 | |||||||||||||||
| 1748 | =item * | ||||||||||||||
| 1749 | |||||||||||||||
| 1750 | Is multiple inheritance actually required, if role composition is implemented | ||||||||||||||
| 1751 | including giving roles the ability to use private fields? | ||||||||||||||
| 1752 | |||||||||||||||
| 1753 | =item * | ||||||||||||||
| 1754 | |||||||||||||||
| 1755 | Consider the visibility of superclass fields to subclasses. Do subclasses | ||||||||||||||
| 1756 | even need to be able to see their superclass's fields, or are accessor methods | ||||||||||||||
| 1757 | always appropriate? | ||||||||||||||
| 1758 | |||||||||||||||
| 1759 | Concrete example: The C<< $self->{split_at} >> access that | ||||||||||||||
| 1760 | L<Tickit::Widget::HSplit> makes of its parent class | ||||||||||||||
| 1761 | L<Tickit::Widget::LinearSplit>. | ||||||||||||||
| 1762 | |||||||||||||||
| 1763 | =back | ||||||||||||||
| 1764 | |||||||||||||||
| 1765 | =head1 IMPLEMENTATION TODOs | ||||||||||||||
| 1766 | |||||||||||||||
| 1767 | These points are more about this particular module's implementation: | ||||||||||||||
| 1768 | |||||||||||||||
| 1769 | =over 4 | ||||||||||||||
| 1770 | |||||||||||||||
| 1771 | =item * | ||||||||||||||
| 1772 | |||||||||||||||
| 1773 | Consider multiple inheritance of subclassing, if that is still considered | ||||||||||||||
| 1774 | useful after adding roles. | ||||||||||||||
| 1775 | |||||||||||||||
| 1776 | =item * | ||||||||||||||
| 1777 | |||||||||||||||
| 1778 | Work out why C<no indirect> doesn't appear to work properly before perl 5.20. | ||||||||||||||
| 1779 | |||||||||||||||
| 1780 | =item * | ||||||||||||||
| 1781 | |||||||||||||||
| 1782 | Work out why we don't get a C<Subroutine new redefined at ...> warning if we | ||||||||||||||
| 1783 | |||||||||||||||
| 1784 | sub new { ... } | ||||||||||||||
| 1785 | |||||||||||||||
| 1786 | =item * | ||||||||||||||
| 1787 | |||||||||||||||
| 1788 | The C<local> modifier does not work on field variables, because they appear to | ||||||||||||||
| 1789 | be regular lexicals to the parser at that point. A workaround is to use | ||||||||||||||
| 1790 | L<Syntax::Keyword::Dynamically> instead: | ||||||||||||||
| 1791 | |||||||||||||||
| 1792 | use Syntax::Keyword::Dynamically; | ||||||||||||||
| 1793 | |||||||||||||||
| 1794 | field $loglevel; | ||||||||||||||
| 1795 | |||||||||||||||
| 1796 | method quietly { | ||||||||||||||
| 1797 | dynamically $loglevel = LOG_ERROR; | ||||||||||||||
| 1798 | ... | ||||||||||||||
| 1799 | } | ||||||||||||||
| 1800 | |||||||||||||||
| 1801 | =back | ||||||||||||||
| 1802 | |||||||||||||||
| 1803 | =cut | ||||||||||||||
| 1804 | |||||||||||||||
| 1805 | =head1 FEEDBACK | ||||||||||||||
| 1806 | |||||||||||||||
| 1807 | The following resources are useful forms of providing feedback, especially in | ||||||||||||||
| 1808 | the form of reports of what you find good or bad about the module, requests | ||||||||||||||
| 1809 | for new features, questions on best practice, etc... | ||||||||||||||
| 1810 | |||||||||||||||
| 1811 | =over 4 | ||||||||||||||
| 1812 | |||||||||||||||
| 1813 | =item * | ||||||||||||||
| 1814 | |||||||||||||||
| 1815 | The RT queue at L<https://rt.cpan.org/Dist/Display.html?Name=Object-Pad>. | ||||||||||||||
| 1816 | |||||||||||||||
| 1817 | =item * | ||||||||||||||
| 1818 | |||||||||||||||
| 1819 | The C<#cor> IRC channel on C<irc.perl.org>. | ||||||||||||||
| 1820 | |||||||||||||||
| 1821 | =back | ||||||||||||||
| 1822 | |||||||||||||||
| 1823 | =cut | ||||||||||||||
| 1824 | |||||||||||||||
| 1825 | =head1 SPONSORS | ||||||||||||||
| 1826 | |||||||||||||||
| 1827 | With thanks to the following sponsors, who have helped me be able to spend | ||||||||||||||
| 1828 | time working on this module and other perl features. | ||||||||||||||
| 1829 | |||||||||||||||
| 1830 | =over 4 | ||||||||||||||
| 1831 | |||||||||||||||
| 1832 | =item * | ||||||||||||||
| 1833 | |||||||||||||||
| 1834 | Oetiker+Partner AG L<https://www.oetiker.ch/en/> | ||||||||||||||
| 1835 | |||||||||||||||
| 1836 | =item * | ||||||||||||||
| 1837 | |||||||||||||||
| 1838 | Deriv L<http://deriv.com> | ||||||||||||||
| 1839 | |||||||||||||||
| 1840 | =item * | ||||||||||||||
| 1841 | |||||||||||||||
| 1842 | Perl-Verein Schweiz L<https://www.perl-workshop.ch/> | ||||||||||||||
| 1843 | |||||||||||||||
| 1844 | =back | ||||||||||||||
| 1845 | |||||||||||||||
| 1846 | Additional details may be found at | ||||||||||||||
| 1847 | L<https://github.com/Ovid/Cor/wiki/Sponsors>. | ||||||||||||||
| 1848 | |||||||||||||||
| 1849 | =cut | ||||||||||||||
| 1850 | |||||||||||||||
| 1851 | =head1 AUTHOR | ||||||||||||||
| 1852 | |||||||||||||||
| 1853 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||
| 1854 | |||||||||||||||
| 1855 | =cut | ||||||||||||||
| 1856 | |||||||||||||||
| 1857 | 58 | 0x55AA; | |||||||||||||