File Coverage

blib/lib/Class/Accessor/Class.pm
Criterion Covered Total %
statement 43 43 100.0
branch 6 6 100.0
condition 1 3 33.3
subroutine 13 13 100.0
pod 4 4 100.0
total 67 69 97.1


line stmt bran cond sub pod time code
1 3     3   227551 use strict;
  3         36  
  3         91  
2 3     3   15 use warnings;
  3         7  
  3         197  
3             package Class::Accessor::Class 0.504;
4 3     3   1767 use Class::Accessor 0.16 ();
  3         6033  
  3         93  
5 3     3   1604 use parent 'Class::Accessor';
  3         1051  
  3         17  
6             # ABSTRACT: simple class variable accessors
7              
8             #pod =head1 SYNOPSIS
9             #pod
10             #pod Set up a module with class accessors:
11             #pod
12             #pod package Text::Fortune;
13             #pod
14             #pod use base qw(Class::Accessor::Class Exporter);
15             #pod Robot->mk_class_accessors(qw(language offensive collection));
16             #pod
17             #pod sub fortune {
18             #pod if (__PACKAGE__->offensive) {
19             #pod ..
20             #pod
21             #pod Then, when using the module:
22             #pod
23             #pod use Text::Fortune;
24             #pod
25             #pod Text::Fortune->offensive(1);
26             #pod
27             #pod print fortune; # prints an offensive fortune
28             #pod
29             #pod Text::Fortune->language('EO');
30             #pod
31             #pod print fortune; # prints an offensive fortune in Esperanto
32             #pod
33             #pod =head1 DESCRIPTION
34             #pod
35             #pod Class::Accessor::Class provides a simple way to create accessor and mutator
36             #pod methods for class variables, just as Class::Accessor provides for objects. It
37             #pod can use either an enclosed lexical variable, or a package variable.
38             #pod
39             #pod This module was once implemented in terms of Class::Accessor, but changes to
40             #pod that module broke this relationship. Class::Accessor::Class is still a
41             #pod subclass of Class::Accessor, strictly for historical reasons. As a side
42             #pod benefit, a class that isa Class::Accessor::Class is also a Class::Accessor
43             #pod and can use its methods.
44             #pod
45             #pod =method mk_class_accessors
46             #pod
47             #pod package Foo;
48             #pod use base qw(Class::Accessor::Class);
49             #pod Foo->mk_class_accessors(qw(foo bar baz));
50             #pod
51             #pod Foo->foo(10);
52             #pod my $obj = new Foo;
53             #pod print $obj->foo; # 10
54             #pod
55             #pod This method adds accessors for the named class variables. The accessor will
56             #pod get or set a lexical variable to which the accessor is the only access.
57             #pod
58             #pod =cut
59              
60             sub mk_class_accessors {
61 1     1 1 673 my ($self, @fields) = @_;
62              
63             ## no critic (ProhibitNoStrict)
64 3     3   347 no strict 'refs';
  3         9  
  3         264  
65 1         4 for my $field (@fields) {
66 1         8 *{"${self}::$field"} = $self->make_class_accessor($field);
  1         8  
67             }
68             }
69              
70             #pod =method mk_package_accessors
71             #pod
72             #pod package Foo;
73             #pod use base qw(Class::Accessor::Class);
74             #pod Foo->mk_package_accessors(qw(foo bar baz));
75             #pod
76             #pod Foo->foo(10);
77             #pod my $obj = new Foo;
78             #pod print $obj->foo; # 10
79             #pod print $Foo::foo; # 10
80             #pod
81             #pod This method adds accessors for the named class variables. The accessor will
82             #pod get or set the named variable in the package's symbol table.
83             #pod
84             #pod =cut
85              
86             sub mk_package_accessors {
87 1     1 1 762 my ($self, @fields) = @_;
88              
89             ## no critic (ProhibitNoStrict)
90 3     3   18 no strict 'refs';
  3         7  
  3         1110  
91 1         4 for my $field (@fields) {
92 1         10 *{"${self}::$field"} = $self->make_package_accessor($field);
  1         7  
93             }
94             }
95              
96             #pod =head1 DETAILS
97             #pod
98             #pod =head2 make_class_accessor
99             #pod
100             #pod $accessor = Class->make_class_accessor($field);
101             #pod
102             #pod This method generates a subroutine reference which acts as an accessor for the
103             #pod named field.
104             #pod
105             #pod =cut
106              
107             {
108             my %accessor;
109              
110             sub make_class_accessor {
111 4     4 1 141 my ($class, $field) = @_;
112              
113             return $accessor{$class}{$field}
114 4 100       18 if $accessor{$class}{$field};
115              
116 3         6 my $field_value;
117              
118             $accessor{$class}{$field} = sub {
119 37     37   4195 my $class = shift;
120              
121             return @_
122 37 100       154 ? ($field_value = $_[0])
123             : $field_value;
124             }
125 3         18 }
126             }
127              
128             #pod =head2 make_package_accessor
129             #pod
130             #pod $accessor = Class->make_package_accessor($field);
131             #pod
132             #pod This method generates a subroutine reference which acts as an accessor for the
133             #pod named field, which is stored in the scalar named C in C's symbol
134             #pod table.
135             #pod
136             #pod This can be useful for dealing with legacy code, but using package variables is
137             #pod almost never a good idea for new code. Use this with care.
138             #pod
139             #pod =cut
140              
141             sub make_package_accessor {
142 1     1 1 4 my ($self, $field) = @_;
143 1   33     8 my $class = ref $self || $self;
144              
145 1         4 my $varname = "$class\:\:$field";
146             return sub {
147 30     30   2548 my $class = shift;
148              
149             ## no critic (ProhibitNoStrict)
150 3     3   24 no strict 'refs';
  3         6  
  3         266  
151             return @_
152 5         26 ? (${$varname} = $_[0])
153 30 100       76 : ${$varname}
  25         176  
154             }
155 1         7 }
156              
157             1;
158              
159             __END__