Debian Codegraph @

README

raw
1 NAME
2 Class::Accessor::Grouped - Lets you build groups of accessors
3
4 SYNOPSIS
5 use base 'Class::Accessor::Grouped';
6
7 # make basic accessors for objects
8 __PACKAGE__->mk_group_accessors(simple => qw(id name email));
9
10 # make accessor that works for objects and classes
11 __PACKAGE__->mk_group_accessors(inherited => 'awesome_level');
12
13 # make an accessor which calls a custom pair of getters/setters
14 sub get_column { ... this will be called when you do $obj->name() ... }
15 sub set_column { ... this will be called when you do $obj->name('foo') ... }
16 __PACKAGE__->mk_group_accessors(column => 'name');
17
18 DESCRIPTION
19 This class lets you build groups of accessors that will call different
20 getters and setters. The documentation of this module still requires a
21 lot of work (volunteers welcome >.>), but in the meantime you can refer
22 to this post
23 <http://lo-f.at/glahn/2009/08/WritingPowerfulAccessorsForPerlClasses.htm
24 l> for more information.
25
26 Notes on accessor names
27 In general method names in Perl are considered identifiers, and as such
28 need to conform to the identifier specification of
29 "qr/\A[A-Z_a-z][0-9A-Z_a-z]*\z/". While it is rather easy to invoke
30 methods with non-standard names ("$obj->${\"anything goes"}"), it is not
31 possible to properly declare such methods without the use of Sub::Name.
32 Since this module must be able to function identically with and without
33 its optional dependencies, starting with version 0.10008 attempting to
34 declare an accessor with a non-standard name is a fatal error (such
35 operations would silently succeed since version 0.08004, as long as
36 Sub::Name is present, or otherwise would result in a syntax error during
37 a string eval).
38
39 Unfortunately in the years since 0.08004 a rather large body of code
40 accumulated in the wild that does attempt to declare accessors with
41 funny names. One notable perpetrator is DBIx::Class::Schema::Loader,
42 which under certain conditions could create accessors of the "column"
43 group which start with numbers and/or some other punctuation (the proper
44 way would be to declare columns with the "accessor" attribute set to
45 "undef").
46
47 Therefore an escape mechanism is provided via the environment variable
48 "CAG_ILLEGAL_ACCESSOR_NAME_OK". When set to a true value, one warning is
49 issued per class on attempts to declare an accessor with a
50 non-conforming name, and as long as Sub::Name is available all accessors
51 will be properly created. Regardless of this setting, accessor names
52 containing nulls "\0" are disallowed, due to various deficiencies in
53 perl itself.
54
55 If your code base has too many instances of illegal accessor
56 declarations, and a fix is not feasible due to time constraints, it is
57 possible to disable the warnings altogether by setting
58 $ENV{CAG_ILLEGAL_ACCESSOR_NAME_OK} to "DO_NOT_WARN" (observe
59 capitalization).
60
61 METHODS
62 mk_group_accessors
63 __PACKAGE__->mk_group_accessors(simple => 'hair_length', [ hair_color => 'hc' ]);
64
65 Arguments: $group, @fieldspec
66 Returns: none
67
68 Creates a set of accessors in a given group.
69
70 $group is the name of the accessor group for the generated accessors;
71 they will call get_$group($field) on get and set_$group($field, $value)
72 on set.
73
74 If you want to mimic Class::Accessor's mk_accessors $group has to be
75 'simple' to tell Class::Accessor::Grouped to use its own get_simple and
76 set_simple methods.
77
78 @fieldspec is a list of field/accessor names; if a fieldspec is a scalar
79 this is used as both field and accessor name, if a listref it is
80 expected to be of the form [ $accessor, $field ].
81
82 mk_group_ro_accessors
83 __PACKAGE__->mk_group_ro_accessors(simple => 'birthdate', [ social_security_number => 'ssn' ]);
84
85 Arguments: $group, @fieldspec
86 Returns: none
87
88 Creates a set of read only accessors in a given group. Identical to
89 "mk_group_accessors" but accessors will throw an error if passed a value
90 rather than setting the value.
91
92 mk_group_wo_accessors
93 __PACKAGE__->mk_group_wo_accessors(simple => 'lie', [ subject => 'subj' ]);
94
95 Arguments: $group, @fieldspec
96 Returns: none
97
98 Creates a set of write only accessors in a given group. Identical to
99 "mk_group_accessors" but accessors will throw an error if not passed a
100 value rather than getting the value.
101
102 get_simple
103 Arguments: $field
104 Returns: $value
105
106 Simple getter for hash-based objects which returns the value for the
107 field name passed as an argument.
108
109 set_simple
110 Arguments: $field, $new_value
111 Returns: $new_value
112
113 Simple setter for hash-based objects which sets and then returns the
114 value for the field name passed as an argument.
115
116 get_inherited
117 Arguments: $field
118 Returns: $value
119
120 Simple getter for Classes and hash-based objects which returns the value
121 for the field name passed as an argument. This behaves much like
122 Class::Data::Accessor where the field can be set in a base class,
123 inherited and changed in subclasses, and inherited and changed for
124 object instances.
125
126 set_inherited
127 Arguments: $field, $new_value
128 Returns: $new_value
129
130 Simple setter for Classes and hash-based objects which sets and then
131 returns the value for the field name passed as an argument. When called
132 on a hash-based object it will set the appropriate hash key value. When
133 called on a class, it will set a class level variable.
134
135 Note:: This method will die if you try to set an object variable on a
136 non hash-based object.
137
138 get_component_class
139 Arguments: $field
140 Returns: $value
141
142 Gets the value of the specified component class.
143
144 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
145
146 $self->result_class->method();
147
148 ## same as
149 $self->get_component_class('result_class')->method();
150
151 set_component_class
152 Arguments: $field, $class
153 Returns: $new_value
154
155 Inherited accessor that automatically loads the specified class before
156 setting it. This method will die if the specified class could not be
157 loaded.
158
159 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
160 __PACKAGE__->result_class('MyClass');
161
162 $self->result_class->method();
163
164 INTERNAL METHODS
165 These methods are documented for clarity, but are never meant to be
166 called directly, and are not really meant for overriding either.
167
168 get_super_paths
169 Returns a list of 'parent' or 'super' class names that the current class
170 inherited from. This is what drives the traversal done by
171 "get_inherited".
172
173 make_group_accessor
174 __PACKAGE__->make_group_accessor('simple', 'hair_length', 'hair_length');
175 __PACKAGE__->make_group_accessor('simple', 'hc', 'hair_color');
176
177 Arguments: $group, $field, $accessor
178 Returns: \&accessor_coderef ?
179
180 Called by mk_group_accessors for each entry in @fieldspec. Either
181 returns a coderef which will be installed at "&__PACKAGE__::$accessor",
182 or returns "undef" if it elects to install the coderef on its own.
183
184 make_group_ro_accessor
185 __PACKAGE__->make_group_ro_accessor('simple', 'birthdate', 'birthdate');
186 __PACKAGE__->make_group_ro_accessor('simple', 'ssn', 'social_security_number');
187
188 Arguments: $group, $field, $accessor
189 Returns: \&accessor_coderef ?
190
191 Called by mk_group_ro_accessors for each entry in @fieldspec. Either
192 returns a coderef which will be installed at "&__PACKAGE__::$accessor",
193 or returns "undef" if it elects to install the coderef on its own.
194
195 make_group_wo_accessor
196 __PACKAGE__->make_group_wo_accessor('simple', 'lie', 'lie');
197 __PACKAGE__->make_group_wo_accessor('simple', 'subj', 'subject');
198
199 Arguments: $group, $field, $accessor
200 Returns: \&accessor_coderef ?
201
202 Called by mk_group_wo_accessors for each entry in @fieldspec. Either
203 returns a coderef which will be installed at "&__PACKAGE__::$accessor",
204 or returns "undef" if it elects to install the coderef on its own.
205
206 PERFORMANCE
207 To provide total flexibility Class::Accessor::Grouped calls methods
208 internally while performing get/set actions, which makes it noticeably
209 slower than similar modules. To compensate, this module will
210 automatically use the insanely fast Class::XSAccessor to generate the
211 "simple"-group accessors if this module is available on your system.
212
213 Benchmark
214 This is the benchmark of 200 get/get/set/get/set cycles on perl 5.16.2
215 with thread support, showcasing how this modules simple (CAG_S),
216 inherited (CAG_INH) and inherited with parent-class data (CAG_INHP)
217 accessors stack up against most popular accessor builders: Moose, Moo,
218 Mo, Mouse (both pure-perl and XS variant), Object::Tiny::RW (OTRW),
219 Class::Accessor (CA), Class::Accessor::Lite (CAL), Class::Accessor::Fast
220 (CAF), Class::Accessor::Fast::XS (CAF_XS) and Class::XSAccessor (XSA)
221
222 Rate CAG_INHP CAG_INH CA CAG_S CAF moOse OTRW CAL mo moUse HANDMADE moo CAF_XS moUse_XS XSA
223
224 CAG_INHP 287.021+-0.02/s -- -0.3% -10.0% -37.1% -53.1% -53.6% -53.7% -54.1% -56.9% -59.0% -59.6% -59.8% -78.7% -81.9% -83.5%
225
226 CAG_INH 288.025+-0.031/s 0.3% -- -9.7% -36.9% -52.9% -53.5% -53.5% -53.9% -56.7% -58.8% -59.5% -59.7% -78.6% -81.9% -83.5%
227
228 CA 318.967+-0.047/s 11.1% 10.7% -- -30.1% -47.9% -48.5% -48.5% -49.0% -52.1% -54.4% -55.1% -55.3% -76.3% -79.9% -81.7%
229
230 CAG_S 456.107+-0.054/s 58.9% 58.4% 43.0% -- -25.4% -26.3% -26.4% -27.0% -31.5% -34.8% -35.8% -36.1% -66.1% -71.3% -73.9%
231
232 CAF 611.745+-0.099/s 113.1% 112.4% 91.8% 34.1% -- -1.2% -1.2% -2.1% -8.1% -12.6% -14.0% -14.3% -54.5% -61.5% -64.9%
233
234 moOse 619.051+-0.059/s 115.7% 114.9% 94.1% 35.7% 1.2% -- -0.1% -1.0% -7.0% -11.6% -12.9% -13.3% -54.0% -61.0% -64.5%
235
236 OTRW 619.475+-0.1/s 115.8% 115.1% 94.2% 35.8% 1.3% 0.1% -- -0.9% -6.9% -11.5% -12.9% -13.2% -54.0% -61.0% -64.5%
237
238 CAL 625.106+-0.085/s 117.8% 117.0% 96.0% 37.1% 2.2% 1.0% 0.9% -- -6.1% -10.7% -12.1% -12.5% -53.5% -60.6% -64.2%
239
240 mo 665.44+-0.12/s 131.8% 131.0% 108.6% 45.9% 8.8% 7.5% 7.4% 6.5% -- -4.9% -6.4% -6.8% -50.5% -58.1% -61.9%
241
242 moUse 699.9+-0.15/s 143.9% 143.0% 119.4% 53.5% 14.4% 13.1% 13.0% 12.0% 5.2% -- -1.6% -2.0% -48.0% -55.9% -59.9%
243
244 HANDMADE 710.98+-0.16/s 147.7% 146.8% 122.9% 55.9% 16.2% 14.9% 14.8% 13.7% 6.8% 1.6% -- -0.4% -47.2% -55.2% -59.2%
245
246 moo 714.04+-0.13/s 148.8% 147.9% 123.9% 56.6% 16.7% 15.3% 15.3% 14.2% 7.3% 2.0% 0.4% -- -46.9% -55.0% -59.1%
247
248 CAF_XS 1345.55+-0.051/s 368.8% 367.2% 321.8% 195.0% 120.0% 117.4% 117.2% 115.3% 102.2% 92.2% 89.3% 88.4% -- -15.3% -22.9%
249
250 moUse_XS 1588+-0.036/s 453.3% 451.3% 397.9% 248.2% 159.6% 156.5% 156.3% 154.0% 138.6% 126.9% 123.4% 122.4% 18.0% -- -9.0%
251
252 XSA 1744.67+-0.052/s 507.9% 505.7% 447.0% 282.5% 185.2% 181.8% 181.6% 179.1% 162.2% 149.3% 145.4% 144.3% 29.7% 9.9% --
253
254 Benchmarking program is available in the root of the repository
255 <http://search.cpan.org/dist/Class-Accessor-Grouped/>:
256
257 Notes on Class::XSAccessor
258 You can force (or disable) the use of Class::XSAccessor before creating
259 a particular "simple" accessor by either manipulating the global
260 variable $Class::Accessor::Grouped::USE_XS to true or false (preferably
261 with localization, or you can do so before runtime via the "CAG_USE_XS"
262 environment variable.
263
264 Since Class::XSAccessor has no knowledge of "get_simple" and
265 "set_simple" this module does its best to detect if you are overriding
266 one of these methods and will fall back to using the perl version of the
267 accessor in order to maintain consistency. However be aware that if you
268 enable use of "Class::XSAccessor" (automatically or explicitly), create
269 an object, invoke a simple accessor on that object, and then manipulate
270 the symbol table to install a "get/set_simple" override - you get to
271 keep all the pieces.
272
273 AUTHORS
274 Matt S. Trout <[email protected]>
275
276 Christopher H. Laco <[email protected]>
277
278 CONTRIBUTORS
279 Caelum: Rafael Kitover <[email protected]>
280
281 frew: Arthur Axel "fREW" Schmidt <[email protected]>
282
283 groditi: Guillermo Roditi <[email protected]>
284
285 Jason Plum <[email protected]>
286
287 ribasushi: Peter Rabbitson <[email protected]>
288
289 COPYRIGHT & LICENSE
290 Copyright (c) 2006-2010 Matt S. Trout <[email protected]>
291
292 This program is free software; you can redistribute it and/or modify it
293 under the same terms as perl itself.
294