source: main/trunk/greenstone2/perllib/cpan/Class/Accessor/Fast.pm@ 23064

Last change on this file since 23064 was 23064, checked in by davidb, 14 years ago

Supporting Perl classes (100% pure Perl) for DL talkback facility

File size: 2.2 KB
Line 
1package Class::Accessor::Fast;
2use base 'Class::Accessor';
3use strict;
4$Class::Accessor::Fast::VERSION = '0.34';
5
6sub make_accessor {
7 my($class, $field) = @_;
8
9 return sub {
10 return $_[0]->{$field} if scalar(@_) == 1;
11 return $_[0]->{$field} = scalar(@_) == 2 ? $_[1] : [@_[1..$#_]];
12 };
13}
14
15
16sub make_ro_accessor {
17 my($class, $field) = @_;
18
19 return sub {
20 return $_[0]->{$field} if @_ == 1;
21 my $caller = caller;
22 $_[0]->_croak("'$caller' cannot alter the value of '$field' on objects of class '$class'");
23 };
24}
25
26
27sub make_wo_accessor {
28 my($class, $field) = @_;
29
30 return sub {
31 if (@_ == 1) {
32 my $caller = caller;
33 $_[0]->_croak("'$caller' cannot access the value of '$field' on objects of class '$class'");
34 }
35 else {
36 return $_[0]->{$field} = $_[1] if @_ == 2;
37 return (shift)->{$field} = \@_;
38 }
39 };
40}
41
42
431;
44
45__END__
46
47=head1 NAME
48
49Class::Accessor::Fast - Faster, but less expandable, accessors
50
51=head1 SYNOPSIS
52
53 package Foo;
54 use base qw(Class::Accessor::Fast);
55
56 # The rest is the same as Class::Accessor but without set() and get().
57
58=head1 DESCRIPTION
59
60This is a faster but less expandable version of Class::Accessor.
61Class::Accessor's generated accessors require two method calls to accompish
62their task (one for the accessor, another for get() or set()).
63Class::Accessor::Fast eliminates calling set()/get() and does the access itself,
64resulting in a somewhat faster accessor.
65
66The downside is that you can't easily alter the behavior of your
67accessors, nor can your subclasses. Of course, should you need this
68later, you can always swap out Class::Accessor::Fast for
69Class::Accessor.
70
71Read the documentation for Class::Accessor for more info.
72
73=head1 EFFICIENCY
74
75L<Class::Accessor/EFFICIENCY> for an efficiency comparison.
76
77=head1 AUTHORS
78
79Copyright 2007 Marty Pauley <[email protected]>
80
81This program is free software; you can redistribute it and/or modify it under
82the same terms as Perl itself. That means either (a) the GNU General Public
83License or (b) the Artistic License.
84
85=head2 ORIGINAL AUTHOR
86
87Michael G Schwern <[email protected]>
88
89=head1 SEE ALSO
90
91L<Class::Accessor>
92
93=cut
Note: See TracBrowser for help on using the repository browser.