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