1Object::Pad(3)        User Contributed Perl Documentation       Object::Pad(3)
2
3
4

NAME

6       "Object::Pad" - a simple syntax for lexical slot-based objects
7

SYNOPSIS

9       On perl version 5.26 onwards:
10
11          use v5.26;
12          use Object::Pad;
13
14          class Point {
15             has $x :param = 0;
16             has $y :param = 0;
17
18             method move ($dX, $dY) {
19                $x += $dX;
20                $y += $dY;
21             }
22
23             method describe () {
24                print "A point at ($x, $y)\n";
25             }
26          }
27
28          Point->new(x => 5, y => 10)->describe;
29
30       Or, for older perls that lack signatures:
31
32          use Object::Pad;
33
34          class Point {
35             has $x :param = 0;
36             has $y :param = 0;
37
38             method move {
39                my ($dX, $dY) = @_;
40                $x += $dX;
41                $y += $dY;
42             }
43
44             method describe {
45                print "A point at ($x, $y)\n";
46             }
47          }
48
49          Point->new(x => 5, y => 10)->describe;
50

DESCRIPTION

52       This module provides a simple syntax for creating object classes, which
53       uses private variables that look like lexicals as object member fields.
54
55       WARNING This module is still experimental. The parts that currently
56       exist do seem to work reliably but much of the design is still
57       evolving, and many features and have yet to be implemented. I don't yet
58       guarantee I won't have to change existing details in order to continue
59       its development. Feel free to try it out in experimental or newly-
60       developed code, but don't complain if a later version is incompatible
61       with your current code and you'll have to change it.
62
63       That all said, please do get in contact if you find the module overall
64       useful.  The more feedback you provide in terms of what features you
65       are using, what you find works, and what doesn't, will help the ongoing
66       development and hopefully eventual stability of the design. See the
67       "FEEDBACK" section.
68
69   Automatic Construction
70       Classes are automatically provided with a constructor method, called
71       "new", which helps create the object instances. This may respond to
72       passed arguments, automatically assigning values of slots, and invoking
73       other blocks of code provided by the class. It proceeds in the
74       following stages:
75
76       The BUILDARGS phase
77
78       If the class provides a "BUILDARGS" class method, that is used to
79       mangle the list of arguments before the "BUILD" blocks are called. Note
80       this must be a class method not an instance method (and so implemented
81       using "sub"). It should perform any "SUPER" chaining as may be
82       required.
83
84          @args = $class->BUILDARGS( @_ )
85
86       Slot assignment
87
88       If any slot in the class has the ":param" attribute, then the
89       constructor will expect to receive its argmuents in an even-sized list
90       of name/value pairs. This applies even to slots inherited from the
91       parent class or applied roles. It is therefore a good idea to shape the
92       parameters to the constructor in this way in roles, and in classes if
93       you intend your class to be extended.
94
95       The constructor will also check for required parameters (these are all
96       the parameters for slots that do not have default initialisation
97       expressions). If any of these are missing an exception is thrown.
98
99       The BUILD phase
100
101       As part of the construction process, the "BUILD" block of every
102       component class will be invoked, passing in the list of arguments the
103       constructor was invoked with. Each class should perform its required
104       setup behaviour, but does not need to chain to the "SUPER" class first;
105       this is handled automatically.
106
107       The ADJUST phase
108
109       Next, the "ADJUST" and "ADJUSTPARAMS" block of every component class is
110       invoked. This happens after the slots are assigned their initial values
111       and the "BUILD" blocks have been run.
112
113       Note also that both "ADJUST" and "ADJUSTPARAMS" blocks happen at the
114       same time, in declaration order. The "ADJUSTPARAMS" blocks do not form
115       their own separate phase.
116
117       The strict-checking phase
118
119       Finally, before the object is returned, if the ":strict(params)" class
120       attribute is present, then the constructor will throw an exception if
121       there are any remaining named arguments left over after assigning them
122       to slots as per ":param" declarations, and running any "ADJUSTPARAMS"
123       blocks.
124

KEYWORDS

