| 1 | package Object::Extend; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 4 | ▶ | use 5.008; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 4 | ▶ | use base qw(Exporter); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 4 | ▶ | use constant { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 4 | SINGLETON => sprintf('%s::_Singleton', __PACKAGE__ ), | |||||||||||||
| 10 | METHOD_NAME => qr{^[a-zA-Z_]\w*$}, | ||||||||||||||
| 11 | 4 | }; | |||||||||||||
| 12 | |||||||||||||||
| 13 | 4 | ▶ | use B qw(perlstring); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 4 | ▶ | use Carp qw(confess); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 4 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 4 | ▶ | use Storable qw(freeze); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 4 | our @EXPORT_OK = qw(extend with SINGLETON); | |||||||||||||
| 19 | 4 | our $VERSION = '0.4.0'; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 4 | my $ID = 0; | |||||||||||||
| 22 | 4 | my %CACHE; | |||||||||||||
| 23 | |||||||||||||||
| 24 | # find/create a unique class name for the supplied object's class/methods combination. | ||||||||||||||
| 25 | # | ||||||||||||||
| 26 | # Eigenclasses are immutable i.e. once an eigenclass has been created, | ||||||||||||||
| 27 | # its @ISA and installed methods never change. This means we can reuse/recycle | ||||||||||||||
| 28 | # an eigenclass if we're passed the same superclass/methods combo. | ||||||||||||||
| 29 | # | ||||||||||||||
| 30 | # Note: we need to identify the subs in the method hash by value (deparse) | ||||||||||||||
| 31 | # rather than by reference (refaddr), since ref addresses can be recycled | ||||||||||||||
| 32 | # (and frequently are for anonymous subs). | ||||||||||||||
| 33 | # | ||||||||||||||
| 34 | # Note: the SINGLETON class added to the eigenclass's @ISA doesn't | ||||||||||||||
| 35 | # implement any methods: we just use it as metadata to indicate that | ||||||||||||||
| 36 | # the object has been extended. | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 36 | ▶ | sub _eigenclass($$) { | ||||||||||||
|
Subroutine
_eigenclass: called CC=4 SCAR=13.9
| |||||||||||||||
| 39 | 36 | my ($class, $methods) = @_; | |||||||||||||
| 40 | |||||||||||||||
| 41 | 36 | my $key = do { | |||||||||||||
| 42 | 4 | ▶ | no warnings qw(once); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 43 | |||||||||||||||
| 44 | 36 | local $Storable::Deparse = 1; | |||||||||||||
| 45 | # XXX squashed bugs 1) sort hash keys | ||||||||||||||
| 46 | # 2) freeze the $hashref, not the %$hash! | ||||||||||||||
| 47 | 36 | local $Storable::canonical = 1; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 36 | freeze [ $class, $methods ]; | |||||||||||||
| 50 | }; | ||||||||||||||
| 51 | |||||||||||||||
| 52 | 36 | my $eigenclass = $CACHE{$key}; | |||||||||||||
| 53 | |||||||||||||||
| 54 | 36 | ▶ | unless ($eigenclass) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 17 | $eigenclass = sprintf '%s::_%x', SINGLETON, ++$ID; | |||||||||||||
| 56 | 17 | $CACHE{$key} = $eigenclass; | |||||||||||||
| 57 | |||||||||||||||
| 58 | 17 | ▶ | if ($class->isa(SINGLETON)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 4 | _set_isa($eigenclass, [ $class ]); | |||||||||||||
| 60 | } else { | ||||||||||||||
| 61 | 13 | _set_isa($eigenclass, [ $class, SINGLETON ]); | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 17 | ▶ | while (my ($name, $sub) = each(%$methods)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | 26 | _install_sub("$eigenclass\::$name", $sub); | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 36 | return $eigenclass; | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | # install the supplied sub in the supplied class. | ||||||||||||||
| 73 | # "extend" is a pretty clear statement of intent, so | ||||||||||||||
| 74 | # we don't issue a warning if the sub already exists | ||||||||||||||
| 75 | # | ||||||||||||||
| 76 | # XXX if we used Exporter::Tiny, we could | ||||||||||||||
| 77 | # allow the redefine warning to be enabled e.g.: | ||||||||||||||
| 78 | # | ||||||||||||||
| 79 | # use Object::Extend extend => { warn_on_redefine => 1 }; | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 26 | ▶ | sub _install_sub($$) { | ||||||||||||
|
Subroutine
_install_sub: called CC=1 SCAR=0.0
| |||||||||||||||
| 82 | 26 | my ($class, $sub) = @_; | |||||||||||||
| 83 | 4 | ▶ | no warnings 'redefine'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 84 | 4 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 85 | 26 | *$class = $sub; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | # set a class's @ISA array | ||||||||||||||
| 89 | 17 | ▶ | sub _set_isa($$) { | ||||||||||||
|
Subroutine
_set_isa: called CC=1 SCAR=0.0
| |||||||||||||||
| 90 | 17 | my ($class, $isa) = @_; | |||||||||||||
| 91 | 4 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 92 | 17 | *{"$class\::ISA"} = $isa; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | # return true if $ref ISA $class - works with non-references, | ||||||||||||||
| 96 | # unblessed references and objects | ||||||||||||||
| 97 | 90 | ▶ | sub _isa($$) { | ||||||||||||
|
Subroutine
_isa: called CC=2 SCAR=6.9
| |||||||||||||||
| 98 | 90 | my ($ref, $class) = @_; | |||||||||||||
| 99 | 90 | ▶ | return blessed($ref) ? $ref->isa($class) : ref($ref) eq $class; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | } | ||||||||||||||
| 101 | |||||||||||||||
| 102 | # confess with a message whose string parameters are quoted | ||||||||||||||
| 103 | 0 | ▶ | sub _error($;@) { | ||||||||||||
|
Subroutine
_error: not called CC=2 SCAR=17.9
| |||||||||||||||
| 104 | 0 | my $template = shift; | |||||||||||||
| 105 | 0 | ▶ | my @args = map { defined($_) ? perlstring($_) : 'undef' } @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | 0 | confess sprintf($template, @args); | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | |||||||||||||||
| 109 | # sanity check the arguments to extend | ||||||||||||||
| 110 | 36 | ▶ | sub _validate(@) { | ||||||||||||
|
Subroutine
_validate: called CC=9 SCAR=25.1
| |||||||||||||||
| 111 | 36 | my $object = shift; | |||||||||||||
| 112 | 36 | my $class = blessed($object); | |||||||||||||
| 113 | |||||||||||||||
| 114 | 36 | ▶ | unless ($class) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 115 | 0 | _error( | |||||||||||||
| 116 | "invalid 'object' parameter: expected blessed reference, got: %s", | ||||||||||||||
| 117 | ref($object) | ||||||||||||||
| 118 | ); | ||||||||||||||
| 119 | } | ||||||||||||||
| 120 | |||||||||||||||
| 121 | 36 | my $methods; | |||||||||||||
| 122 | |||||||||||||||
| 123 | 36 | ▶ | if (@_ == 1) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 124 | 28 | $methods = shift; | |||||||||||||
| 125 | } elsif (@_ % 2 == 0) { | ||||||||||||||
| 126 | 8 | $methods = { @_ }; | |||||||||||||
| 127 | } | ||||||||||||||
| 128 | |||||||||||||||
| 129 | 36 | ▶ | unless (_isa($methods, 'HASH')) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | 0 | _error( | |||||||||||||
| 131 | "invalid 'methods' parameter: expected a hashref, got: %s", | ||||||||||||||
| 132 | ref($methods) | ||||||||||||||
| 133 | ); | ||||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 36 | for my $name (keys %$methods) { | |||||||||||||
| 137 | 53 | ▶ | if (!defined($name)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 138 | 0 | _error 'invalid method name (undef)'; | |||||||||||||
| 139 | } elsif ($name !~ METHOD_NAME) { | ||||||||||||||
| 140 | 0 | _error( | |||||||||||||
| 141 | 'invalid method name (%s): name must match %s', | ||||||||||||||
| 142 | $name, | ||||||||||||||
| 143 | METHOD_NAME | ||||||||||||||
| 144 | ); | ||||||||||||||
| 145 | } else { | ||||||||||||||
| 146 | 53 | my $method = $methods->{$name}; | |||||||||||||
| 147 | |||||||||||||||
| 148 | 53 | ▶ | unless (_isa($method, 'CODE')) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 149 | 0 | _error( | |||||||||||||
| 150 | 'invalid method value for %s: expected a coderef, got: %s', | ||||||||||||||
| 151 | $name, | ||||||||||||||
| 152 | ref($method), | ||||||||||||||
| 153 | ); | ||||||||||||||
| 154 | } | ||||||||||||||
| 155 | } | ||||||||||||||
| 156 | } | ||||||||||||||
| 157 | |||||||||||||||
| 158 | 36 | return ($object, $class, $methods); | |||||||||||||
| 159 | } | ||||||||||||||
| 160 | |||||||||||||||
| 161 | # dummy sub to optionally make the syntax | ||||||||||||||
| 162 | # a bit more DSL-ish: extend $object => with ... | ||||||||||||||
| 163 | 1 | ▶ | sub with($) { | ||||||||||||
|
Subroutine
with: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||
| 164 | 1 | my $methods = shift; | |||||||||||||
| 165 | |||||||||||||||
| 166 | 1 | ▶ | unless (_isa($methods, 'HASH')) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 167 | 0 | _error( | |||||||||||||
| 168 | "invalid 'methods' parameter: expected a hashref, got: %s", | ||||||||||||||
| 169 | ref($methods) | ||||||||||||||
| 170 | ); | ||||||||||||||
| 171 | } | ||||||||||||||
| 172 | |||||||||||||||
| 173 | 1 | return $methods; | |||||||||||||
| 174 | } | ||||||||||||||
| 175 | |||||||||||||||
| 176 | # find/create an eigenclass for the object's class/methods and bless the object into it | ||||||||||||||
| 177 | 36 | ▶ | sub extend($;@) { | ||||||||||||
|
Subroutine
extend: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 178 | 36 | my ($object, $class, $methods) = _validate(@_); | |||||||||||||
| 179 | |||||||||||||||
| 180 | 36 | ▶ | if (%$methods) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 181 | 36 | my $eigenclass = _eigenclass($class, $methods); | |||||||||||||
| 182 | 36 | bless $object, $eigenclass; | |||||||||||||
| 183 | } # else return the original object unchanged | ||||||||||||||
| 184 | |||||||||||||||
| 185 | 36 | return $object; | |||||||||||||
| 186 | } | ||||||||||||||
| 187 | |||||||||||||||
| 188 | 4 | 1; | |||||||||||||
| 189 | |||||||||||||||
| 190 | =head1 NAME | ||||||||||||||
| 191 | |||||||||||||||
| 192 | Object::Extend - add and override per-object methods | ||||||||||||||
| 193 | |||||||||||||||
| 194 | =head1 SYNOPSIS | ||||||||||||||
| 195 | |||||||||||||||
| 196 | use Object::Extend qw(extend); | ||||||||||||||
| 197 | |||||||||||||||
| 198 | my $foo1 = Foo->new; | ||||||||||||||
| 199 | my $foo2 = Foo->new; | ||||||||||||||
| 200 | |||||||||||||||
| 201 | extend $foo1 => { | ||||||||||||||
| 202 | bar => sub { ... }, | ||||||||||||||
| 203 | }; | ||||||||||||||
| 204 | |||||||||||||||
| 205 | $foo1->bar; # OK | ||||||||||||||
| 206 | $foo2->bar; # error | ||||||||||||||
| 207 | |||||||||||||||
| 208 | =head1 DESCRIPTION | ||||||||||||||
| 209 | |||||||||||||||
| 210 | This module allows objects to be extended with per-object methods, similar to the use of | ||||||||||||||
| 211 | L<singleton methods|http://madebydna.com/all/code/2011/06/24/eigenclasses-demystified.html> | ||||||||||||||
| 212 | in Ruby. Object methods are added to an object-specific shim class (known as an C<eigenclass>), | ||||||||||||||
| 213 | which extends the object's original class. The original class is left unchanged. | ||||||||||||||
| 214 | |||||||||||||||
| 215 | =head2 EXPORTS | ||||||||||||||
| 216 | |||||||||||||||
| 217 | =head3 extend | ||||||||||||||
| 218 | |||||||||||||||
| 219 | C<extend> takes an object and a hash or hashref of method names and method values (coderefs) and adds | ||||||||||||||
| 220 | the methods to the object's shim class. The object is then blessed into this class and returned. | ||||||||||||||
| 221 | |||||||||||||||
| 222 | It can be used in standalone statements: | ||||||||||||||
| 223 | |||||||||||||||
| 224 | extend $object, foo => sub { ... }, bar => \&bar; | ||||||||||||||
| 225 | |||||||||||||||
| 226 | Or expressions: | ||||||||||||||
| 227 | |||||||||||||||
| 228 | return extend($object => { bar => sub { ... } })->bar; | ||||||||||||||
| 229 | |||||||||||||||
| 230 | In both cases, C<extend> operates on and returns the supplied object i.e. a new object is never created. | ||||||||||||||
| 231 | If a new object is needed, it can be created manually e.g.: | ||||||||||||||
| 232 | |||||||||||||||
| 233 | my $object2 = Object->new($object1); | ||||||||||||||
| 234 | my $object3 = clone($object1); | ||||||||||||||
| 235 | |||||||||||||||
| 236 | extend($object2, foo => sub { ... })->foo; | ||||||||||||||
| 237 | return extend($object3 => ...); | ||||||||||||||
| 238 | |||||||||||||||
| 239 | Objects can be extended multiple times with new or overridden methods: | ||||||||||||||
| 240 | |||||||||||||||
| 241 | # call the original method | ||||||||||||||
| 242 | my $object = Foo->new; | ||||||||||||||
| 243 | $object->foo; | ||||||||||||||
| 244 | |||||||||||||||
| 245 | # override the original method | ||||||||||||||
| 246 | extend $object, foo => sub { ... }; | ||||||||||||||
| 247 | $object->foo; | ||||||||||||||
| 248 | |||||||||||||||
| 249 | # add a new method | ||||||||||||||
| 250 | extend $object, bar => sub { ... }; | ||||||||||||||
| 251 | $object->bar; | ||||||||||||||
| 252 | |||||||||||||||
| 253 | =head3 with | ||||||||||||||
| 254 | |||||||||||||||
| 255 | This sub can optionally be imported to make the use of C<extend> more descriptive. It takes and | ||||||||||||||
| 256 | returns a hashref of method names/coderefs: | ||||||||||||||
| 257 | |||||||||||||||
| 258 | use Object::Extend qw(extend with); | ||||||||||||||
| 259 | |||||||||||||||
| 260 | extend $object => with { foo => sub { ... } }; | ||||||||||||||
| 261 | |||||||||||||||
| 262 | =head3 SINGLETON | ||||||||||||||
| 263 | |||||||||||||||
| 264 | Every extended object's shim class includes an additional (empty) class in its C<@ISA> which indicates | ||||||||||||||
| 265 | that the object has been extended. The name of this class can be accessed by importing the C<SINGLETON> | ||||||||||||||
| 266 | constant e.g.: | ||||||||||||||
| 267 | |||||||||||||||
| 268 | use Object::Extend qw(SINGLETON); | ||||||||||||||
| 269 | |||||||||||||||
| 270 | if ($object->isa(SINGLETON)) { ... } # object extended with object-specific methods | ||||||||||||||
| 271 | |||||||||||||||
| 272 | =head1 VERSION | ||||||||||||||
| 273 | |||||||||||||||
| 274 | 0.4.0 | ||||||||||||||
| 275 | |||||||||||||||
| 276 | =head1 SEE ALSO | ||||||||||||||
| 277 | |||||||||||||||
| 278 | =over | ||||||||||||||
| 279 | |||||||||||||||
| 280 | =item * L<Class::SingletonMethod|Class::SingletonMethod> | ||||||||||||||
| 281 | |||||||||||||||
| 282 | =item * L<MooseX::SingletonMethod|MooseX::SingletonMethod> | ||||||||||||||
| 283 | |||||||||||||||
| 284 | =item * L<MouseX::SingletonMethod|MouseX::SingletonMethod> | ||||||||||||||
| 285 | |||||||||||||||
| 286 | =item * L<SingletonMethod|https://github.com/tom-lpsd/p5-singleton-method> | ||||||||||||||
| 287 | |||||||||||||||
| 288 | =back | ||||||||||||||
| 289 | |||||||||||||||
| 290 | =head1 AUTHOR | ||||||||||||||
| 291 | |||||||||||||||
| 292 | chocolateboy <chocolate@cpan.org> | ||||||||||||||
| 293 | |||||||||||||||
| 294 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 295 | |||||||||||||||
| 296 | Copyright (C) 2013 by chocolateboy | ||||||||||||||
| 297 | |||||||||||||||
| 298 | This library is free software; you can redistribute it and/or modify | ||||||||||||||
| 299 | it under the same terms as Perl itself, either Perl version 5.14.2 or, | ||||||||||||||
| 300 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||
| 301 | |||||||||||||||
| 302 | =cut | ||||||||||||||