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