File Coverage

lib/Object/Accessor.pm
Criterion Covered Total %
statement 188 196 95.9
branch 71 92 77.1
condition 6 8 75.0
subroutine 39 40 97.5
pod 10 10 100.0
total 314 346 90.7


line stmt bran cond sub pod time code
1             package Object::Accessor;
2 7     7   96880 use if $] > 5.017, 'deprecate';
  7         76  
  7         47  
3              
4 7     7   12492 use strict;
  7         16  
  7         220  
5 7     7   51 use Carp qw[carp croak];
  7         13  
  7         579  
6 7     7   38 use vars qw[$FATAL $DEBUG $AUTOLOAD $VERSION];
  7         15  
  7         514  
7 7     7   7169 use Params::Check qw[allow];
  7         43112  
  7         926  
8              
9             ### some objects might have overload enabled, we'll need to
10             ### disable string overloading for callbacks
11             require overload;
12              
13             $VERSION = '0.48';
14             $FATAL = 0;
15             $DEBUG = 0;
16              
17 7     7   68 use constant VALUE => 0; # array index in the hash value
  7         16  
  7         1010  
18 7     7   34 use constant ALLOW => 1; # array index in the hash value
  7         13  
  7         266  
19 7     7   30 use constant ALIAS => 2; # array index in the hash value
  7         12  
  7         16447  
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 provides an interface to create per object
102             accessors (as opposed to per C accessors, as, for example,
103             C 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
107             object inside your own object, and access the accessors from there.
108             See the C for examples.
109              
110             =head1 METHODS
111              
112             =head2 $object = Object::Accessor->new( [ARGS] );
113              
114             Creates a new (and empty) C object. This method is
115             inheritable.
116              
117             Any arguments given to C are passed straight to C.
118              
119             If you want to be able to assign to your accessors as if they
120             were Cs, you should create your object in the
121             C namespace instead. See the section
122             on C below.
123              
124             =cut
125              
126             sub new {
127 16     16 1 6620 my $class = shift;
128 16         57 my $obj = bless {}, $class;
129              
130 16 100       70 $obj->mk_accessors( @_ ) if @_;
131              
132 16         47 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 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.
141              
142             When providing a C 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's C 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 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
161             VARIABLES> for details.
162              
163             Note that you can bind the values of attributes to a scope. This allows
164             you to C 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 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
187             section for details.
188              
189             =cut
190              
191             sub mk_accessors {
192 15     15 1 6868 my $self = $_[0];
193 15         78 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 100       85 for my $acc ( $is_hash ? keys %{$_[1]} : @_[1..$#_] ) {
  2         9  
198              
199             ### already created apparently
200 16 100       60 if( exists $self->{$acc} ) {
201 1         5 __PACKAGE__->___debug( "Accessor '$acc' already exists");
202 1         3 next;
203             }
204              
205 15         95 __PACKAGE__->___debug( "Creating accessor '$acc'");
206              
207             ### explicitly vivify it, so that exists works in ls_accessors()
208 15         53 $self->{$acc}->[VALUE] = undef;
209              
210             ### set the allow handler only if one was specified
211 15 100       64 $self->{$acc}->[ALLOW] = $_[1]->{$acc} if $is_hash;
212             }
213              
214 15         63 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 method.
222              
223             =cut
224              
225             sub ls_accessors {
226             ### metainformation is stored in the stringified
227             ### key of the object, so skip that when listing accessors
228 17     17 1 3159 return sort grep { $_ ne "$_[0]" } keys %{$_[0]};
  21         235  
  17         57  
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's C handler. If there was no allow handler
235             specified, an allow handler that always returns true will be returned.
236              
237             =cut
238              
239             sub ls_allow {
240 3     3 1 8 my $self = shift;
241 3 50       9 my $key = shift or return;
242             return exists $self->{$key}->[ALLOW]
243             ? $self->{$key}->[ALLOW]
244 3 50   0   26 : sub { 1 };
  0         0  
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             sub mk_aliases {
266 1     1 1 404 my $self = shift;
267 1         3 my %aliases = @_;
268              
269 1         6 while( my($alias, $method) = each %aliases ) {
270              
271             ### already created apparently
272 1 50       7 if( exists $self->{$alias} ) {
273 0         0 __PACKAGE__->___debug( "Accessor '$alias' already exists");
274 0         0 next;
275             }
276              
277 1         4 $self->___alias( $alias => $method );
278             }
279              
280 1         4 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             sub mk_clone {
293 7     7 1 3559 my $self = $_[0];
294 7         16 my $class = ref $self;
295              
296 7         35 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         11 my %hash; my @list;
302 7         27 for my $acc ( $self->ls_accessors ) {
303 7         18 my $allow = $self->{$acc}->[ALLOW];
304 7 50       22 $allow ? $hash{$acc} = $allow : push @list, $acc;
305              
306             ### is this an alias?
307 7 100       38 if( my $org = $self->{ $acc }->[ ALIAS ] ) {
308 1         3 $clone->___alias( $acc => $org );
309             }
310             }
311              
312             ### copy the accessors from $self to $clone
313 7 50       27 $clone->mk_accessors( \%hash ) if %hash;
314 7 100       35 $clone->mk_accessors( @list ) if @list;
315              
316             ### copy callbacks
317             #$clone->{"$clone"} = $self->{"$self"} if $self->{"$self"};
318 7         22 $clone->___callback( $self->___callback );
319              
320 7         29 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.
327              
328             Returns true on success and false on failure.
329              
330             =cut
331              
332             sub mk_flush {
333 3     3 1 2439 my $self = $_[0];
334              
335             # set each accessor's data to undef
336 3         11 $self->{$_}->[VALUE] = undef for $self->ls_accessors;
337              
338 3         18 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             sub mk_verify {
350 2     2 1 5 my $self = $_[0];
351              
352 2         4 my $fail;
353 2         7 for my $name ( $self->ls_accessors ) {
354 2 100       13 unless( allow( $self->$name, $self->ls_allow( $name ) ) ) {
355 1 50       20 my $val = defined $self->$name ? $self->$name : '';
356              
357 1         9 __PACKAGE__->___error("'$name' ($val) is invalid");
358 1         56 $fail++;
359             }
360             }
361              
362 2 100       24 return if $fail;
363 1         2 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 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             sub register_callback {
398 1     1 1 2 my $self = shift;
399 1 50       4 my $sub = shift or return;
400              
401             ### use the memory address as key, it's not used EVER as an
402             ### accessor --kane
403 1         4 $self->___callback( $sub );
404              
405 1         6 return 1;
406             }
407              
408              
409             =head2 $bool = $self->can( METHOD_NAME )
410              
411             This method overrides C in order to provide coderefs to
412             accessors which are loaded on demand. It will behave just like
413             C 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 for more examples.
425              
426             =cut
427              
428             ### custom 'can' as UNIVERSAL::can ignores autoload
429             sub can {
430 26     26 1 14611 my($self, $method) = @_;
431              
432             ### it's one of our regular methods
433 26         151 my $code = $self->UNIVERSAL::can($method);
434 26 100       99 if( $code ) {
435 1 50       3 carp( "Can '$method' -- provided by package" ) if $DEBUG;
436 1         4 return $code;
437             }
438              
439             ### it's an accessor we provide;
440 25 100 100     337 if( UNIVERSAL::isa( $self, 'HASH' ) and exists $self->{$method} ) {
441 9 50       31 carp( "Can '$method' -- provided by object" ) if $DEBUG;
442 4     4   2634 return sub { $self->$method(@_); }
443 9         80 }
444              
445             ### we don't support it
446 16 50       43 carp( "Cannot '$method'" ) if $DEBUG;
447 16         250 return;
448             }
449              
450             ### don't autoload this
451 16     16   10030 sub DESTROY { 1 };
452              
453             ### use autoload so we can have per-object accessors,
454             ### not per class, as that is incorrect
455             sub AUTOLOAD {
456 48     48   12913 my $self = shift;
457 48         456 my($method) = ($AUTOLOAD =~ /([^:']+$)/);
458              
459 48 100       281 my $val = $self->___autoload( $method, @_ ) or return;
460              
461 43         306 return $val->[0];
462             }
463              
464             sub ___autoload {
465 59     59   99 my $self = shift;
466 59         81 my $method = shift;
467 59         133 my $assign = scalar @_; # is this an assignment?
468              
469             ### a method on our object
470 59 100       406 if( UNIVERSAL::isa( $self, 'HASH' ) ) {
471 58 100       176 if ( not exists $self->{$method} ) {
472 3         17 __PACKAGE__->___error("No such accessor '$method'", 1);
473 2         139 return;
474             }
475              
476             ### a method on something else, die with a descriptive error;
477             } else {
478 1         4 local $FATAL = 1;
479 1         12 __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 100       168 if( my $original = $self->{ $method }->[ALIAS] ) {
487 6         18 return $self->___autoload( $original, @_ );
488             }
489              
490             ### assign?
491 49 100       168 my $val = $assign ? shift(@_) : $self->___get( $method );
492              
493 49 100       125 if( $assign ) {
494              
495             ### any binding?
496 15 100       158 if( $_[0] ) {
497 1 50 33     13 if( ref $_[0] and 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         3 my $cur = $self->{$method}->[VALUE];
503              
504 1         12 tie ${$_[0]}, __PACKAGE__ . '::TIE',
505 1     1   2 sub { $self->$method( $cur ) };
  1         6  
506              
507 1         3 ${$_[0]} = $val;
  1         12  
508              
509             } else {
510 0         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 100       62 if( defined $self->{$method}->[ALLOW] ) {
518              
519             ### double assignment due to 'used only once' warnings
520 2         5 local $Params::Check::VERBOSE = 0;
521 2         2 local $Params::Check::VERBOSE = 0;
522              
523 2 100       11 allow( $val, $self->{$method}->[ALLOW] ) or (
524             __PACKAGE__->___error(
525             "'$val' is an invalid value for '$method'", 1),
526             return
527             );
528             }
529             }
530              
531             ### callbacks?
532 48 100       141 if( my $sub = $self->___callback ) {
533 3 100       5 $val = eval { $sub->( $self, $method, ($assign ? [$val] : []) ) };
  3         16  
534              
535             ### register the error
536 3 50       9843 $self->___error( $@, 1 ), return if $@;
537             }
538              
539             ### now we can actually assign it
540 48 100       109 if( $assign ) {
541 14 50       199 $self->___set( $method, $val ) or return;
542             }
543              
544 48         335 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! 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             sub ___get {
560 38     38   464 my $self = shift;
561 38 50       113 my $method = shift or return;
562 38         114 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! General usage for
571             this functionality would be in your own custom callbacks.
572              
573             =cut
574              
575             sub ___set {
576 15     15   414 my $self = shift;
577 15 50       42 my $method = shift or return;
578              
579             ### you didn't give us a value to set!
580 15 50       40 @_ or return;
581 15         379 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         34 $self->{$method}->[VALUE] = $val;
587              
588 15         57 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!
597              
598             =cut
599              
600             sub ___alias {
601 2     2   3 my $self = shift;
602 2 50       7 my $alias = shift or return;
603 2 50       5 my $method = shift or return;
604              
605 2         6 $self->{ $alias }->[ALIAS] = $method;
606              
607 2         7 return 1;
608             }
609              
610             sub ___debug {
611 16 50   16   270 return unless $DEBUG;
612              
613 0         0 my $self = shift;
614 0         0 my $msg = shift;
615              
616 0         0 local $Carp::CarpLevel += 1;
617              
618 0         0 carp($msg);
619             }
620              
621             sub ___error {
622 8     8   48 my $self = shift;
623 8         41 my $msg = shift;
624 8   100     34 my $lvl = shift || 0;
625 8         23 local $Carp::CarpLevel += ($lvl + 1);
626 8 100       3828 $FATAL ? croak($msg) : carp($msg);
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             sub ___callback {
633 63     63   82 my $self = shift;
634 63         82 my $sub = shift;
635              
636 63 50       280 my $mem = overload::Overloaded( $self )
637             ? overload::StrVal( $self )
638             : "$self";
639              
640 63 100       16685 $self->{$mem} = $sub if $sub;
641              
642 63         406 return $self->{$mem};
643             }
644              
645             =head1 LVALUE ACCESSORS
646              
647             C supports C attributes as well. To enable
648             these, you should create your objects in the designated namespace,
649             C. 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 class.
658              
659             Doing the same on the standard C>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 support on C routines is a
668             C feature. See perldoc L for details.
669              
670             =head2 CAVEATS
671              
672             =over 4
673              
674             =item * Allow handlers
675              
676             Due to the nature of C, 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
679             conditions.
680              
681             See C for details.
682              
683             =item * Callbacks
684              
685             Due to the nature of C, 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 and a C call. Callbacks are therefor unsupported
689             under C conditions.
690              
691             See C for details.
692              
693              
694             =cut
695              
696             { package Object::Accessor::Lvalue;
697 7     7   59 use base 'Object::Accessor';
  7         11  
  7         783  
698 7     7   53 use strict;
  7         11  
  7         314  
699 7     7   32 use vars qw[$AUTOLOAD];
  7         14  
  7         2699  
700              
701             ### constants needed to access values from the objects
702             *VALUE = *Object::Accessor::VALUE;
703             *ALLOW = *Object::Accessor::ALLOW;
704              
705             ### largely copied from O::A::Autoload
706             sub AUTOLOAD : lvalue {
707 5     5   696 my $self = shift;
708 5         25 my($method) = ($AUTOLOAD =~ /([^:']+$)/);
709              
710 5 50       14 $self->___autoload( $method, @_ ) or return;
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         42 $self->{$method}->[ VALUE() ];
718             }
719              
720             sub mk_accessors {
721 4     4   298 my $self = shift;
722 4         17 my $is_hash = UNIVERSAL::isa( $_[0], 'HASH' );
723              
724 4 100       15 $self->___error(
725             "Allow handlers are not supported for '". __PACKAGE__ ."' objects"
726             ) if $is_hash;
727              
728 4         103 return $self->SUPER::mk_accessors( @_ );
729             }
730              
731             sub register_callback {
732 1     1   253 my $self = shift;
733 1         3 $self->___error(
734             "Callbacks are not supported for '". __PACKAGE__ ."' objects"
735             );
736 1         77 return;
737             }
738             }
739              
740              
741             ### standard tie class for bound attributes
742             { package Object::Accessor::TIE;
743 7     7   7766 use Tie::Scalar;
  7         5546  
  7         197  
744 7     7   46 use base 'Tie::StdScalar';
  7         13  
  7         5721  
745              
746             my %local = ();
747              
748             sub TIESCALAR {
749 1     1   2 my $class = shift;
750 1         2 my $sub = shift;
751 1         2 my $ref = undef;
752 1         4 my $obj = bless \$ref, $class;
753              
754             ### store the restore sub
755 1         12 $local{ $obj } = $sub;
756 1         6 return $obj;
757             }
758              
759             sub DESTROY {
760 1     1   590 my $tied = shift;
761 1         4 my $sub = delete $local{ $tied };
762              
763             ### run the restore sub to set the old value back
764 1         5 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.
777              
778             =head2 $Object::Accessor::DEBUG
779              
780             Set this variable to enable debugging output.
781             This defaults to C.
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.
794              
795             Due to a bug in storable (until at least version 2.15), C 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 Ebug-object-accessor@rt.cpan.orgE.
806              
807             =head1 AUTHOR
808              
809             This module by Jos Boumans Ekane@cpan.orgE.
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             1;