126   class
127          class Name :ATTRS... {
128             ...
129          }
130
131          class Name :ATTRS...;
132
133       Behaves similarly to the "package" keyword, but provides a package that
134       defines a new class. Such a class provides an automatic constructor
135       method called "new".
136
137       As with "package", an optional block may be provided. If so, the
138       contents of that block define the new class and the preceding package
139       continues afterwards. If not, it sets the class as the package context
140       of following keywords and definitions.
141
142       As with "package", an optional version declaration may be given. If so,
143       this sets the value of the package's $VERSION variable.
144
145          class Name VERSION { ... }
146
147          class Name VERSION;
148
149       A single superclass is supported by the keyword "isa"
150
151       Since version 0.41.
152
153          class Name isa BASECLASS {
154             ...
155          }
156
157          class Name isa BASECLASS BASEVER {
158             ...
159          }
160
161       (prior to version 0.41 this was called "extends", which is currently
162       recognised as a compatibility synonym).
163
164       If a package providing the superclass does not exist, an attempt is
165       made to load it by code equivalent to
166
167          require Animal ();
168
169       and thus it must either already exist, or be locatable via the usual
170       @INC mechanisms.
171
172       The superclass may or may not itself be implemented by "Object::Pad",
173       but if it is not then see "SUBCLASSING CLASSIC PERL CLASSES" for
174       further detail on the semantics of how this operates.
175
176       An optional version check can also be supplied; it performs the
177       equivalent of
178
179          BaseClass->VERSION( $ver )
180
181       One or more roles can be composed into the class by the keyword "does"
182
183       Since version 0.41.
184
185          class Name does ROLE, ROLE,... {
186             ...
187          }
188
189       (prior to version 0.41 this was called "implements", which is currently
190       recognised as a compatibility synonym).
191
192       An optional list of attributes may be supplied in similar syntax as for
193       subs or lexical variables. (These are annotations about the class
194       itself; the concept should not be confused with per-object-instance
195       data, which here is called "slots"). The following class attributes are
196       supported:
197
198       :repr(TYPE)
199
200       Sets the representation type for instances of this class. Must be one
201       of the following values:
202
203          :repr(native)
204
205       The native representation. This is an opaque representation type whose
206       contents are not specified. It only works for classes whose entire
207       inheritence hierarchy is built only from classes based on
208       "Object::Pad".
209
210          :repr(HASH)
211
212       The representation will be a blessed hash reference. The instance data
213       will be stored in an array referenced by a key called
214       "Object::Pad/slots", which is fairly unlikely to clash with existing
215       storage on the instance. No other keys will be used; they are available
216       for implementions and subclasses to use.  The exact format of the value
217       stored here is not specified and may change between module versions,
218       though it can be relied on to be well-behaved as some kind of perl data
219       structure for purposes of modules like Data::Dumper or serialisation
220       into things like "YAML" or "JSON".
221
222       This representation type may be useful when converting existing classes
223       into using "Object::Pad" where there may be existing subclasses of it
224       that presume a blessed hash for their own use.
225
226          :repr(magic)
227
228       The representation will use MAGIC to apply the instance data in a way
229       that is invisible at the Perl level, and shouldn't get in the way of
230       other things the instance is doing even in XS modules.
231
232       This representation type is the only one that will work for subclassing
233       existing classes that do not use blessed hashes.
234
235          :repr(autoselect), :repr(default)
236
237       Since version 0.23.
238
239       This representation will select one of the representations above
240       depending on what is best for the situation. Classes not derived from a
241       non-"Object::Pad" base class will pick "native", and classes derived
242       from non-"Object::Pad" bases will pick either the "HASH" or "magic"
243       forms depending on whether the instance is a blessed hash reference or
244       some other kind.
245
246       This achieves the best combination of DWIM while still allowing the
247       common forms of hash reference to be inspected by "Data::Dumper", etc.
248       This is the default representation type, and does not have to be
249       specifically requested.
250
251       :strict(params)
252
253       Since version 0.43.
254
255       Can only be applied to classes that contain no "BUILD" blocks. If set,
256       then the constructor will complain about any unrecognised named
257       arguments passed to it (i.e. names that do not correspond to the
258       ":param" of any defined slot and left unconsumed by any "ADJUSTPARAMS"
259       block).
260
261       Since "BUILD" blocks can inspect the arguments arbitrarily, the
262       presence of any such block means the constructor cannot determine which
263       named arguments are not recognised.
264
265       This attribute is a temporary stepping-stone for compatibility with
266       existing code. It is recommended to enable this whenever possible, as a
267       later version of this module will likely perform this behaviour
268       unconditionally whenever no "BUILD" blocks are present.
269
270   role
271          role Name :ATTRS... {
272             ...
273          }
274
275          role Name :ATTRS...;
276
277       Since version 0.32.
278
279       Similar to "class", but provides a package that defines a new role. A
280       role acts simliar to a class in some respects, and differently in
281       others.
282
283       Like a class, a role can have a version, and named methods.
284
285          role Name VERSION {
286             method a { ... }
287             method b { ... }
288          }
289
290       A role does not provide a constructor, and instances cannot directly be
291       constructed. A role cannot extend a class.
292
293       A role can declare that it requires methods of given names from any
294       class that implements the role.
295
296          role Name {
297             requires METHOD;
298          }
299
300       A role can provide instance slots. These are visible to any "BUILD"
301       blocks or methods provided by that role.
302
303       Since version 0.33.
304
305          role Name {
306             has $slot;
307
308             BUILD { $slot = "a value" }
309
310             method slot { return $slot }
311          }
312
313       The following role attributes are supported:
314
315       :compat(invokable)
316
317       Since version 0.35.
318
319       Enables a form of backward-compatibility behaviour useful for gradually
320       upgrading existing code from classical Perl inheritance or mixins into
321       using roles.
322
323       Normally, methods of a role cannot be directly invoked and the role
324       must be applied to an Object::Pad-based class in order to be used. This
325       however presents a problem when gradually upgrading existing code that
326       already uses techniques like roles, multiple inheritance or mixins when
327       that code may be split across multiple distributions, or for some other
328       reason cannot be upgraded all at once. Methods within a role that has
329       the ":compat(invokable)" attribute applied to it may be directly
330       invoked on any object instance. This allows the creation of a role that
331       can still provide code for existing classes written in classical Perl
332       that has not yet been rewritten to use "Object::Pad".
333
334       The tradeoff is that a ":compat(invokable)" role may not create slot
335       data using the "has" keyword. Whatever behaviours the role wishes to
336       perform must be provided only by calling other methods on $self, or
337       perhaps by making assumptions about the representation type of
338       instances.
339
340       It should be stressed again: This option is only intended for gradual
341       upgrade of existing classical Perl code into using "Object::Pad". When
342       all existing code is using "Object::Pad" then this attribute can be
343       removed from the role.
344
345   has
346          has $var;
347          has @var;
348          has %var;
349
350          has $var :ATTR ATTR...;
351
352          has $var = EXPR;
353
354          has $var { BLOCK };
355
356       Declares that the instances of the class or role have a member field of
357       the given name. This member field (called a "slot") will be accessible
358       as a lexical variable within any "method" declarations in the class.
359
360       Array and hash members are permitted and behave as expected; you do not
361       need to store references to anonymous arrays or hashes.
362
363       Member fields are private to a class or role. They are not visible to
364       users of the class, nor to subclasses, nor to any class that a role is
365       applied to. In order to provide access to them a class may wish to use
366       "method" to create an accessor, or use the attributes such as ":reader"
367       to get one generated.
368
369       A scalar slot may provide a expression that gives an initialisation
370       value, which will be assigned into the slot of every instance during
371       the constructor before the "BUILD" blocks are invoked. Since version
372       0.29 this expression does not have to be a compiletime constant, though
373       it is evaluated exactly once, at runtime, after the class definition
374       has been parsed. It is not evaluated individually for every object
375       instance of that class. Since version 0.54 this is also permitted on
376       array and hash slots.
377
378       Slot Initialiser Blocks
379
380       Since version 0.54 a deferred statement block is also permitted, on any
381       slot variable type. This is an experimental feature that permits code
382       to be executed as part of the instance constructor, rather than running
383       just once when the class is set up. Code in a slot initialisation block
384       is roughly equivalent to being placed in a "BUILD" or "ADJUST" block.
385
386       Control flow that attempts to leave a slot initialiser block is not
387       permitted.  This includes any "return" expression, any "next/last/redo"
388       outside of a loop, with a dynamically-calculated label expression, or
389       with a label that it doesn't appear in. "goto" statements are also
390       currently forbidden, though known-safe ones may be permitted in future.
391
392       Loop control expressions that are known at compiletime to affect a loop
393       that they appear within are permitted.
394
395          has $slot { foreach(@list) { next; } }       # this is fine
396
397          has $slot { LOOP: while(1) { last LOOP; } }  # this is fine too
398
399       The following slot attributes are supported:
400
401       :reader, :reader(NAME)
402
403       Since version 0.27.
404
405       Generates a reader method to return the current value of the slot.
406       Currently these are only permitted for scalar slots. If no name is
407       given, the name of the slot is used. A single prefix character "_" will
408       be removed if present.
409
410          has $slot :reader;
411
412          # equivalent to
413          has $slot;  method slot { return $slot }
414
415       :writer, :writer(NAME)
416
417       Since version 0.27.
418
419       Generates a writer method to set a new value of the slot from its first
420       argument. Currently these are only permitted for scalar slots. If no
421       name is given, the name of the slot is used prefixed by "set_". A
422       single prefix character "_" will be removed if present.
423
424          has $slot :writer;
425
426          # equivalent to
427          has $slot;  method set_slot { $slot = shift; return $self }
428
429       Since version 0.28 a generated writer method will return the object
430       invocant itself, allowing a chaining style.
431
432          $obj->set_x("x")
433             ->set_y("y")
434             ->set_z("z");
435
436       :mutator, :mutator(NAME)
437
438       Since version 0.27.
439
440       Generates an lvalue mutator method to return or set the value of the
441       slot.  These are only permitted for scalar slots. If no name is given,
442       the name of the slot is used. A single prefix character "_" will be
443       removed if present.
444
445          has $slot :mutator;
446
447          # equivalent to
448          has $slot;  method slot :lvalue { $slot }
449
450       Since version 0.28 all of these generated accessor methods will include
451       argument checking similar to that used by subroutine signatures, to
452       ensure the correct number of arguments are passed - usually zero, but
453       exactly one in the case of a ":writer" method.
454
455       :accessor, :accessor(NAME)
456
457       Since version 0.53.
458
459       Generates a combined reader-writer accessor method to set or return the
460       value of the slot. These are only permitted for scalar slots. If no
461       name is given, the name of the slot is used. A prefix character "_"
462       will be removed if present.
463
464       This method takes either zero or one additional arguments. If an
465       argument is passed, the value of the slot is set from this argument
466       (even if it is "undef"). If no argument is passed (i.e. "scalar @_" is
467       false) then the slot is not modified. In either case, the value of the
468       slot is then returned.
469
470          has $slot :accessor;
471
472          # equivalent to
473          has $slot;
474
475          method slot {
476             $slot = shift if @_;
477             return $slot;
478          }
479
480       :weak
481
482       Since version 0.44.
483
484       Generated code which sets the value of this slot will weaken it if it
485       contains a reference. This applies to within the constructor if
486       ":param" is given, and to a ":writer" accessor method. Note that this
487       only applies to automatically generated code; not normal code written
488       in regular method bodies. If you assign into the slot variable you must
489       remember to call "Scalar::Util::weaken" yourself.
490
491       :param, :param(NAME)
492
493       Since version 0.41.
494
495       Sets this slot to be initialised automatically in the generated
496       constructor.  This is only permitted on scalar slots. If no name is
497       given, the name of the slot is used. A single prefix character "_" will
498       be removed if present.
499
500       Any slot that has ":param" but does not have a default initialisation
501       expression or block becomes a required argument to the constructor.
502       Attempting to invoke the constructor without a named argument for this
503       will throw an exception. In order to make a parameter optional, make
504       sure to give it a default expression - even if that expression is
505       "undef":
506
507          has $x :param;          # this is required
508          has $z :param = undef;  # this is optional
509
510       Any slot that has a ":param" and an initialisation block will only run
511       the code in the block if required by the constructor. If a named
512       parameter is passed to the constructor for this slot, then its code
513       block will not be executed.
514
515       Values for slots are assigned by the constructor before any "BUILD"
516       blocks are invoked.
517
518   method
519          method NAME {
520             ...
521          }
522
523          method NAME (SIGNATURE) {
524             ...
525          }
526
527          method NAME :ATTRS... {
528             ...
529          }
530
531       Declares a new named method. This behaves similarly to the "sub"
532       keyword, except that within the body of the method all of the member
533       fields ("slots") are also accessible. In addition, the method body will
534       have a lexical called $self which contains the invocant object
535       directly; it will already have been shifted from the @_ array.
536
537       The "signatures" feature is automatically enabled for method
538       declarations. In this case the signature does not have to account for
539       the invocant instance; that is handled directly.
540
541          method m ($one, $two) {
542             say "$self invokes method on one=$one two=$two";
543          }
544
545          ...
546          $obj->m(1, 2);
547
548       A list of attributes may be supplied as for "sub". The most useful of
549       these is ":lvalue", allowing easy creation of read-write accessors for
550       slots (but see also the ":reader", ":writer" and ":mutator" slot
551       attributes).
552
553          class Counter {
554             has $count;
555
556             method count :lvalue { $count }
557          }
558
559          my $c = Counter->new;
560          $c->count++;
561
562       Every method automatically gets the ":method" attribute applied, which
563       suppresses warnings about ambiguous calls resolved to core functions if
564       the name of a method matches a core function.
565
566       The following additional attributes are recognised by "Object::Pad"
567       directly:
568
569       :override
570
571       Since version 0.29.
572
573       Marks that this method expects to override another of the same name
574       from a superclass. It is an error at compiletime if the superclass does
575       not provide such a method.
576
577   BUILD
578          BUILD {
579             ...
580          }
581
582          BUILD (SIGNATURE) {
583             ...
584          }
585
586       Since version 0.27.
587
588       Declares the builder block for this component class. A builder block
589       may use subroutine signature syntax, as for methods, to assist in
590       unpacking its arguments. A build block is not a subroutine and thus is
591       not permitted to use subroutine attributes (for example ":lvalue").
592
593       Note that a "BUILD" block is a named phaser block and not a method.
594       Attempts to create a method named "BUILD" (i.e. with syntax "method
595       BUILD {...}") will fail with a compiletime error, to avoid this
596       confusion.
597
598   ADJUST
599          ADJUST {
600             ...
601          }
602
603       Since version 0.43.
604
605       Declares an adjust block for this component class. This block of code
606       runs within the constructor, after any "BUILD" blocks and automatic
607       slot value assignment. It can make any final adjustments to the
608       instance (such as initialising slots from calculated values). No
609       additional parameters are passed.
610
611       An adjust block is not a subroutine and thus is not permitted to use
612       subroutine attributes. Note that an "ADJUST" block is a named phaser
613       block and not a method; it does not use the "sub" or "method" keyword.
614
615   ADJUSTPARAMS
616          ADJUSTPARAMS ( $params ) {    # on perl 5.26 onwards
617             ...
618          }
619
620          ADJUSTPARAMS {
621             my $params = shift;
622             ...
623          }
624
625       Since version 0.51.
626
627       Declares an adjust block for this component class that receives the
628       parameters hash reference. This block of code runs within the
629       constructor at the same time as "ADJUST" blocks, but receives in
630       addition a reference to the hash containing the current constructor
631       parameters. This hash will not contain any constructor parameters
632       already consumed by ":param" declarations on any slots, but only the
633       leftovers once those are processed.
634
635       The code in the block should "delete" from this hash any parameters it
636       wishes to consume. Once all the "ADJUSTPARAMS" blocks have run, any
637       remaining keys in the hash will be considered errors, subject to the
638       ":strict(params)" check.
639
640   requires
641          requires NAME;
642
643       Declares that this role requires a method of the given name from any
644       class that implements it. It is an error at compiletime if the
645       implementing class does not provide such a method.
646

CREPT FEATURES

648       While not strictly part of being an object system, this module has
649       nevertheless gained a number of behaviours by feature creep, as they
650       have been found useful.
651
652   Implied Pragmata
653       In order to encourage users to write clean, modern code, the body of
654       the "class" block acts as if the following pragmata are in effect:
655
656          use strict;
657          use warnings;
658          no indirect ':fatal';  # or  no feature 'indirect' on perl 5.32 onwards
659          use feature 'signatures';
660
661       This list may be extended in subsequent versions to add further
662       restrictions and should not be considered exhaustive.
663
664       Further additions will only be ones that remove "discouraged" or
665       deprecated language features with the overall goal of enforcing a more
666       clean modern style within the body. As long as you write code that is
667       in a clean, modern style (and I fully accept that this wording is vague
668       and subjective) you should not find any new restrictions to be majorly
669       problematic. Either the code will continue to run unaffected, or you
670       may have to make some small alterations to bring it into a conforming
671       style.
672
673   Yield True
674       A "class" statement or block will yield a true boolean value. This
675       means that it can be used directly inside a .pm file, avoiding the need
676       to explicitly yield a true value from the end of it.
677

SUBCLASSING CLASSIC PERL CLASSES

679       There are a number of details specific to the case of deriving an
680       "Object::Pad" class from an existing classic Perl class that is not
681       implemented using "Object::Pad".
682
683   Storage of Instance Data
684       Instances will pick either the ":repr(HASH)" or ":repr(magic)" storage
685       type.
686
687   Object State During Methods Invoked By Superclass Constructor
688       It is common in classic Perl OO style to invoke methods on $self during
689       the constructor. This is supported here since "Object::Pad" version
690       0.19.  Note however that any methods invoked by the superclass
691       constructor may not see the object in a fully consistent state. (This
692       fact is not specific to using "Object::Pad" and would happen in classic
693       Perl OO as well). The slot initialisers will have been invoked but the
694       "BUILD" blocks will not.
695
696       For example; in the following
697
698          package ClassicPerlBaseClass {
699             sub new {
700                my $self = bless {}, shift;
701                say "Value seen by superconstructor is ", $self->get_value;
702                return $self;
703             }
704             sub get_value { return "A" }
705          }
706
707          class DerivedClass isa ClassicPerlBaseClass {
708             has $_value = "B";
709             BUILD {
710                $_value = "C";
711             }
712             method get_value { return $_value }
713          }
714
715          my $obj = DerivedClass->new;
716          say "Value seen by user is ", $obj->get_value;
717
718       Until the "ClassicPerlBaseClass::new" superconstructor has returned the
719       "BUILD" block will not have been invoked. The $_value slot will still
720       exist, but its value will be "B" during the superconstructor. After the
721       superconstructor, the "BUILD" blocks are invoked before the completed
722       object is returned to the user. The result will therefore be:
723
724          Value seen by superconstructor is B
725          Value seen by user is C
726

STYLE SUGGESTIONS

728       While in no way required, the following suggestions of code style
729       should be noted in order to establish a set of best practices, and
730       encourage consistency of code which uses this module.
731
732   $VERSION declaration
733       While it would be nice for CPAN and other toolchain modules to parse
734       the embedded version declarations in "class" statements, the current
735       state at time of writing (June 2020) is that none of them actually do.
736       As such, it will still be necessary to make a once-per-file $VERSION
737       declaration in syntax those modules can parse.
738
739       Further note that these modules will also not parse the "class"
740       declaration, so you will have to duplicate this with a "package"
741       declaration as well as a "class" keyword. This does involve repeating
742       the package name, so is slightly undesirable.
743
744       It is hoped that eventually upstream toolchain modules will be adapted
745       to accept the "class" syntax as being sufficient to declare a package
746       and set its version.
747
748       See also
749
750       • <https://github.com/Perl-Toolchain-Gang/Module-Metadata/issues/33>
751
752   File Layout
753       Begin the file with a "use Object::Pad" line; ideally including a
754       minimum-required version. This should be followed by the toplevel
755       "package" and "class" declarations for the file. As it is at toplevel
756       there is no need to use the block notation; it can be a unit class.
757
758       There is no need to "use strict" or apply other usual pragmata; these
759       will be implied by the "class" keyword.
760
761          use Object::Pad 0.16;
762
763          package My::Classname 1.23;
764          class My::Classname;
765
766          # other use statements
767
768          # has, methods, etc.. can go here
769
770   Slot Names
771       Slot names should follow similar rules to regular lexical variables in
772       code - lowercase, name components separated by underscores. For tiny
773       examples such as "dumb record" structures this may be sufficient.
774
775          class Tag {
776             has $name  :mutator;
777             has $value :mutator;
778          }
779
780       In larger examples with lots of non-trivial method bodies, it can get
781       confusing to remember where the slot variables come from (because we no
782       longer have the "$self->{ ... }" visual clue). In these cases it is
783       suggested to prefix the slot names with a leading underscore, to make
784       them more visually distinct.
785
786          class Spudger {
787             has $_grapefruit;
788
789             ...
790
791             method mangle {
792                $_grapefruit->peel; # The leading underscore reminds us this is a slot
793             }
794          }
795

WITH OTHER MODULES

797   Syntax::Keyword::Dynamically
798       A cross-module integration test asserts that "dynamically" works
799       correctly on object instance slots:
800
801          use Object::Pad;
802          use Syntax::Keyword::Dynamically;
803
804          class Container {
805             has $value = 1;
806
807             method example {
808                dynamically $value = 2;
809                ,..
810                # value is restored to 1 on return from this method
811             }
812          }
813
814   Future::AsyncAwait
815       As of Future::AsyncAwait version 0.38 and Object::Pad version 0.15,
816       both modules now use XS::Parse::Sublike to parse blocks of code.
817       Because of this the two modules can operate together and allow class
818       methods to be written as async subs which await expressions:
819
820          use Future::AsyncAwait;
821          use Object::Pad;
822
823          class Example
824          {
825             async method perform ($block)
826             {
827                say "$self is performing code";
828                await $block->();
829                say "code finished";
830             }
831          }
832
833       These three modules combine; there is additionally a cross-module test
834       to ensure that object instance slots can be "dynamically" set during a
835       suspended "async method".
836

DESIGN TODOs

838       The following points are details about the design of pad slot-based
839       object systems in general:
840
841       •   Is multiple inheritence actually required, if role composition is
842           implemented including giving roles the ability to use private
843           slots?
844
845       •   Consider the visibility of superclass slots to subclasses. Do
846           subclasses even need to be able to see their superclass's slots, or
847           are accessor methods always appropriate?
848
849           Concrete example: The "$self->{split_at}" access that
850           Tickit::Widget::HSplit makes of its parent class
851           Tickit::Widget::LinearSplit.
852

IMPLEMENTATION TODOs

854       These points are more about this particular module's implementation:
855
856       •   Consider multiple inheritence of subclassing, if that is still
857           considered useful after adding roles.
858
859       •   Work out why "no indirect" doesn't appear to work properly before
860           perl 5.20.
861
862       •   Work out why we don't get a "Subroutine new redefined at ..."
863           warning if we
864
865             sub new { ... }
866
867       •   The "local" modifier does not work on slot variables, because they
868           appear to be regular lexicals to the parser at that point. A
869           workaround is to use Syntax::Keyword::Dynamically instead:
870
871              use Syntax::Keyword::Dynamically;
872
873              has $loglevel;
874
875              method quietly {
876                 dynamically $loglevel = LOG_ERROR;
877                 ...
878              }
879

FEEDBACK

881       The following resources are useful forms of providing feedback,
882       especially in the form of reports of what you find good or bad about
883       the module, requests for new features, questions on best practice,
884       etc...
885
886       •   The RT queue at
887           <https://rt.cpan.org/Dist/Display.html?Name=Object-Pad>.
888
889       •   The "#cor" IRC channel on "irc.perl.org".
890

SPONSORS

892       With thanks to the following sponsors, who have helped me be able to
893       spend time working on this module and other perl features.
894
895       •   Oetiker+Partner AG <https://www.oetiker.ch/en/>
896
897       •   Deriv <http://deriv.com>
898
899       •   Perl-Verein Schweiz <https://www.perl-workshop.ch/>
900
901       Additional details may be found at
902       <https://github.com/Ovid/Cor/wiki/Sponsors>.
903

AUTHOR

905       Paul Evans <leonerd@leonerd.org.uk>
906
907
908
909perl v5.34.0                      2021-10-08                    Object::Pad(3)
Impressum