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