| 1 | package Object::Accessor; | ||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use if $] > 5.017, 'deprecate'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Carp qw[carp croak]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use vars qw[$FATAL $DEBUG $AUTOLOAD $VERSION]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Params::Check qw[allow]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | ### some objects might have overload enabled, we'll need to | ||||||||||||||||||||||||||||
| 10 | ### disable string overloading for callbacks | ||||||||||||||||||||||||||||
| 11 | 7 | require overload; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 7 | $VERSION = '0.48'; | |||||||||||||||||||||||||||
| 14 | 7 | $FATAL = 0; | |||||||||||||||||||||||||||
| 15 | 7 | $DEBUG = 0; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 7 | ▶ | use constant VALUE => 0; # array index in the hash value | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 7 | ▶ | use constant ALLOW => 1; # array index in the hash value | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 7 | ▶ | use constant ALIAS => 2; # array index in the hash value | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 NAME | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | Object::Accessor - interface to create per object accessors | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | ### using the object | ||||||||||||||||||||||||||||
| 28 | $obj = Object::Accessor->new; # create object | ||||||||||||||||||||||||||||
| 29 | $obj = Object::Accessor->new(@list); # create object with accessors | ||||||||||||||||||||||||||||
| 30 | $obj = Object::Accessor->new(\%h); # create object with accessors | ||||||||||||||||||||||||||||
| 31 | # and their allow handlers | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | $bool = $obj->mk_accessors('foo'); # create accessors | ||||||||||||||||||||||||||||
| 34 | $bool = $obj->mk_accessors( # create accessors with input | ||||||||||||||||||||||||||||
| 35 | {foo => ALLOW_HANDLER} ); # validation | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | $bool = $obj->mk_aliases( # create an alias to an existing | ||||||||||||||||||||||||||||
| 38 | alias_name => 'method'); # method name | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | $clone = $obj->mk_clone; # create a clone of original | ||||||||||||||||||||||||||||
| 41 | # object without data | ||||||||||||||||||||||||||||
| 42 | $bool = $obj->mk_flush; # clean out all data | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | @list = $obj->ls_accessors; # retrieves a list of all | ||||||||||||||||||||||||||||
| 45 | # accessors for this object | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | $bar = $obj->foo('bar'); # set 'foo' to 'bar' | ||||||||||||||||||||||||||||
| 48 | $bar = $obj->foo(); # retrieve 'bar' again | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | $sub = $obj->can('foo'); # retrieve coderef for | ||||||||||||||||||||||||||||
| 51 | # 'foo' accessor | ||||||||||||||||||||||||||||
| 52 | $bar = $sub->('bar'); # set 'foo' via coderef | ||||||||||||||||||||||||||||
| 53 | $bar = $sub->(); # retrieve 'bar' by coderef | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | ### using the object as base class | ||||||||||||||||||||||||||||
| 56 | package My::Class; | ||||||||||||||||||||||||||||
| 57 | use base 'Object::Accessor'; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | $obj = My::Class->new; # create base object | ||||||||||||||||||||||||||||
| 60 | $bool = $obj->mk_accessors('foo'); # create accessors, etc... | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | ### make all attempted access to non-existent accessors fatal | ||||||||||||||||||||||||||||
| 63 | ### (defaults to false) | ||||||||||||||||||||||||||||
| 64 | $Object::Accessor::FATAL = 1; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | ### enable debugging | ||||||||||||||||||||||||||||
| 67 | $Object::Accessor::DEBUG = 1; | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | ### advanced usage -- callbacks | ||||||||||||||||||||||||||||
| 70 | { my $obj = Object::Accessor->new('foo'); | ||||||||||||||||||||||||||||
| 71 | $obj->register_callback( sub { ... } ); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | $obj->foo( 1 ); # these calls invoke the callback you registered | ||||||||||||||||||||||||||||
| 74 | $obj->foo() # which allows you to change the get/set | ||||||||||||||||||||||||||||
| 75 | # behaviour and what is returned to the caller. | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | ### advanced usage -- lvalue attributes | ||||||||||||||||||||||||||||
| 79 | { my $obj = Object::Accessor::Lvalue->new('foo'); | ||||||||||||||||||||||||||||
| 80 | print $obj->foo = 1; # will print 1 | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | ### advanced usage -- scoped attribute values | ||||||||||||||||||||||||||||
| 84 | { my $obj = Object::Accessor->new('foo'); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | $obj->foo( 1 ); | ||||||||||||||||||||||||||||
| 87 | print $obj->foo; # will print 1 | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | ### bind the scope of the value of attribute 'foo' | ||||||||||||||||||||||||||||
| 90 | ### to the scope of '$x' -- when $x goes out of | ||||||||||||||||||||||||||||
| 91 | ### scope, 'foo's previous value will be restored | ||||||||||||||||||||||||||||
| 92 | { $obj->foo( 2 => \my $x ); | ||||||||||||||||||||||||||||
| 93 | print $obj->foo, ' ', $x; # will print '2 2' | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | print $obj->foo; # will print 1 | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | C<Object::Accessor> provides an interface to create per object | ||||||||||||||||||||||||||||
| 102 | accessors (as opposed to per C<Class> accessors, as, for example, | ||||||||||||||||||||||||||||
| 103 | C<Class::Accessor> provides). | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | You can choose to either subclass this module, and thus using its | ||||||||||||||||||||||||||||
| 106 | accessors on your own module, or to store an C<Object::Accessor> | ||||||||||||||||||||||||||||
| 107 | object inside your own object, and access the accessors from there. | ||||||||||||||||||||||||||||
| 108 | See the C<SYNOPSIS> for examples. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 METHODS | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 $object = Object::Accessor->new( [ARGS] ); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Creates a new (and empty) C<Object::Accessor> object. This method is | ||||||||||||||||||||||||||||
| 115 | inheritable. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Any arguments given to C<new> are passed straight to C<mk_accessors>. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | If you want to be able to assign to your accessors as if they | ||||||||||||||||||||||||||||
| 120 | were C<lvalue>s, you should create your object in the | ||||||||||||||||||||||||||||
| 121 | C<Object::Accessor::Lvalue> namespace instead. See the section | ||||||||||||||||||||||||||||
| 122 | on C<LVALUE ACCESSORS> below. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 16 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | 16 | my $class = shift; | |||||||||||||||||||||||||||
| 128 | 16 | my $obj = bless {}, $class; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 16 | ▶ | $obj->mk_accessors( @_ ) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 16 | return $obj; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head2 $bool = $object->mk_accessors( @ACCESSORS | \%ACCESSOR_MAP ); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Creates a list of accessors for this object (and C<NOT> for other ones | ||||||||||||||||||||||||||||
| 138 | in the same class!). | ||||||||||||||||||||||||||||
| 139 | Will not clobber existing data, so if an accessor already exists, | ||||||||||||||||||||||||||||
| 140 | requesting to create again is effectively a C<no-op>. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | When providing a C<hashref> as argument, rather than a normal list, | ||||||||||||||||||||||||||||
| 143 | you can specify a list of key/value pairs of accessors and their | ||||||||||||||||||||||||||||
| 144 | respective input validators. The validators can be anything that | ||||||||||||||||||||||||||||
| 145 | C<Params::Check>'s C<allow> function accepts. Please see its manpage | ||||||||||||||||||||||||||||
| 146 | for details. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | For example: | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | $object->mk_accessors( { | ||||||||||||||||||||||||||||
| 151 | foo => qr/^\d+$/, # digits only | ||||||||||||||||||||||||||||
| 152 | bar => [0,1], # booleans | ||||||||||||||||||||||||||||
| 153 | zot => \&my_sub # a custom verification sub | ||||||||||||||||||||||||||||
| 154 | } ); | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | Returns true on success, false on failure. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Accessors that are called on an object, that do not exist return | ||||||||||||||||||||||||||||
| 159 | C<undef> by default, but you can make this a fatal error by setting the | ||||||||||||||||||||||||||||
| 160 | global variable C<$FATAL> to true. See the section on C<GLOBAL | ||||||||||||||||||||||||||||
| 161 | VARIABLES> for details. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Note that you can bind the values of attributes to a scope. This allows | ||||||||||||||||||||||||||||
| 164 | you to C<temporarily> change a value of an attribute, and have it's | ||||||||||||||||||||||||||||
| 165 | original value restored up on the end of it's bound variable's scope; | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | For example, in this snippet of code, the attribute C<foo> will | ||||||||||||||||||||||||||||
| 168 | temporarily be set to C<2>, until the end of the scope of C<$x>, at | ||||||||||||||||||||||||||||
| 169 | which point the original value of C<1> will be restored. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | my $obj = Object::Accessor->new; | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | $obj->mk_accessors('foo'); | ||||||||||||||||||||||||||||
| 174 | $obj->foo( 1 ); | ||||||||||||||||||||||||||||
| 175 | print $obj->foo; # will print 1 | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | ### bind the scope of the value of attribute 'foo' | ||||||||||||||||||||||||||||
| 178 | ### to the scope of '$x' -- when $x goes out of | ||||||||||||||||||||||||||||
| 179 | ### scope, 'foo' previous value will be restored | ||||||||||||||||||||||||||||
| 180 | { $obj->foo( 2 => \my $x ); | ||||||||||||||||||||||||||||
| 181 | print $obj->foo, ' ', $x; # will print '2 2' | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | print $obj->foo; # will print 1 | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Note that all accessors are read/write for everyone. See the C<TODO> | ||||||||||||||||||||||||||||
| 187 | section for details. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 15 | ▶ | sub mk_accessors { | ||||||||||||||||||||||||||
|
Subroutine
mk_accessors: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 192 | 15 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 193 | 15 | my $is_hash = UNIVERSAL::isa( $_[1], 'HASH' ); | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | ### first argument is a hashref, which means key/val pairs | ||||||||||||||||||||||||||||
| 196 | ### as keys + allow handlers | ||||||||||||||||||||||||||||
| 197 | 15 | ▶ | for my $acc ( $is_hash ? keys %{$_[1]} : @_[1..$#_] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | ### already created apparently | ||||||||||||||||||||||||||||
| 200 | 16 | ▶ | if( exists $self->{$acc} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 1 | __PACKAGE__->___debug( "Accessor '$acc' already exists"); | |||||||||||||||||||||||||||
| 202 | 1 | next; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 15 | __PACKAGE__->___debug( "Creating accessor '$acc'"); | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | ### explicitly vivify it, so that exists works in ls_accessors() | ||||||||||||||||||||||||||||
| 208 | 15 | $self->{$acc}->[VALUE] = undef; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | ### set the allow handler only if one was specified | ||||||||||||||||||||||||||||
| 211 | 15 | ▶ | $self->{$acc}->[ALLOW] = $_[1]->{$acc} if $is_hash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 15 | return 1; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head2 @list = $self->ls_accessors; | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | Returns a list of accessors that are supported by the current object. | ||||||||||||||||||||||||||||
| 220 | The corresponding coderefs can be retrieved by passing this list one | ||||||||||||||||||||||||||||
| 221 | by one to the C<can> method. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =cut | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 17 | ▶ | sub ls_accessors { | ||||||||||||||||||||||||||
|
Subroutine
ls_accessors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 226 | ### metainformation is stored in the stringified | ||||||||||||||||||||||||||||
| 227 | ### key of the object, so skip that when listing accessors | ||||||||||||||||||||||||||||
| 228 | 17 | return sort grep { $_ ne "$_[0]" } keys %{$_[0]}; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =head2 $ref = $self->ls_allow(KEY) | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | Returns the allow handler for the given key, which can be used with | ||||||||||||||||||||||||||||
| 234 | C<Params::Check>'s C<allow()> handler. If there was no allow handler | ||||||||||||||||||||||||||||
| 235 | specified, an allow handler that always returns true will be returned. | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =cut | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 3 | ▶ | sub ls_allow { | ||||||||||||||||||||||||||
|
Subroutine
ls_allow: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 240 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 241 | 3 | ▶ | my $key = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | return exists $self->{$key}->[ALLOW] | ||||||||||||||||||||||||||||
| 243 | ? $self->{$key}->[ALLOW] | ||||||||||||||||||||||||||||
| 244 | 0 | ▶ | : sub { 1 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =head2 $bool = $self->mk_aliases( alias => method, [alias2 => method2, ...] ); | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | Creates an alias for a given method name. For all intents and purposes, | ||||||||||||||||||||||||||||
| 250 | these two accessors are now identical for this object. This is akin to | ||||||||||||||||||||||||||||
| 251 | doing the following on the symbol table level: | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | *alias = *method | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | This allows you to do the following: | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | $self->mk_accessors('foo'); | ||||||||||||||||||||||||||||
| 258 | $self->mk_aliases( bar => 'foo' ); | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | $self->bar( 42 ); | ||||||||||||||||||||||||||||
| 261 | print $self->foo; # will print 42 | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =cut | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 1 | ▶ | sub mk_aliases { | ||||||||||||||||||||||||||
|
Subroutine
mk_aliases: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 266 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 267 | 1 | my %aliases = @_; | |||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 1 | ▶ | while( my($alias, $method) = each %aliases ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | ### already created apparently | ||||||||||||||||||||||||||||
| 272 | 1 | ▶ | if( exists $self->{$alias} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 0 | __PACKAGE__->___debug( "Accessor '$alias' already exists"); | |||||||||||||||||||||||||||
| 274 | 0 | next; | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 1 | $self->___alias( $alias => $method ); | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 1 | return 1; | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | =head2 $clone = $self->mk_clone; | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | Makes a clone of the current object, which will have the exact same | ||||||||||||||||||||||||||||
| 286 | accessors as the current object, but without the data stored in them. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =cut | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | ### XXX this creates an object WITH allow handlers at all times. | ||||||||||||||||||||||||||||
| 291 | ### even if the original didn't | ||||||||||||||||||||||||||||
| 292 | 7 | ▶ | sub mk_clone { | ||||||||||||||||||||||||||
|
Subroutine
mk_clone: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 293 | 7 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 294 | 7 | my $class = ref $self; | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 7 | my $clone = $class->new; | |||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | ### split out accessors with and without allow handlers, so we | ||||||||||||||||||||||||||||
| 299 | ### don't install dummy allow handlers (which makes O::A::lvalue | ||||||||||||||||||||||||||||
| 300 | ### warn for example) | ||||||||||||||||||||||||||||
| 301 | 7 | my %hash; my @list; | |||||||||||||||||||||||||||
| 302 | 7 | for my $acc ( $self->ls_accessors ) { | |||||||||||||||||||||||||||
| 303 | 7 | my $allow = $self->{$acc}->[ALLOW]; | |||||||||||||||||||||||||||
| 304 | 7 | ▶ | $allow ? $hash{$acc} = $allow : push @list, $acc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | ### is this an alias? | ||||||||||||||||||||||||||||
| 307 | 7 | ▶ | if( my $org = $self->{ $acc }->[ ALIAS ] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 1 | $clone->___alias( $acc => $org ); | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | ### copy the accessors from $self to $clone | ||||||||||||||||||||||||||||
| 313 | 7 | ▶ | $clone->mk_accessors( \%hash ) if %hash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 314 | 7 | ▶ | $clone->mk_accessors( @list ) if @list; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | ### copy callbacks | ||||||||||||||||||||||||||||
| 317 | #$clone->{"$clone"} = $self->{"$self"} if $self->{"$self"}; | ||||||||||||||||||||||||||||
| 318 | 7 | $clone->___callback( $self->___callback ); | |||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 7 | return $clone; | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =head2 $bool = $self->mk_flush; | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | Flushes all the data from the current object; all accessors will be | ||||||||||||||||||||||||||||
| 326 | set back to their default state of C<undef>. | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | Returns true on success and false on failure. | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | =cut | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 3 | ▶ | sub mk_flush { | ||||||||||||||||||||||||||
|
Subroutine
mk_flush: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 333 | 3 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | # set each accessor's data to undef | ||||||||||||||||||||||||||||
| 336 | 3 | $self->{$_}->[VALUE] = undef for $self->ls_accessors; | |||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 3 | return 1; | |||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =head2 $bool = $self->mk_verify; | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | Checks if all values in the current object are in accordance with their | ||||||||||||||||||||||||||||
| 344 | own allow handler. Specifically useful to check if an empty initialised | ||||||||||||||||||||||||||||
| 345 | object has been filled with values satisfying their own allow criteria. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =cut | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 2 | ▶ | sub mk_verify { | ||||||||||||||||||||||||||
|
Subroutine
mk_verify: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 350 | 2 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 2 | my $fail; | |||||||||||||||||||||||||||
| 353 | 2 | for my $name ( $self->ls_accessors ) { | |||||||||||||||||||||||||||
| 354 | 2 | ▶ | unless( allow( $self->$name, $self->ls_allow( $name ) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | 1 | ▶ | my $val = defined $self->$name ? $self->$name : '<undef>'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 1 | __PACKAGE__->___error("'$name' ($val) is invalid"); | |||||||||||||||||||||||||||
| 358 | 1 | $fail++; | |||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 2 | ▶ | return if $fail; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 363 | 1 | return 1; | |||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | =head2 $bool = $self->register_callback( sub { ... } ); | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | This method allows you to register a callback, that is invoked | ||||||||||||||||||||||||||||
| 369 | every time an accessor is called. This allows you to munge input | ||||||||||||||||||||||||||||
| 370 | data, access external data stores, etc. | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | You are free to return whatever you wish. On a C<set> call, the | ||||||||||||||||||||||||||||
| 373 | data is even stored in the object. | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | Below is an example of the use of a callback. | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | $object->some_method( "some_value" ); | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | my $callback = sub { | ||||||||||||||||||||||||||||
| 380 | my $self = shift; # the object | ||||||||||||||||||||||||||||
| 381 | my $meth = shift; # "some_method" | ||||||||||||||||||||||||||||
| 382 | my $val = shift; # ["some_value"] | ||||||||||||||||||||||||||||
| 383 | # could be undef -- check 'exists'; | ||||||||||||||||||||||||||||
| 384 | # if scalar @$val is empty, it was a 'get' | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | # your code here | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | return $new_val; # the value you want to be set/returned | ||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | To access the values stored in the object, circumventing the | ||||||||||||||||||||||||||||
| 392 | callback structure, you should use the C<___get> and C<___set> methods | ||||||||||||||||||||||||||||
| 393 | documented further down. | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =cut | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 1 | ▶ | sub register_callback { | ||||||||||||||||||||||||||
|
Subroutine
register_callback: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 398 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 399 | 1 | ▶ | my $sub = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | ### use the memory address as key, it's not used EVER as an | ||||||||||||||||||||||||||||
| 402 | ### accessor --kane | ||||||||||||||||||||||||||||
| 403 | 1 | $self->___callback( $sub ); | |||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 1 | return 1; | |||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | =head2 $bool = $self->can( METHOD_NAME ) | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | This method overrides C<UNIVERAL::can> in order to provide coderefs to | ||||||||||||||||||||||||||||
| 412 | accessors which are loaded on demand. It will behave just like | ||||||||||||||||||||||||||||
| 413 | C<UNIVERSAL::can> where it can -- returning a class method if it exists, | ||||||||||||||||||||||||||||
| 414 | or a closure pointing to a valid accessor of this particular object. | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | You can use it as follows: | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | $sub = $object->can('some_accessor'); # retrieve the coderef | ||||||||||||||||||||||||||||
| 419 | $sub->('foo'); # 'some_accessor' now set | ||||||||||||||||||||||||||||
| 420 | # to 'foo' for $object | ||||||||||||||||||||||||||||
| 421 | $foo = $sub->(); # retrieve the contents | ||||||||||||||||||||||||||||
| 422 | # of 'some_accessor' | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | See the C<SYNOPSIS> for more examples. | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | =cut | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | ### custom 'can' as UNIVERSAL::can ignores autoload | ||||||||||||||||||||||||||||
| 429 | 26 | ▶ | sub can { | ||||||||||||||||||||||||||
|
Subroutine
can: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 430 | 26 | my($self, $method) = @_; | |||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | ### it's one of our regular methods | ||||||||||||||||||||||||||||
| 433 | 26 | my $code = $self->UNIVERSAL::can($method); | |||||||||||||||||||||||||||
| 434 | 26 | ▶ | if( $code ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 435 | 1 | ▶ | carp( "Can '$method' -- provided by package" ) if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 436 | 1 | return $code; | |||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | ### it's an accessor we provide; | ||||||||||||||||||||||||||||
| 440 | 25 | ▶ | if( UNIVERSAL::isa( $self, 'HASH' ) and exists $self->{$method} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
UNIVERSAL::isa($self, "HASH") and exists $self->{$method}
MC/DC100%2/2
UNIVERSAL::isa($self, "HASH") and exists $self->{$method}
UNIVERSAL::isa($self, "HASH")
exists $self->{$method}
Truth table100%3/3
A and B
A= UNIVERSAL::isa($self, "HASH")B= exists $self->{$method}
| |||||||||||||||||||||||||||||
| 441 | 9 | ▶ | carp( "Can '$method' -- provided by object" ) if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 442 | 4 | ▶ | return sub { $self->$method(@_); } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 443 | 9 | } | |||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | ### we don't support it | ||||||||||||||||||||||||||||
| 446 | 16 | ▶ | carp( "Cannot '$method'" ) if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 447 | 16 | return; | |||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | ### don't autoload this | ||||||||||||||||||||||||||||
| 451 | 16 | ▶ | sub DESTROY { 1 }; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | ### use autoload so we can have per-object accessors, | ||||||||||||||||||||||||||||
| 454 | ### not per class, as that is incorrect | ||||||||||||||||||||||||||||
| 455 | 48 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 456 | 48 | my $self = shift; | |||||||||||||||||||||||||||
| 457 | 48 | my($method) = ($AUTOLOAD =~ /([^:']+$)/); | |||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | 48 | ▶ | my $val = $self->___autoload( $method, @_ ) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 43 | return $val->[0]; | |||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | 59 | ▶ | sub ___autoload { | ||||||||||||||||||||||||||
|
Subroutine
___autoload: called CC=16 SCAR=27.9
| |||||||||||||||||||||||||||||
| 465 | 59 | my $self = shift; | |||||||||||||||||||||||||||
| 466 | 59 | my $method = shift; | |||||||||||||||||||||||||||
| 467 | 59 | my $assign = scalar @_; # is this an assignment? | |||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | ### a method on our object | ||||||||||||||||||||||||||||
| 470 | 59 | ▶ | if( UNIVERSAL::isa( $self, 'HASH' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 471 | 58 | ▶ | if ( not exists $self->{$method} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 472 | 3 | __PACKAGE__->___error("No such accessor '$method'", 1); | |||||||||||||||||||||||||||
| 473 | 2 | return; | |||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | ### a method on something else, die with a descriptive error; | ||||||||||||||||||||||||||||
| 477 | } else { | ||||||||||||||||||||||||||||
| 478 | 1 | local $FATAL = 1; | |||||||||||||||||||||||||||
| 479 | 1 | __PACKAGE__->___error( | |||||||||||||||||||||||||||
| 480 | "You called '$AUTOLOAD' on '$self' which was interpreted by ". | ||||||||||||||||||||||||||||
| 481 | __PACKAGE__ . " as an object call. Did you mean to include ". | ||||||||||||||||||||||||||||
| 482 | "'$method' from somewhere else?", 1 ); | ||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | ### is this is an alias, redispatch to the original method | ||||||||||||||||||||||||||||
| 486 | 55 | ▶ | if( my $original = $self->{ $method }->[ALIAS] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 487 | 6 | return $self->___autoload( $original, @_ ); | |||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | ### assign? | ||||||||||||||||||||||||||||
| 491 | 49 | ▶ | my $val = $assign ? shift(@_) : $self->___get( $method ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | 49 | ▶ | if( $assign ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | ### any binding? | ||||||||||||||||||||||||||||
| 496 | 15 | ▶ | if( $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 497 | 1 | ▶ | if( ref $_[0] and UNIVERSAL::isa( $_[0], 'SCALAR' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_[0] and UNIVERSAL::isa($_[0], "SCALAR")
MC/DC0%0/2
ref $_[0] and UNIVERSAL::isa($_[0], "SCALAR")
ref $_[0]
UNIVERSAL::isa($_[0], "SCALAR")
Truth table33%1/3
A and B
A= ref $_[0]B= UNIVERSAL::isa($_[0], "SCALAR")
| |||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | ### tie the reference, so we get an object and | ||||||||||||||||||||||||||||
| 500 | ### we can use it's going out of scope to restore | ||||||||||||||||||||||||||||
| 501 | ### the old value | ||||||||||||||||||||||||||||
| 502 | 1 | my $cur = $self->{$method}->[VALUE]; | |||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | 1 | tie ${$_[0]}, __PACKAGE__ . '::TIE', | |||||||||||||||||||||||||||
| 505 | 1 | ▶ | sub { $self->$method( $cur ) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | 1 | ${$_[0]} = $val; | |||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | } else { | ||||||||||||||||||||||||||||
| 510 | 0 | __PACKAGE__->___error( | |||||||||||||||||||||||||||
| 511 | "Can not bind '$method' to anything but a SCALAR", 1 | ||||||||||||||||||||||||||||
| 512 | ); | ||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | ### need to check the value? | ||||||||||||||||||||||||||||
| 517 | 15 | ▶ | if( defined $self->{$method}->[ALLOW] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | ### double assignment due to 'used only once' warnings | ||||||||||||||||||||||||||||
| 520 | 2 | local $Params::Check::VERBOSE = 0; | |||||||||||||||||||||||||||
| 521 | 2 | local $Params::Check::VERBOSE = 0; | |||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | 2 | ▶ | allow( $val, $self->{$method}->[ALLOW] ) or ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 524 | __PACKAGE__->___error( | ||||||||||||||||||||||||||||
| 525 | "'$val' is an invalid value for '$method'", 1), | ||||||||||||||||||||||||||||
| 526 | return | ||||||||||||||||||||||||||||
| 527 | ); | ||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||
| 529 | } | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | ### callbacks? | ||||||||||||||||||||||||||||
| 532 | 48 | ▶ | if( my $sub = $self->___callback ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 533 | 3 | ▶ | $val = eval { $sub->( $self, $method, ($assign ? [$val] : []) ) }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | ### register the error | ||||||||||||||||||||||||||||
| 536 | 3 | ▶ | $self->___error( $@, 1 ), return if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | ### now we can actually assign it | ||||||||||||||||||||||||||||
| 540 | 48 | ▶ | if( $assign ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 541 | 14 | ▶ | $self->___set( $method, $val ) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | 48 | return [$val]; | |||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | =head2 $val = $self->___get( METHOD_NAME ); | ||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | Method to directly access the value of the given accessor in the | ||||||||||||||||||||||||||||
| 550 | object. It circumvents all calls to allow checks, callbacks, etc. | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | Use only if you C<Know What You Are Doing>! General usage for | ||||||||||||||||||||||||||||
| 553 | this functionality would be in your own custom callbacks. | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | =cut | ||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | ### XXX O::A::lvalue is mirroring this behaviour! if this | ||||||||||||||||||||||||||||
| 558 | ### changes, lvalue's autoload must be changed as well | ||||||||||||||||||||||||||||
| 559 | 38 | ▶ | sub ___get { | ||||||||||||||||||||||||||
|
Subroutine
___get: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 560 | 38 | my $self = shift; | |||||||||||||||||||||||||||
| 561 | 38 | ▶ | my $method = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 562 | 38 | return $self->{$method}->[VALUE]; | |||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | =head2 $bool = $self->___set( METHOD_NAME => VALUE ); | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | Method to directly set the value of the given accessor in the | ||||||||||||||||||||||||||||
| 568 | object. It circumvents all calls to allow checks, callbacks, etc. | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | Use only if you C<Know What You Are Doing>! General usage for | ||||||||||||||||||||||||||||
| 571 | this functionality would be in your own custom callbacks. | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | =cut | ||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | 15 | ▶ | sub ___set { | ||||||||||||||||||||||||||
|
Subroutine
___set: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 576 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 577 | 15 | ▶ | my $method = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||
| 579 | ### you didn't give us a value to set! | ||||||||||||||||||||||||||||
| 580 | 15 | ▶ | @_ or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 581 | 15 | my $val = shift; | |||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | ### if there's more arguments than $self, then | ||||||||||||||||||||||||||||
| 584 | ### replace the method called by the accessor. | ||||||||||||||||||||||||||||
| 585 | ### XXX implement rw vs ro accessors! | ||||||||||||||||||||||||||||
| 586 | 15 | $self->{$method}->[VALUE] = $val; | |||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | 15 | return 1; | |||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | =head2 $bool = $self->___alias( ALIAS => METHOD ); | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | Method to directly alias one accessor to another for | ||||||||||||||||||||||||||||
| 594 | this object. It circumvents all sanity checks, etc. | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | Use only if you C<Know What You Are Doing>! | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | =cut | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | 2 | ▶ | sub ___alias { | ||||||||||||||||||||||||||
|
Subroutine
___alias: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 601 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 602 | 2 | ▶ | my $alias = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 603 | 2 | ▶ | my $method = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | 2 | $self->{ $alias }->[ALIAS] = $method; | |||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | 2 | return 1; | |||||||||||||||||||||||||||
| 608 | } | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | 16 | ▶ | sub ___debug { | ||||||||||||||||||||||||||
|
Subroutine
___debug: called CC=2 SCAR=12.4
| |||||||||||||||||||||||||||||
| 611 | 16 | ▶ | return unless $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 614 | 0 | my $msg = shift; | |||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | 0 | local $Carp::CarpLevel += 1; | |||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | 0 | carp($msg); | |||||||||||||||||||||||||||
| 619 | } | ||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | 8 | ▶ | sub ___error { | ||||||||||||||||||||||||||
|
Subroutine
___error: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 622 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 623 | 8 | my $msg = shift; | |||||||||||||||||||||||||||
| 624 | 8 | ▶ | my $lvl = shift || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 625 | 8 | local $Carp::CarpLevel += ($lvl + 1); | |||||||||||||||||||||||||||
| 626 | 8 | ▶ | $FATAL ? croak($msg) : carp($msg); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 627 | } | ||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | ### objects might be overloaded.. if so, we can't trust what "$self" | ||||||||||||||||||||||||||||
| 630 | ### will return, which might get *really* painful.. so check for that | ||||||||||||||||||||||||||||
| 631 | ### and get their unoverloaded stringval if needed. | ||||||||||||||||||||||||||||
| 632 | 63 | ▶ | sub ___callback { | ||||||||||||||||||||||||||
|
Subroutine
___callback: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 633 | 63 | my $self = shift; | |||||||||||||||||||||||||||
| 634 | 63 | my $sub = shift; | |||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | 63 | ▶ | my $mem = overload::Overloaded( $self ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 637 | ? overload::StrVal( $self ) | ||||||||||||||||||||||||||||
| 638 | : "$self"; | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | 63 | ▶ | $self->{$mem} = $sub if $sub; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | 63 | return $self->{$mem}; | |||||||||||||||||||||||||||
| 643 | } | ||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||
| 645 | =head1 LVALUE ACCESSORS | ||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||
| 647 | C<Object::Accessor> supports C<lvalue> attributes as well. To enable | ||||||||||||||||||||||||||||
| 648 | these, you should create your objects in the designated namespace, | ||||||||||||||||||||||||||||
| 649 | C<Object::Accessor::Lvalue>. For example: | ||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||
| 651 | my $obj = Object::Accessor::Lvalue->new('foo'); | ||||||||||||||||||||||||||||
| 652 | $obj->foo += 1; | ||||||||||||||||||||||||||||
| 653 | print $obj->foo; | ||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||
| 655 | will actually print C<1> and work as expected. Since this is an | ||||||||||||||||||||||||||||
| 656 | optional feature, that's not desirable in all cases, we require | ||||||||||||||||||||||||||||
| 657 | you to explicitly use the C<Object::Accessor::Lvalue> class. | ||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | Doing the same on the standard C<Object>>Accessor> class would | ||||||||||||||||||||||||||||
| 660 | generate the following code & errors: | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | my $obj = Object::Accessor->new('foo'); | ||||||||||||||||||||||||||||
| 663 | $obj->foo += 1; | ||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||
| 665 | Can't modify non-lvalue subroutine call | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | Note that C<lvalue> support on C<AUTOLOAD> routines is a | ||||||||||||||||||||||||||||
| 668 | C<perl 5.8.x> feature. See perldoc L<perl58delta> for details. | ||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||
| 670 | =head2 CAVEATS | ||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||
| 672 | =over 4 | ||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||
| 674 | =item * Allow handlers | ||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||
| 676 | Due to the nature of C<lvalue subs>, we never get access to the | ||||||||||||||||||||||||||||
| 677 | value you are assigning, so we can not check it against your allow | ||||||||||||||||||||||||||||
| 678 | handler. Allow handlers are therefor unsupported under C<lvalue> | ||||||||||||||||||||||||||||
| 679 | conditions. | ||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||
| 681 | See C<perldoc perlsub> for details. | ||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||
| 683 | =item * Callbacks | ||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||
| 685 | Due to the nature of C<lvalue subs>, we never get access to the | ||||||||||||||||||||||||||||
| 686 | value you are assigning, so we can not check provide this value | ||||||||||||||||||||||||||||
| 687 | to your callback. Furthermore, we can not distinguish between | ||||||||||||||||||||||||||||
| 688 | a C<get> and a C<set> call. Callbacks are therefor unsupported | ||||||||||||||||||||||||||||
| 689 | under C<lvalue> conditions. | ||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||
| 691 | See C<perldoc perlsub> for details. | ||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||
| 694 | =cut | ||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||
| 696 | { package Object::Accessor::Lvalue; | ||||||||||||||||||||||||||||
| 697 | 7 | ▶ | use base 'Object::Accessor'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 698 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 699 | 7 | ▶ | use vars qw[$AUTOLOAD]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | ### constants needed to access values from the objects | ||||||||||||||||||||||||||||
| 702 | 7 | *VALUE = *Object::Accessor::VALUE; | |||||||||||||||||||||||||||
| 703 | 7 | *ALLOW = *Object::Accessor::ALLOW; | |||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||
| 705 | ### largely copied from O::A::Autoload | ||||||||||||||||||||||||||||
| 706 | 5 | ▶ | sub AUTOLOAD : lvalue { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 707 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 708 | 5 | my($method) = ($AUTOLOAD =~ /([^:']+$)/); | |||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||
| 710 | 5 | ▶ | $self->___autoload( $method, @_ ) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||
| 712 | ### *don't* add return to it, or it won't be stored | ||||||||||||||||||||||||||||
| 713 | ### see perldoc perlsub on lvalue subs | ||||||||||||||||||||||||||||
| 714 | ### XXX can't use $self->___get( ... ), as we MUST have | ||||||||||||||||||||||||||||
| 715 | ### the container that's used for the lvalue assign as | ||||||||||||||||||||||||||||
| 716 | ### the last statement... :( | ||||||||||||||||||||||||||||
| 717 | 5 | $self->{$method}->[ VALUE() ]; | |||||||||||||||||||||||||||
| 718 | } | ||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||
| 720 | 4 | ▶ | sub mk_accessors { | ||||||||||||||||||||||||||
|
Subroutine
mk_accessors: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 721 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 722 | 4 | my $is_hash = UNIVERSAL::isa( $_[0], 'HASH' ); | |||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||
| 724 | 4 | ▶ | $self->___error( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 725 | "Allow handlers are not supported for '". __PACKAGE__ ."' objects" | ||||||||||||||||||||||||||||
| 726 | ) if $is_hash; | ||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||
| 728 | 4 | return $self->SUPER::mk_accessors( @_ ); | |||||||||||||||||||||||||||
| 729 | } | ||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||
| 731 | 1 | ▶ | sub register_callback { | ||||||||||||||||||||||||||
|
Subroutine
register_callback: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 732 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 733 | 1 | $self->___error( | |||||||||||||||||||||||||||
| 734 | "Callbacks are not supported for '". __PACKAGE__ ."' objects" | ||||||||||||||||||||||||||||
| 735 | ); | ||||||||||||||||||||||||||||
| 736 | 1 | return; | |||||||||||||||||||||||||||
| 737 | } | ||||||||||||||||||||||||||||
| 738 | } | ||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||
| 741 | ### standard tie class for bound attributes | ||||||||||||||||||||||||||||
| 742 | 7 | { package Object::Accessor::TIE; | |||||||||||||||||||||||||||
| 743 | 7 | ▶ | use Tie::Scalar; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 744 | 7 | ▶ | use base 'Tie::StdScalar'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||
| 746 | 7 | my %local = (); | |||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||
| 748 | 1 | ▶ | sub TIESCALAR { | ||||||||||||||||||||||||||
|
Subroutine
TIESCALAR: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 749 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 750 | 1 | my $sub = shift; | |||||||||||||||||||||||||||
| 751 | 1 | my $ref = undef; | |||||||||||||||||||||||||||
| 752 | 1 | my $obj = bless \$ref, $class; | |||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||
| 754 | ### store the restore sub | ||||||||||||||||||||||||||||
| 755 | 1 | $local{ $obj } = $sub; | |||||||||||||||||||||||||||
| 756 | 1 | return $obj; | |||||||||||||||||||||||||||
| 757 | } | ||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||
| 759 | 1 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 760 | 1 | my $tied = shift; | |||||||||||||||||||||||||||
| 761 | 1 | my $sub = delete $local{ $tied }; | |||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||
| 763 | ### run the restore sub to set the old value back | ||||||||||||||||||||||||||||
| 764 | 1 | return $sub->(); | |||||||||||||||||||||||||||
| 765 | } | ||||||||||||||||||||||||||||
| 766 | } | ||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||
| 768 | =back | ||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||
| 770 | =head1 GLOBAL VARIABLES | ||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||
| 772 | =head2 $Object::Accessor::FATAL | ||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||
| 774 | Set this variable to true to make all attempted access to non-existent | ||||||||||||||||||||||||||||
| 775 | accessors be fatal. | ||||||||||||||||||||||||||||
| 776 | This defaults to C<false>. | ||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||
| 778 | =head2 $Object::Accessor::DEBUG | ||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||
| 780 | Set this variable to enable debugging output. | ||||||||||||||||||||||||||||
| 781 | This defaults to C<false>. | ||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||
| 783 | =head1 TODO | ||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||
| 785 | =head2 Create read-only accessors | ||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||
| 787 | Currently all accessors are read/write for everyone. Perhaps a future | ||||||||||||||||||||||||||||
| 788 | release should make it possible to have read-only accessors as well. | ||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||
| 790 | =head1 CAVEATS | ||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||
| 792 | If you use codereferences for your allow handlers, you will not be able | ||||||||||||||||||||||||||||
| 793 | to freeze the data structures using C<Storable>. | ||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||
| 795 | Due to a bug in storable (until at least version 2.15), C<qr//> compiled | ||||||||||||||||||||||||||||
| 796 | regexes also don't de-serialize properly. Although this bug has been | ||||||||||||||||||||||||||||
| 797 | reported, you should be aware of this issue when serializing your objects. | ||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||
| 799 | You can track the bug here: | ||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||
| 801 | http://rt.cpan.org/Ticket/Display.html?id=1827 | ||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||
| 803 | =head1 BUG REPORTS | ||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||
| 805 | Please report bugs or other issues to E<lt>bug-object-accessor@rt.cpan.orgE<gt>. | ||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||
| 807 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||
| 809 | This module by Jos Boumans E<lt>kane@cpan.orgE<gt>. | ||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||
| 811 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||
| 813 | This library is free software; you may redistribute and/or modify it | ||||||||||||||||||||||||||||
| 814 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 815 | |||||||||||||||||||||||||||||
| 816 | =cut | ||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||
| 818 | 7 | 1; | |||||||||||||||||||||||||||