1Class::Accessor::ClassyU(s3e)r Contributed Perl DocumentaCtliaosns::Accessor::Classy(3)
2
3
4
6 Class::Accessor::Classy - accessors with minimal inheritance
7
9 package YourPackage;
10
11 use Class::Accessor::Classy;
12 with qw(new); # with a new() method
13 ro qw(foo); # read-only
14 rw qw(bar); # read-write
15 rs baz => \ (my $set_baz); # read-only, plus a secret writer
16
17 # alternatively:
18 my $set_bip = rs 'bip';
19
20 ro_c suitcase => 'red'; # read-only class data
21 rw_c hat => 'black'; # read-write class data
22 rs_c socks => \ (my $set_socks) => undef;
23
24 # alternative secret writer syntax
25 my $set_shoes = rs_c shoes => undef;
26
27 # also class read-only:
28 constant seven => 7;
29 constant eight => this->seven + 1;
30 no Class::Accessor::Classy;
31 # ^-- removes all of the syntax bits from your namespace
32
33 package whatever;
34
35 YourPackage->set_hat(undef);
36 my $obj = YourPackage->new(foo => 4, bar => 2);
37 # NOTE I'm thinking of deprecating the get_foo() usage
38 warn "foo ", $obj->foo;
39 YourPackage->$set_socks("tube");
40
42 This module provides an extremely small-footprint accessor/mutator
43 declaration scheme for fast and convenient object attribute setup. It
44 is intended as a simple and speedy mechanism for preventing hash-key
45 typos rather than a full-blown object system with type checking and so
46 on.
47
48 The accessor methods appear as a hidden parent class of your package
49 and generally try to stay out of the way. The accessors and mutators
50 generated are of the form "foo()" and "set_foo()", respectively.
51
53 Unlike other class-modifying code, this is not designed to be inherited
54 from. Instead, you simply use it and get an invisible subclass
55 containing your accessors. If you use the 'no' syntax (to call
56 unimport), you are left with a squeaky-clean namespace.
57
58 After 'use' and before 'no', the following pieces of syntax are
59 available.
60
61 with
62 Add a 'standard' method to your class.
63
64 new
65
66 ro
67 Read-only properties (accessors only.)
68
69 ro qw(foo bar baz);
70
71 rw
72 Define read-write (accessor + mutator) properties.
73
74 rw qw(foo bar baz);
75
76 lv
77 Properties with lvalue accessors.
78
79 lv qw(thing deal stuff);
80
81 ri
82 Immutable properties. Once set, further calls to the mutator throw
83 errors.
84
85 ri qw(foo bar baz);
86
87 rs
88 Read-only properties with a secret mutator.
89
90 rs foo => \(my $set_foo);
91
92 lo
93 Read-only list properties. These are stored as an array-ref, but the
94 accessor returns a list.
95
96 lo qw(foo bar baz);
97
98 lw
99 Read-write list properties. The mutator takes a list.
100
101 lw 'foo';
102
103 This defaults to create foo()|get_foo(), set_foo(), and add_foo()
104 methods. Other features are possible here, but currently experimental.
105
106 ls
107 List property with a secret mutator.
108
109 ls foo => \(my $set_foo);
110
111 this
112 A shortcut for your classname. Useful for e.g. defining one constant
113 in terms of another.
114
115 this->some_class_method;
116
117 getter
118 Define a custom getter.
119
120 setter
121 Define a custom setter.
122
123 constant
124 A class constant.
125
126 constant foo => 7;
127
128 ro_c
129 Read-only class method.
130
131 rw_c
132 A read-write class method, with a default.
133
134 rw_c foo => 9;
135
136 rs_c
137 A class method with a secret setter.
138
139 rs_c bar => \(my $set_bar) => 12;
140
141 in
142 Specify the destination package. You need to set this before defining
143 anything else (but it is usually best to just not set it.)
144
145 in 'why_be_so_secretive';
146
147 aka
148 Add an alias for an existing method.
149
150 aka have_method => 'want_method', 'and_also_want';
151
153 This introspection stuff is unreliable -- don't use it.
154
155 find_accessors
156 @attribs = Class::Accessor::Classy->find_accessors($class);
157
158 find_subclasses
159 @classlist = Class::Accessor::Classy->find_subclasses($class);
160
162 Customized subclasses may override these methods to create a new kind
163 of accessor generator.
164
165 NOTE
166 You do not subclass Class::Accessor::Classy to construct your
167 objects.
168
169 If you are just creating MyObject, you are not inheriting any of
170 these methods.
171
172 The rest of this documentation only pertains to you if you are
173 trying to create something like Class::Accessor::Classy::MyWay.
174
175 notation:
176 Read these as: $CAC = 'Class::Accessor::Classy'; (or whatever
177 subclass you're creating.)
178
179 exports
180 my %exports = $CAC->exports;
181
182 import
183 $CAC->import;
184
185 unimport
186 $CAC->unimport;
187
188 create_package
189 Creates and returns the package in which the accessors will live. Also
190 pushes the created accessor package into the caller's @ISA.
191
192 If it already exists, simply returns the cached value.
193
194 my $package = $CAC->create_package(
195 class => $caller,
196 in => $package, # optional
197 );
198
199 install_sub
200 $CAC->install_sub($class, $name, $subref, $note);
201
202 annotate
203 $CAC->annotate($class, $name, $note);
204
205 get_notes
206 my %notes = $CAC->get_notes;
207
208 make_standards
209 $CAC->make_standards($class, @list);
210
211 _getter
212 Returns a compiled getter subref corresponding to whether or not the
213 class has a '--get' method.
214
215 $CAC->_getter($class, $item);
216
217 make_getters
218 $CAC->make_getters($class, @list);
219
220 make_lv_getters
221 $CAC->make_lv_getters($class, @list);
222
223 _setter
224 Returns a compiled setter subref corresponding to whether or not the
225 class has a '--set' method.
226
227 $CAC->_setter($class, $item);
228
229 make_setters
230 $CAC->make_setters($class, @list);
231
232 make_immutable
233 Creates immutable (one-time-only) setters.
234
235 CAC->make_immutable($class, @list);
236
237 make_secrets
238 my @names = $CAC->make_secrets($class, @list);
239
240 make_aliases
241 Constructs 'get_' aliases for a @list of accessors.
242
243 $CAC->make_aliases($class, @list);
244
245 make_aka
246 Create a list of alias methods which runtime refer to $realname.
247
248 $CAC->make_aka($where, $realname, @aliases);
249
250 do_eval
251 my $subref = $package->do_eval($string, @checks);
252
254 make_array_method
255 $CAC->make_array_method(
256 class => $class,
257 item => $name,
258 functions => [@functions],
259 secret => $bool,
260 );
261
262 If secret is true, will return the list of names.
263
264 _get_array_subs
265 my %subs = $CAC->_get_array_subs($name);
266
268 make_class_data
269 $CAC->make_class_data($mode, $class, $key, $value);
270
271 If mode is 'rs', returns the secret setter name.
272
274 Eric Wilhelm @ <ewilhelm at cpan dot org>
275
276 http://scratchcomputing.com/
277
279 If you found this module on CPAN, please report any bugs or feature
280 requests through the web interface at <http://rt.cpan.org>. I will be
281 notified, and then you'll automatically be notified of progress on your
282 bug as I make changes.
283
284 If you pulled this development version from my /svn/, please contact me
285 directly.
286
288 Copyright (C) 2006-2007 Eric L. Wilhelm, All Rights Reserved.
289
291 Absolutely, positively NO WARRANTY, neither express or implied, is
292 offered with this software. You use this software at your own risk.
293 In case of loss, no person or entity owes you anything whatseover. You
294 have been warned.
295
297 This program is free software; you can redistribute it and/or modify it
298 under the same terms as Perl itself.
299
300
301
302perl v5.32.1 2021-01-26 Class::Accessor::Classy(3)