]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - config/mpfire/perl/Accessor/Faster.pm
kernel: disable FW_LOADER_USER_HELPER_FALLBACK
[people/pmueller/ipfire-2.x.git] / config / mpfire / perl / Accessor / Faster.pm
CommitLineData
83d20a45
CS
1package Class::Accessor::Faster;
2use base 'Class::Accessor';
3use strict;
4$Class::Accessor::Faster::VERSION = '0.31';
5
6=head1 NAME
7
8Class::Accessor::Faster - Even faster, but less expandable, accessors
9
10=head1 SYNOPSIS
11
12 package Foo;
13 use base qw(Class::Accessor::Faster);
14
15=head1 DESCRIPTION
16
17This is a faster but less expandable version of Class::Accessor::Fast.
18
19Class::Accessor's generated accessors require two method calls to accompish
20their task (one for the accessor, another for get() or set()).
21
22Class::Accessor::Fast eliminates calling set()/get() and does the access itself,
23resulting in a somewhat faster accessor.
24
25Class::Accessor::Faster uses an array reference underneath to be faster.
26
27Read the documentation for Class::Accessor for more info.
28
29=cut
30
31my %slot;
32sub _slot {
33 my($class, $field) = @_;
34 my $n = $slot{$class}->{$field};
35 return $n if defined $n;
36 $n = keys %{$slot{$class}};
37 $slot{$class}->{$field} = $n;
38 return $n;
39}
40
41sub new {
42 my($proto, $fields) = @_;
43 my($class) = ref $proto || $proto;
44 my $self = bless [], $class;
45
46 $fields = {} unless defined $fields;
47 for my $k (keys %$fields) {
48 my $n = $class->_slot($k);
49 $self->[$n] = $fields->{$k};
50 }
51 return $self;
52}
53
54sub make_accessor {
55 my($class, $field) = @_;
56 my $n = $class->_slot($field);
57 return sub {
58 return $_[0]->[$n] if @_ == 1;
59 return $_[0]->[$n] = $_[1] if @_ == 2;
60 return (shift)->[$n] = \@_;
61 };
62}
63
64
65sub make_ro_accessor {
66 my($class, $field) = @_;
67 my $n = $class->_slot($field);
68 return sub {
69 return $_[0]->[$n] if @_ == 1;
70 my $caller = caller;
71 $_[0]->_croak("'$caller' cannot alter the value of '$field' on objects of class '$class'");
72 };
73}
74
75
76sub make_wo_accessor {
77 my($class, $field) = @_;
78 my $n = $class->_slot($field);
79 return sub {
80 if (@_ == 1) {
81 my $caller = caller;
82 $_[0]->_croak("'$caller' cannot access the value of '$field' on objects of class '$class'");
83 } else {
84 return $_[0]->[$n] = $_[1] if @_ == 2;
85 return (shift)->[$n] = \@_;
86 }
87 };
88}
89
90
91=head1 AUTHORS
92
93Copyright 2007 Marty Pauley <marty+perl@kasei.com>
94
95This program is free software; you can redistribute it and/or modify it under
96the same terms as Perl itself. That means either (a) the GNU General Public
97License or (b) the Artistic License.
98
99=head1 SEE ALSO
100
101L<Class::Accessor>
102
103=cut
104
1051;