| 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, 2021-2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | package Object::Pad::FieldAttr::Trigger 0.07; | ||
| 7 | |||
| 8 | 2 | ▶ | use v5.14; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 2 | ▶ | use Object::Pad 0.66; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 2 | require XSLoader; | |
| 14 | 2 | XSLoader::load( __PACKAGE__, our $VERSION ); | |
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | C<Object::Pad::FieldAttr::Trigger> - invoke an instance method after a C<:writer> accessor | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | use Object::Pad; | ||
| 23 | use Object::Pad::FieldAttr::Trigger; | ||
| 24 | |||
| 25 | class Label { | ||
| 26 | field $title :param :reader :writer :Trigger(redraw); | ||
| 27 | |||
| 28 | method redraw { | ||
| 29 | ... | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | my $label = Label->new( text => "Something" ); | ||
| 34 | |||
| 35 | $label->set_label( "New text here" ); | ||
| 36 | # $label->redraw is automatically invoked | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | This module provides a third-party field attribute for L<Object::Pad>-based | ||
| 41 | classes, which declares that a named instance method shall be invoked after | ||
| 42 | a generated C<:writer> accessor method is called. | ||
| 43 | |||
| 44 | B<WARNING> The ability for L<Object::Pad> to take third-party field attributes | ||
| 45 | is still new and highly experimental, and subject to much API change in | ||
| 46 | future. As a result, this module should be considered equally experimental. | ||
| 47 | |||
| 48 | =head1 FIELD ATTRIBUTES | ||
| 49 | |||
| 50 | =head2 :Trigger | ||
| 51 | |||
| 52 | field $name :writer :Trigger(NAME) ...; | ||
| 53 | |||
| 54 | Declares that the accessor method generated for the field by the C<:writer> | ||
| 55 | attribute will invoke the method named by the C<:Trigger> attribute, after the | ||
| 56 | new value has been stored into the field itself. This method is invoked with | ||
| 57 | no additional arguments, in void context. | ||
| 58 | |||
| 59 | Note that this only applies to the generated accessor method. It does not | ||
| 60 | apply to direct modifications of the field variable by method code within the | ||
| 61 | class itself. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub import | ||
| 66 | 1 | ▶ | { |
|
Subroutine
import: called CC=1 SCAR=0.0
| |||
| 67 | 1 | $^H{"Object::Pad::FieldAttr::Trigger/Trigger"}++; | |
| 68 | } | ||
| 69 | |||
| 70 | sub unimport | ||
| 71 | 0 | ▶ | { |
|
Subroutine
unimport: not called CC=1 SCAR=6.9
| |||
| 72 | 0 | delete $^H{"Object::Pad::FieldAttr::Trigger/Trigger"}; | |
| 73 | } | ||
| 74 | |||
| 75 | =head1 AUTHOR | ||
| 76 | |||
| 77 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | 2 | 0x55AA; | |