| 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, 2011-2023 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | package Sentinel 0.07; | ||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use v5.14; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 8 | our @EXPORT = qw( sentinel ); | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 8 | require XSLoader; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 8 | XSLoader::load( __PACKAGE__, our $VERSION ); | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | 8 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # pureperl fallback | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 8 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | *sentinel = \&Sentinel::PP::sentinel; | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | C<Sentinel> - create lightweight SCALARs with get/set callbacks | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | package Some::Class; | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | use Sentinel; | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | sub foo :lvalue | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | sentinel get => sub { return $self->get_foo }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | set => sub { $self->set_foo( $_[0] ) }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | sub bar :lvalue | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | sentinel value => $self->get_bar, | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | set => sub { $self->set_bar( $_[0] ) }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | sub splot :lvalue | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | sentinel obj => shift, get => \&get_splot, set => \&set_splot; | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | sub wibble :lvalue | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | sentinel obj => shift, get => "get_wibble", set => "set_wibble"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | This module provides a single lvalue function, C<sentinel>, which yields a | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | scalar that invoke callbacks to get or set its value. Primarily this is useful | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | to create lvalue object accessors or other functions, to invoke actual code | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | when a new value is set, rather than simply updating a scalar variable. | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | =head2 sentinel | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | $scalar = sentinel %args | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | Returns (as an lvalue) a scalar with magic attached to it. This magic is used | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | to get the value of the scalar, or to inform of a new value being set, by | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | invoking callback functions supplied to the sentinel. Takes the following | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | named arguments: | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | =over 8 | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | =item get => CODE | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | A C<CODE> reference or C<obj> method name to invoke when the value of the | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | scalar is read, to obtain its value. The value returned from this code will | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | appear as the value of the scalar. | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | =item set => CODE | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | A C<CODE> reference or C<obj> method name to invoke when a new value for the | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | scalar is written. The code will be passed the new value as its only argument. | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | =item value => SCALAR | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | If no C<get> callback is provided, this value is given as the initial value of | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | the scalar. If the scalar manages to survive longer than a single assignment, | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | its value on read will retain the last value set to it. | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | =item obj => SCALAR | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | Optional value to pass as the first argument into the C<get> and C<set> | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | callbacks. If this value is provided, then the C<get> and C<set> callbacks may | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | be given as direct sub references to object methods, or simply method names, | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | rather than closures that capture the referent object. This avoids the runtime | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | overhead of creating lots of small one-use closures around the object. | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | =head1 MUTATION ACCESSORS | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | A useful behaviour of this module is generation of mutation accessor methods | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | that automatically wrap C<get_>/C<set_> accessor/mutator pairs: | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | foreach (qw( name address age height )) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | my $name = $_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | *$name = sub :lvalue { | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | sentinel obj => shift, get => "get_$name", set => "set_$name"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | This is especially useful for methods whose values are simple strings or | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | numbers, because they allow Perl's rich set of mutation operators to be | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | applied to the object's values. | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | $obj->name =~ s/-/_/g; | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | substr( $obj->address, 100 ) = ""; | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | $obj->age++; | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | $obj->height /= 100; | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | =head1 XS vs PUREPERL | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | If an XS compiler is available at build time, this module is implemented using | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | XS. If not, it falls back on an implementation using a C<tie>d scalar. A | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | pureperl installation can also be requested at build time by passing the | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | C<--pp> argument to F<Build.PL>: | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | $ perl Build.PL --pp | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | $ ./Build | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | With thanks to C<leont>, C<Zefram>, and others from C<irc.perl.org/#p5p> for | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | assisting with trickier bits of XS logic. Thanks to C<mst> for suggesting a | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | pureperl implementation for XS-challenged systems. | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||
| 154 | package # Hide from CPAN | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | Sentinel::PP; | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | sub sentinel :lvalue | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sentinel: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | tie my $scalar, "Sentinel::PP", $args{value}, $args{get}, $args{set}, $args{obj}; | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | $scalar; | |||||||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 8 | ▶ | use constant { VALUE => 0, GET => 1, SET => 2, OBJ => 3 }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | sub TIESCALAR | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
TIESCALAR: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | bless [ @_ ], $class; | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | sub FETCH | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FETCH: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | my $get = $self->[GET]; | |||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | my $obj = $self->[OBJ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | if( defined $get and !ref $get and defined $obj ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/6
defined $get and not ref $get and defined $obj
defined $get and not ref $get
MC/DC0%0/3
defined $get and not ref $get and defined $obj
defined $get
not ref $get
defined $obj
Truth table0%0/4
A and not(B) and C
A= defined $getB= not ref $getC= defined $obj
| |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | # Method | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | return $obj->$get; | |||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 180 | elsif( defined $get ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | return $get->( defined $obj ? ( $obj ) : () ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | return $self->[VALUE]; | |||||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | sub STORE | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
STORE: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | my ( $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | my $set = $self->[SET]; | |||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | my $obj = $self->[OBJ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | ▶ | if( defined $set and !ref $set and defined $obj ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/6
defined $set and not ref $set and defined $obj
defined $set and not ref $set
MC/DC0%0/3
defined $set and not ref $set and defined $obj
defined $set
not ref $set
defined $obj
Truth table0%0/4
A and not(B) and C
A= defined $setB= not ref $setC= defined $obj
| |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # Method | ||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | $obj->$set( $value ); | |||||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | elsif( defined $set ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | $set->( defined $obj ? ( $obj ) : (), $value ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | $self->[VALUE] = $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 8 | 0x55AA; | |||||||||||||||||||||||||||||||||||||||||||||