1MakeMethods::Composite:U:sGelrobCaoln(t3r)ibuted Perl DoMcaukmeeMnettahtoidosn::Composite::Global(3)
2
3
4
6 Class::MakeMethods::Composite::Global - Global data
7
9 package MyClass;
10 use Class::MakeMethods::Composite::Global (
11 scalar => [ 'foo' ],
12 array => [ 'my_list' ],
13 hash => [ 'my_index' ],
14 );
15 ...
16
17 MyClass->foo( 'Foozle' );
18 print MyClass->foo();
19
20 print MyClass->new(...)->foo(); # same value for any instance
21 print MySubclass->foo(); # ... and for any subclass
22
23 MyClass->my_list(0 => 'Foozle', 1 => 'Bang!');
24 print MyClass->my_list(1);
25
26 MyClass->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle');
27 print MyClass->my_index('foo');
28
30 The Composite::Global suclass of MakeMethods provides basic accessors
31 for shared data.
32
33 Class::MakeMethods Calling Interface
34
35 When you "use" this package, the method declarations you provide as
36 arguments cause subroutines to be generated and installed in your mod‐
37 ule.
38
39 You can also omit the arguments to "use" and instead make methods at
40 runtime by passing the declarations to a subsequent call to "make()".
41
42 You may include any number of declarations in each call to "use" or
43 "make()". If methods with the same name already exist, earlier calls to
44 "use" or "make()" win over later ones, but within each call, later dec‐
45 larations superceed earlier ones.
46
47 You can install methods in a different package by passing "-TargetClass
48 => package" as your first arguments to "use" or "make".
49
50 See Class::MakeMethods for more details.
51
52 Class::MakeMethods::Basic Declaration Syntax
53
54 The following types of Basic declarations are supported:
55
56 · generator_type => "method_name"
57
58 · generator_type => "name_1 name_2..."
59
60 · generator_type => [ "name_1", "name_2", ...]
61
62 See the "METHOD GENERATOR TYPES" section below for a list of the sup‐
63 ported values of generator_type.
64
65 For each method name you provide, a subroutine of the indicated type
66 will be generated and installed under that name in your module.
67
68 Method names should start with a letter, followed by zero or more let‐
69 ters, numbers, or underscores.
70
71 Class::MakeMethods::Composite Declaration Syntax
72
73 The Composite syntax also provides several ways to optionally associate
74 a hash of additional parameters with a given method name.
75
76 · generator_type => [ "name_1" => { param=>value... }, ... ]
77
78 A hash of parameters to use just for this method name.
79
80 (Note: to prevent confusion with self-contained definition hashes,
81 described below, parameter hashes following a method name must not
82 contain the key 'name'.)
83
84 · generator_type => [ [ "name_1", "name_2", ... ] => {
85 param=>value... } ]
86
87 Each of these method names gets a copy of the same set of parame‐
88 ters.
89
90 · generator_type => [ { "name"=>"name_1", param=>value... }, ... ]
91
92 By including the reserved parameter "name", you create a self-con‐
93 tained declaration with that name and any associated hash values.
94
95 Basic declarations, as described above, are given an empty parameter
96 hash.
97
99 scalar - Global Accessor
100
101 For each method name passed, uses a closure to generate a subroutine
102 with the following characteristics:
103
104 · May be called as a class method, or on any instance or subclass,
105 and behaves identically regardless of what it was called on.
106
107 · If called without any arguments returns the current value.
108
109 · If called with an argument, stores that as the value, and returns
110 it,
111
112 · If called with multiple arguments, stores a reference to a new
113 array with those arguments as contents, and returns that array ref‐
114 erence.
115
116 Sample declaration and usage:
117
118 package MyClass;
119 use Class::MakeMethods::Composite::Global (
120 scalar => 'foo',
121 );
122 ...
123
124 # Store value
125 MyClass->foo('Foozle');
126
127 # Retrieve value
128 print MyClass->foo;
129
130 array - Global Ref Accessor
131
132 For each method name passed, uses a closure to generate a subroutine
133 with the following characteristics:
134
135 · May be called as a class method, or on any instance or subclass,
136 and behaves identically regardless of what it was called on.
137
138 · The global value will be a reference to an array (or undef).
139
140 · If called without any arguments, returns the current array-ref
141 value (or undef).
142
143 · If called with a single non-ref argument, uses that argument as an
144 index to retrieve from the referenced array, and returns that value
145 (or undef).
146
147 · If called with a single array ref argument, uses that list to
148 return a slice of the referenced array.
149
150 · If called with a list of argument pairs, each with a non-ref index
151 and an associated value, stores the value at the given index in the
152 referenced array. If the global value was previously undefined, a
153 new array is autovivified. The current value in each position will
154 be overwritten, and later arguments with the same index will over‐
155 ride earlier ones. Returns the current array-ref value.
156
157 · If called with a list of argument pairs, each with the first item
158 being a reference to an array of up to two numbers, loops over each
159 pair and uses those numbers to splice the value array.
160
161 The first controlling number is the position at which the splice
162 will begin. Zero will start before the first item in the list. Neg‐
163 ative numbers count backwards from the end of the array.
164
165 The second number is the number of items to be removed from the
166 list. If it is omitted, or undefined, or zero, no items are
167 removed. If it is a positive integer, that many items will be
168 returned.
169
170 If both numbers are omitted, or are both undefined, they default to
171 containing the entire value array.
172
173 If the second argument is undef, no values will be inserted; if it
174 is a non-reference value, that one value will be inserted; if it is
175 an array-ref, its values will be copied.
176
177 The method returns the items that removed from the array, if any.
178
179 Sample declaration and usage:
180
181 package MyClass;
182 use Class::MakeMethods::Composite::Global (
183 array => 'bar',
184 );
185 ...
186
187 # Clear and set contents of list
188 print MyClass->bar([ 'Spume', 'Frost' ] );
189
190 # Set values by position
191 MyClass->bar(0 => 'Foozle', 1 => 'Bang!');
192
193 # Positions may be overwritten, and in any order
194 MyClass->bar(2 => 'And Mash', 1 => 'Blah!');
195
196 # Retrieve value by position
197 print MyClass->bar(1);
198
199 # Direct access to referenced array
200 print scalar @{ MyClass->bar() };
201
202 There are also calling conventions for slice and splice operations:
203
204 # Retrieve slice of values by position
205 print join(', ', MyClass->bar( undef, [0, 2] ) );
206
207 # Insert an item at position in the array
208 MyClass->bar([3], 'Potatoes' );
209
210 # Remove 1 item from position 3 in the array
211 MyClass->bar([3, 1], undef );
212
213 # Set a new value at position 2, and return the old value
214 print MyClass->bar([2, 1], 'Froth' );
215
216 hash - Global Ref Accessor
217
218 For each method name passed, uses a closure to generate a subroutine
219 with the following characteristics:
220
221 · May be called as a class method, or on any instance or subclass,
222 and behaves identically regardless of what it was called on.
223
224 · The global value will be a reference to a hash (or undef).
225
226 · If called without any arguments, returns the contents of the hash
227 in list context, or a hash reference in scalar context (or undef).
228
229 · If called with one non-ref argument, uses that argument as an index
230 to retrieve from the referenced hash, and returns that value (or
231 undef).
232
233 · If called with one array-ref argument, uses the contents of that
234 array to retrieve a slice of the referenced hash.
235
236 · If called with one hash-ref argument, sets the contents of the ref‐
237 erenced hash to match that provided.
238
239 · If called with a list of key-value pairs, stores the value under
240 the given key in the referenced hash. If the global value was pre‐
241 viously undefined, a new hash is autovivified. The current value
242 under each key will be overwritten, and later arguments with the
243 same key will override earlier ones. Returns the contents of the
244 hash in list context, or a hash reference in scalar context.
245
246 Sample declaration and usage:
247
248 package MyClass;
249 use Class::MakeMethods::Composite::Global (
250 hash => 'baz',
251 );
252 ...
253
254 # Set values by key
255 MyClass->baz('foo' => 'Foozle', 'bar' => 'Bang!');
256
257 # Values may be overwritten, and in any order
258 MyClass->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle');
259
260 # Retrieve value by key
261 print MyClass->baz('foo');
262
263 # Retrive slice of values by position
264 print join(', ', MyClass->baz( ['foo', 'bar'] ) );
265
266 # Direct access to referenced hash
267 print keys %{ MyClass->baz() };
268
269 # Reset the hash contents to empty
270 @{ MyClass->baz() } = ();
271
272 object - Global Ref Accessor
273
274 For each method name passed, uses a closure to generate a subroutine
275 with the following characteristics:
276
277 · May be called as a class method, or on any instance or subclass,
278 and behaves identically regardless of what it was called on.
279
280 · The global value will be a reference to an object (or undef).
281
282 · If called without any arguments returns the current value.
283
284 · If called with an argument, stores that as the value, and returns
285 it,
286
287 Sample declaration and usage:
288
289 package MyClass;
290 use Class::MakeMethods::Composite::Global (
291 object => 'foo',
292 );
293 ...
294
295 # Store value
296 MyClass->foo( Foozle->new() );
297
298 # Retrieve value
299 print MyClass->foo;
300
302 See Class::MakeMethods for general information about this distribution.
303
304 See Class::MakeMethods::Composite for more about this family of sub‐
305 classes.
306
307
308
309perl v5.8.8 2004-09-06 MakeMethods::Composite::Global(3)