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