1 | package Class::Accessor::Fast;
|
---|
2 | use base 'Class::Accessor';
|
---|
3 | use strict;
|
---|
4 | $Class::Accessor::Fast::VERSION = '0.34';
|
---|
5 |
|
---|
6 | sub 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 |
|
---|
16 | sub 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 |
|
---|
27 | sub 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 |
|
---|
43 | 1;
|
---|
44 |
|
---|
45 | __END__
|
---|
46 |
|
---|
47 | =head1 NAME
|
---|
48 |
|
---|
49 | Class::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 |
|
---|
60 | This is a faster but less expandable version of Class::Accessor.
|
---|
61 | Class::Accessor's generated accessors require two method calls to accompish
|
---|
62 | their task (one for the accessor, another for get() or set()).
|
---|
63 | Class::Accessor::Fast eliminates calling set()/get() and does the access itself,
|
---|
64 | resulting in a somewhat faster accessor.
|
---|
65 |
|
---|
66 | The downside is that you can't easily alter the behavior of your
|
---|
67 | accessors, nor can your subclasses. Of course, should you need this
|
---|
68 | later, you can always swap out Class::Accessor::Fast for
|
---|
69 | Class::Accessor.
|
---|
70 |
|
---|
71 | Read the documentation for Class::Accessor for more info.
|
---|
72 |
|
---|
73 | =head1 EFFICIENCY
|
---|
74 |
|
---|
75 | L<Class::Accessor/EFFICIENCY> for an efficiency comparison.
|
---|
76 |
|
---|
77 | =head1 AUTHORS
|
---|
78 |
|
---|
79 | Copyright 2007 Marty Pauley <[email protected]>
|
---|
80 |
|
---|
81 | This program is free software; you can redistribute it and/or modify it under
|
---|
82 | the same terms as Perl itself. That means either (a) the GNU General Public
|
---|
83 | License or (b) the Artistic License.
|
---|
84 |
|
---|
85 | =head2 ORIGINAL AUTHOR
|
---|
86 |
|
---|
87 | Michael G Schwern <[email protected]>
|
---|
88 |
|
---|
89 | =head1 SEE ALSO
|
---|
90 |
|
---|
91 | L<Class::Accessor>
|
---|
92 |
|
---|
93 | =cut
|
---|