1MakeMethods::Standard::UGsleorbaClo(n3t)ributed Perl DocMuamkeenMteatthioodns::Standard::Global(3)
2
3
4
6 Class::MakeMethods::Standard::Global - Global data
7
9 package MyClass;
10 use Class::MakeMethods::Standard::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 Standard::Global suclass of MakeMethods provides basic accessors
31 for shared data.
32
33 Calling Conventions
34
35 When you "use" this package, the method names you provide as arguments
36 cause subroutines to be generated and installed in your module.
37
38 See "Calling Conventions" in Class::MakeMethods::Standard for more
39 information.
40
41 Declaration Syntax
42
43 To declare methods, pass in pairs of a method-type name followed by one
44 or more method names.
45
46 Valid method-type names for this package are listed in "METHOD GENERA‐
47 TOR TYPES".
48
49 See "Declaration Syntax" in Class::MakeMethods::Standard and "Parameter
50 Syntax" in Class::MakeMethods::Standard for more information.
51
53 scalar - Global Accessor
54
55 For each method name passed, uses a closure to generate a subroutine
56 with the following characteristics:
57
58 · May be called as a class method, or on any instance or subclass,
59 and behaves identically regardless of what it was called on.
60
61 · If called without any arguments returns the current value.
62
63 · If called with an argument, stores that as the value, and returns
64 it,
65
66 Sample declaration and usage:
67
68 package MyClass;
69 use Class::MakeMethods::Standard::Global (
70 scalar => 'foo',
71 );
72 ...
73
74 # Store value
75 MyClass->foo('Foozle');
76
77 # Retrieve value
78 print MyClass->foo;
79
80 array - Global Ref Accessor
81
82 For each method name passed, uses a closure to generate a subroutine
83 with the following characteristics:
84
85 · May be called as a class method, or on any instance or subclass,
86 and behaves identically regardless of what it was called on.
87
88 · The global value will be a reference to an array (or undef).
89
90 · If called without any arguments, returns the current array-ref
91 value (or undef).
92
93 · If called with a single non-ref argument, uses that argument as an
94 index to retrieve from the referenced array, and returns that value
95 (or undef).
96
97 · If called with a single array ref argument, uses that list to
98 return a slice of the referenced array.
99
100 · If called with a list of argument pairs, each with a non-ref index
101 and an associated value, stores the value at the given index in the
102 referenced array. If the global value was previously undefined, a
103 new array is autovivified. The current value in each position will
104 be overwritten, and later arguments with the same index will over‐
105 ride earlier ones. Returns the current array-ref value.
106
107 · If called with a list of argument pairs, each with the first item
108 being a reference to an array of up to two numbers, loops over each
109 pair and uses those numbers to splice the value array.
110
111 The first controlling number is the position at which the splice
112 will begin. Zero will start before the first item in the list. Neg‐
113 ative numbers count backwards from the end of the array.
114
115 The second number is the number of items to be removed from the
116 list. If it is omitted, or undefined, or zero, no items are
117 removed. If it is a positive integer, that many items will be
118 returned.
119
120 If both numbers are omitted, or are both undefined, they default to
121 containing the entire value array.
122
123 If the second argument is undef, no values will be inserted; if it
124 is a non-reference value, that one value will be inserted; if it is
125 an array-ref, its values will be copied.
126
127 The method returns the items that removed from the array, if any.
128
129 Sample declaration and usage:
130
131 package MyClass;
132 use Class::MakeMethods::Standard::Global (
133 array => 'bar',
134 );
135 ...
136
137 # Clear and set contents of list
138 print MyClass->bar([ 'Spume', 'Frost' ] );
139
140 # Set values by position
141 MyClass->bar(0 => 'Foozle', 1 => 'Bang!');
142
143 # Positions may be overwritten, and in any order
144 MyClass->bar(2 => 'And Mash', 1 => 'Blah!');
145
146 # Retrieve value by position
147 print MyClass->bar(1);
148
149 # Direct access to referenced array
150 print scalar @{ MyClass->bar() };
151
152 There are also calling conventions for slice and splice operations:
153
154 # Retrieve slice of values by position
155 print join(', ', MyClass->bar( undef, [0, 2] ) );
156
157 # Insert an item at position in the array
158 MyClass->bar([3], 'Potatoes' );
159
160 # Remove 1 item from position 3 in the array
161 MyClass->bar([3, 1], undef );
162
163 # Set a new value at position 2, and return the old value
164 print MyClass->bar([2, 1], 'Froth' );
165
166 hash - Global Ref Accessor
167
168 For each method name passed, uses a closure to generate a subroutine
169 with the following characteristics:
170
171 · May be called as a class method, or on any instance or subclass,
172 and behaves identically regardless of what it was called on.
173
174 · The global value will be a reference to a hash (or undef).
175
176 · If called without any arguments, returns the contents of the hash
177 in list context, or a hash reference in scalar context (or undef).
178
179 · If called with one non-ref argument, uses that argument as an index
180 to retrieve from the referenced hash, and returns that value (or
181 undef).
182
183 · If called with one array-ref argument, uses the contents of that
184 array to retrieve a slice of the referenced hash.
185
186 · If called with one hash-ref argument, sets the contents of the ref‐
187 erenced hash to match that provided.
188
189 · If called with a list of key-value pairs, stores the value under
190 the given key in the referenced hash. If the global value was pre‐
191 viously undefined, a new hash is autovivified. The current value
192 under each key will be overwritten, and later arguments with the
193 same key will override earlier ones. Returns the contents of the
194 hash in list context, or a hash reference in scalar context.
195
196 Sample declaration and usage:
197
198 package MyClass;
199 use Class::MakeMethods::Standard::Global (
200 hash => 'baz',
201 );
202 ...
203
204 # Set values by key
205 MyClass->baz('foo' => 'Foozle', 'bar' => 'Bang!');
206
207 # Values may be overwritten, and in any order
208 MyClass->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle');
209
210 # Retrieve value by key
211 print MyClass->baz('foo');
212
213 # Retrive slice of values by position
214 print join(', ', MyClass->baz( ['foo', 'bar'] ) );
215
216 # Direct access to referenced hash
217 print keys %{ MyClass->baz() };
218
219 # Reset the hash contents to empty
220 @{ MyClass->baz() } = ();
221
222 object - Global Ref Accessor
223
224 For each method name passed, uses a closure to generate a subroutine
225 with the following characteristics:
226
227 · May be called as a class method, or on any instance or subclass,
228 and behaves identically regardless of what it was called on.
229
230 · The global value will be a reference to an object (or undef).
231
232 · If called without any arguments returns the current value.
233
234 · If called with an argument, stores that as the value, and returns
235 it,
236
237 Sample declaration and usage:
238
239 package MyClass;
240 use Class::MakeMethods::Standard::Global (
241 object => 'foo',
242 );
243 ...
244
245 # Store value
246 MyClass->foo( Foozle->new() );
247
248 # Retrieve value
249 print MyClass->foo;
250
252 See Class::MakeMethods for general information about this distribution.
253
254 See Class::MakeMethods::Standard for more about this family of sub‐
255 classes.
256
257
258
259perl v5.8.8 2004-09-06 MakeMethods::Standard::Global(3)