1MakeMethods::Basic::GloUbsaelr(3C)ontributed Perl DocumeMnatkaetMieotnhods::Basic::Global(3)
2
3
4
6 Class::MakeMethods::Basic::Global - Basic shared methods
7
9 package MyObject;
10 use Class::MakeMethods::Basic::Global (
11 scalar => [ 'foo', 'bar' ],
12 array => 'my_list',
13 hash => 'my_index',
14 );
15 ....
16
17 # Store and retrieve global values
18 MyObject->foo('Foobar');
19 print MyObject->foo();
20
21 # All instances of your class access the same values
22 $my_object->bar('Barbados');
23 print $other_one->bar();
24
25 # Array accessor
26 MyObject->my_list(0 => 'Foozle', 1 => 'Bang!');
27 print MyObject->my_list(1);
28
29 # Hash accessor
30 MyObject->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle');
31 print MyObject->my_index('foo');
32
34 The Basic::Global subclass of MakeMethods provides basic accessors for
35 data shared by an entire class, sometimes called "static" or "class
36 data."
37
38 Calling Conventions
39 When you "use" this package, the method names you provide as arguments
40 cause subroutines to be generated and installed in your module.
41
42 See "Calling Conventions" in Class::MakeMethods::Basic for a summary,
43 or "USAGE" in Class::MakeMethods for full details.
44
45 Declaration Syntax
46 To declare methods, pass in pairs of a method-type name followed by one
47 or more method names. Valid method-type names for this package are
48 listed in "METHOD GENERATOR TYPES".
49
50 See "Declaration Syntax" in Class::MakeMethods::Basic for more syntax
51 information.
52
54 scalar - Shared Accessor
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 equivalently, on any object
59 instance.
60
61 • Stores a global value accessible only through this method.
62
63 • If called without any arguments returns the current value.
64
65 • If called with an argument, stores that as the value, and returns
66 it,
67
68 Sample declaration and usage:
69
70 package MyObject;
71 use Class::MakeMethods::Basic::Hash (
72 scalar => 'foo',
73 );
74 ...
75
76 # Store value
77 MyObject->foo('Foozle');
78
79 # Retrieve value
80 print MyObject->foo;
81
82 array - Shared Ref Accessor
83 For each method name passed, uses a closure to generate a subroutine
84 with the following characteristics:
85
86 • May be called as a class method, or equivalently, on any object
87 instance.
88
89 • Stores a global value accessible only through this method.
90
91 • The value will be a reference to an array (or undef).
92
93 • If called without any arguments, returns the current array-ref
94 value (or undef).
95
96 • If called with one argument, uses that argument as an index to
97 retrieve from the referenced array, and returns that value (or
98 undef). If the single argument is an array ref, then a slice of the
99 referenced array is returned.
100
101 • If called with a list of index-value pairs, stores the value at the
102 given index in the referenced array. If the value was previously
103 undefined, a new array is autovivified. The current value in each
104 position will be overwritten, and later arguments with the same
105 index will override earlier ones. Returns the current array-ref
106 value.
107
108 Sample declaration and usage:
109
110 package MyObject;
111 use Class::MakeMethods::Basic::Hash (
112 array => 'bar',
113 );
114 ...
115
116 # Set values by position
117 $obj->bar(0 => 'Foozle', 1 => 'Bang!');
118
119 # Positions may be overwritten, and in any order
120 $obj->bar(2 => 'And Mash', 1 => 'Blah!');
121
122 # Retrieve value by position
123 print $obj->bar(1);
124
125 # Retrieve slice of values by position
126 print join(', ', $obj->bar( [0, 2] ) );
127
128 # Direct access to referenced array
129 print scalar @{ $obj->bar() };
130
131 # Reset the array contents to empty
132 @{ $obj->bar() } = ();
133
134 hash - Shared Ref Accessor
135 For each method name passed, uses a closure to generate a subroutine
136 with the following characteristics:
137
138 • May be called as a class method, or equivalently, on any object
139 instance.
140
141 • Stores a global value accessible only through this method.
142
143 • The value will be a reference to a hash (or undef).
144
145 • If called without any arguments, returns the current hash-ref value
146 (or undef).
147
148 • If called with one argument, uses that argument as an index to
149 retrieve from the referenced hash, and returns that value (or
150 undef). If the single argument is an array ref, then a slice of the
151 referenced hash is returned.
152
153 • If called with a list of key-value pairs, stores the value under
154 the given key in the referenced hash. If the value was previously
155 undefined, a new hash is autovivified. The current value under each
156 key will be overwritten, and later arguments with the same key will
157 override earlier ones. Returns the current hash-ref value.
158
159 Sample declaration and usage:
160
161 package MyObject;
162 use Class::MakeMethods::Basic::Hash (
163 hash => 'baz',
164 );
165 ...
166
167 # Set values by key
168 $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!');
169
170 # Values may be overwritten, and in any order
171 $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle');
172
173 # Retrieve value by key
174 print $obj->baz('foo');
175
176 # Retrieve slice of values by position
177 print join(', ', $obj->baz( ['foo', 'bar'] ) );
178
179 # Direct access to referenced hash
180 print keys %{ $obj->baz() };
181
182 # Reset the hash contents to empty
183 @{ $obj->baz() } = ();
184
186 See Class::MakeMethods for general information about this distribution.
187
188 See Class::MakeMethods::Basic for more about this family of subclasses.
189
190
191
192perl v5.38.0 2023-07-20 MakeMethods::Basic::Global(3)