Generates a subroutine refrence which acts as a read-only accessor for
the given $field. It only calls get().
Override get() to change the behavior of your accessors.
=cut
sub make_ro_accessor {
my($class, $field) = @_;
return sub {
my $self = shift;
if (@_) {
my $caller = caller;
$self->_croak("'$caller' cannot alter the value of '$field' on objects of class '$class'");
}
else {
return $self->get($field);
}
};
}
=head2 make_wo_accessor
$read_only_accessor = Class->make_wo_accessor($field);
Generates a subroutine refrence which acts as a write-only accessor
(mutator) for the given $field. It only calls set().
Override set() to change the behavior of your accessors.
=cut
sub make_wo_accessor {
my($class, $field) = @_;
return sub {
my $self = shift;
unless (@_) {
my $caller = caller;
$self->_croak("'$caller' cannot access the value of '$field' on objects of class '$class'");
}
else {
return $self->set($field, @_);
}
};
}
=head1 EXCEPTIONS
If something goes wrong Class::Accessor will warn or die by calling Carp::carp
or Carp::croak. If you don't like this you can override _carp() and _croak() in
your subclass and do whatever else you want.
=cut
use Carp ();
sub _carp {
my ($self, $msg) = @_;
Carp::carp($msg || $self);
return;
}
sub _croak {
my ($self, $msg) = @_;
Carp::croak($msg || $self);
return;
}
=head1 EFFICIENCY
Class::Accessor does not employ an autoloader, thus it is much faster
than you'd think. Its generated methods incur no special penalty over
ones you'd write yourself.
accessors:
Rate Basic Average Fast Faster Direct
Basic 189150/s -- -42% -51% -55% -89%
Average 327679/s 73% -- -16% -22% -82%
Fast 389212/s 106% 19% -- -8% -78%
Faster 421646/s 123% 29% 8% -- -76%
Direct 1771243/s 836% 441% 355% 320% --
mutators:
Rate Basic Average Fast Faster Direct
Basic 173769/s -- -34% -53% -59% -90%
Average 263046/s 51% -- -29% -38% -85%
Fast 371158/s 114% 41% -- -13% -78%
Faster 425821/s 145% 62% 15% -- -75%
Direct 1699081/s 878% 546% 358% 299% --
Class::Accessor::Fast is faster than methods written by an average programmer
(where "average" is based on Schwern's example code).
Class::Accessor is slower than average, but more flexible.
=5= |