| 1 | package Class::Accessor::Lvalue::Trigger; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | use base 'Class::Accessor'; |
|---|
| 6 | |
|---|
| 7 | our $VERSION = '0.01'; |
|---|
| 8 | |
|---|
| 9 | our $TRIGGER_METHOD = sub { |
|---|
| 10 | my $self = shift; |
|---|
| 11 | my $name = shift; |
|---|
| 12 | |
|---|
| 13 | my $last_name = $self->{__class_accessor_lvalue_trigger_lastname}; |
|---|
| 14 | my $last_value = $self->{__class_accessor_lvalue_trigger_lastvalue}; |
|---|
| 15 | |
|---|
| 16 | $self->set($last_name, $self->{$last_name}, $last_value, @_) if $last_name; |
|---|
| 17 | $self->{$name} = $self->get($name, @_); |
|---|
| 18 | |
|---|
| 19 | $self->{__class_accessor_lvalue_trigger_lastname} = $name; |
|---|
| 20 | $self->{__class_accessor_lvalue_trigger_lastvalue} = $self->{$name}; |
|---|
| 21 | |
|---|
| 22 | $self->{$name}; |
|---|
| 23 | }; |
|---|
| 24 | |
|---|
| 25 | sub set { shift->SUPER::set(@_[1..2]) } |
|---|
| 26 | sub get { shift->SUPER::get($_[0]) } |
|---|
| 27 | |
|---|
| 28 | sub make_accessor { |
|---|
| 29 | my($class, $name) = @_; |
|---|
| 30 | |
|---|
| 31 | return sub :lvalue { |
|---|
| 32 | my $self = shift; |
|---|
| 33 | if (ref $TRIGGER_METHOD) { |
|---|
| 34 | $TRIGGER_METHOD->($self, $name, 'rw', @_) if ref $TRIGGER_METHOD eq 'CODE' |
|---|
| 35 | } elsif (defined $TRIGGER_METHOD) { |
|---|
| 36 | $self->$TRIGGER_METHOD($name, 'rw', @_); |
|---|
| 37 | } |
|---|
| 38 | $self->{$name}; |
|---|
| 39 | }; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | sub make_ro_accessor { |
|---|
| 43 | my($class, $name) = @_; |
|---|
| 44 | require Want; |
|---|
| 45 | |
|---|
| 46 | return sub :lvalue { |
|---|
| 47 | my $self = shift; |
|---|
| 48 | if (Want::want('LVALUE')) { |
|---|
| 49 | my $caller = caller; |
|---|
| 50 | require Carp; |
|---|
| 51 | Carp::croak("'$caller' cannot alter the value of '$name' on ". |
|---|
| 52 | "objects of class '$class'"); |
|---|
| 53 | } |
|---|
| 54 | if (ref $TRIGGER_METHOD) { |
|---|
| 55 | $TRIGGER_METHOD->($self, $name, 'ro', @_) if ref $TRIGGER_METHOD eq 'CODE' |
|---|
| 56 | } elsif (defined $TRIGGER_METHOD) { |
|---|
| 57 | $self->$TRIGGER_METHOD($name, 'ro', @_); |
|---|
| 58 | } |
|---|
| 59 | return $self->{$name}; |
|---|
| 60 | }; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | sub make_wo_accessor { |
|---|
| 64 | my($class, $name) = @_; |
|---|
| 65 | require Want; |
|---|
| 66 | |
|---|
| 67 | return sub :lvalue { |
|---|
| 68 | my $self = shift; |
|---|
| 69 | unless (Want::want('LVALUE')) { |
|---|
| 70 | my $caller = caller; |
|---|
| 71 | require Carp; |
|---|
| 72 | Carp::croak("'$caller' cannot access the value of '$name' on ". |
|---|
| 73 | "objects of class '$class'"); |
|---|
| 74 | } |
|---|
| 75 | if (ref $TRIGGER_METHOD) { |
|---|
| 76 | $TRIGGER_METHOD->($self, $name, 'wo', @_) if ref $TRIGGER_METHOD eq 'CODE' |
|---|
| 77 | } elsif (defined $TRIGGER_METHOD) { |
|---|
| 78 | $self->$TRIGGER_METHOD($name, 'wo', @_); |
|---|
| 79 | } |
|---|
| 80 | $self->{$name}; |
|---|
| 81 | }; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | 1; |
|---|
| 85 | __END__ |
|---|
| 86 | |
|---|
| 87 | =head1 NAME |
|---|
| 88 | |
|---|
| 89 | Class::Accessor::Lvalue::Trigger - |
|---|
| 90 | |
|---|
| 91 | =head1 SYNOPSIS |
|---|
| 92 | |
|---|
| 93 | use Class::Accessor::Lvalue::Trigger; |
|---|
| 94 | |
|---|
| 95 | =head1 DESCRIPTION |
|---|
| 96 | |
|---|
| 97 | Class::Accessor::Lvalue::Trigger is |
|---|
| 98 | |
|---|
| 99 | =head1 AUTHOR |
|---|
| 100 | |
|---|
| 101 | Kazuhiro Osawa E<lt>ko@yappo.ne.jpE<gt> |
|---|
| 102 | |
|---|
| 103 | =head1 SEE ALSO |
|---|
| 104 | |
|---|
| 105 | L<Class::Accessor>, L<Class::Accessor::Lvalue::Fast> |
|---|
| 106 | |
|---|
| 107 | =head1 LICENSE |
|---|
| 108 | |
|---|
| 109 | This library is free software; you can redistribute it and/or modify |
|---|
| 110 | it under the same terms as Perl itself. |
|---|
| 111 | |
|---|
| 112 | =cut |
|---|