blib/lib/Class/MakeMethods/Composite/Global.pm untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 279 CC 1 SCAR 6.9
CC
1
Cov
0%
scalar
2.0
array
2.0
hash
2.0
CRAP 2.0
1 =head1 NAME
2  
3 Class::MakeMethods::Composite::Global - Global data
4  
5 =head1 SYNOPSIS
6  
7 package MyClass;
8 use Class::MakeMethods::Composite::Global (
9 scalar => [ 'foo' ],
10 array => [ 'my_list' ],
11 hash => [ 'my_index' ],
12 );
13 ...
14
15 MyClass->foo( 'Foozle' );
16 print MyClass->foo();
17  
18 print MyClass->new(...)->foo(); # same value for any instance
19 print MySubclass->foo(); # ... and for any subclass
20
21 MyClass->my_list(0 => 'Foozle', 1 => 'Bang!');
22 print MyClass->my_list(1);
23
24 MyClass->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle');
25 print MyClass->my_index('foo');
26  
27  
28 =head1 DESCRIPTION
29  
30 The Composite::Global suclass of MakeMethods provides basic accessors for shared data.
31  
32 =head2 Class::MakeMethods Calling Interface
33  
34 When you C<use> this package, the method declarations you provide
35 as arguments cause subroutines to be generated and installed in
36 your module.
37  
38 You can also omit the arguments to C<use> and instead make methods
39 at runtime by passing the declarations to a subsequent call to
40 C<make()>.
41  
42 You may include any number of declarations in each call to C<use>
43 or C<make()>. If methods with the same name already exist, earlier
44 calls to C<use> or C<make()> win over later ones, but within each
45 call, later declarations superceed earlier ones.
46  
47 You can install methods in a different package by passing C<-TargetClass =E<gt> I<package>> as your first arguments to C<use> or C<make>.
48  
49 See L<Class::MakeMethods> for more details.
50  
51 =head2 Class::MakeMethods::Basic Declaration Syntax
52  
53 The following types of Basic declarations are supported:
54  
55 =over 4
56  
57 =item *
58  
59 I<generator_type> => "I<method_name>"
60  
61 =item *
62  
63 I<generator_type> => "I<name_1> I<name_2>..."
64  
65 =item *
66  
67 I<generator_type> => [ "I<name_1>", "I<name_2>", ...]
68  
69 =back
70  
71 See the "METHOD GENERATOR TYPES" section below for a list of the supported values of I<generator_type>.
72  
73 For each method name you provide, a subroutine of the indicated
74 type will be generated and installed under that name in your module.
75  
76 Method names should start with a letter, followed by zero or more
77 letters, numbers, or underscores.
78  
79 =head2 Class::MakeMethods::Composite Declaration Syntax
80  
81 The Composite syntax also provides several ways to optionally
82 associate a hash of additional parameters with a given method
83 name.
84  
85 =over 4
86  
87 =item *
88  
89 I<generator_type> => [ "I<name_1>" => { I<param>=>I<value>... }, ... ]
90  
91 A hash of parameters to use just for this method name.
92  
93 (Note: to prevent confusion with self-contained definition hashes,
94 described below, parameter hashes following a method name must not
95 contain the key 'name'.)
96  
97 =item *
98  
99 I<generator_type> => [ [ "I<name_1>", "I<name_2>", ... ] => { I<param>=>I<value>... } ]
100  
101 Each of these method names gets a copy of the same set of parameters.
102  
103 =item *
104  
105 I<generator_type> => [ { "name"=>"I<name_1>", I<param>=>I<value>... }, ... ]
106  
107 By including the reserved parameter C<name>, you create a self-contained declaration with that name and any associated hash values.
108  
109 =back
110  
111 Basic declarations, as described above, are given an empty parameter hash.
112  
113 =cut
114  
115 package Class::MakeMethods::Composite::Global;
116  
117 $VERSION = 1.000;
118 use strict;
119 use Class::MakeMethods::Composite '-isasubclass';
120  
121 ########################################################################
122  
123 =head1 METHOD GENERATOR TYPES
124  
125 =head2 scalar - Global Accessor
126  
127 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
128  
129 =over 4
130  
131 =item *
132  
133 May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on.
134  
135 =item *
136  
137 If called without any arguments returns the current value.
138  
139 =item *
140  
141 If called with an argument, stores that as the value, and returns it,
142  
143 =item *
144  
145 If called with multiple arguments, stores a reference to a new array with those arguments as contents, and returns that array reference.
146  
147 =back
148  
149 Sample declaration and usage:
150  
151 package MyClass;
152 use Class::MakeMethods::Composite::Global (
153 scalar => 'foo',
154 );
155 ...
156
157 # Store value
158 MyClass->foo('Foozle');
159
160 # Retrieve value
161 print MyClass->foo;
162  
163 =cut
164  
165 use vars qw( %ScalarFragments );
166  
167 sub scalar {
168 (shift)->_build_composite( \%ScalarFragments, @_ );
169 }
170  
171 %ScalarFragments = (
172 '' => [
173 '+init' => sub {
174 my ($method) = @_;
175 $method->{target_class} ||= $Class::MethodMaker::CONTEXT{TargetClass};
176 $method->{array_index} ||=
177 _array_index( $method->{target_class}, $name );
178 },
179 'do' => sub {
180 my $method = pop @_;
181 my $self = shift @_;
182 if ( scalar(@_) == 0 ) {
183 $method->{global_data};
184 } elsif ( scalar(@_) == 1 ) {
185 $method->{global_data} = shift;
186 } else {
187 $method->{global_data} = [@_];
188 }
189 },
190 ],
191 'rw' => [],
192 'p' => [
193 '+pre' => sub {
194 my $method = pop @_;
195 unless ( UNIVERSAL::isa((caller(1))[0], $method->{target_class}) ) {
196 croak "Method $method->{name} is protected";
197 }
198 },
199 ],
200 'pp' => [
201 '+pre' => sub {
202 my $method = pop @_;
203 unless ( (caller(1))[0] eq $method->{target_class} ) {
204 croak "Method $method->{name} is private";
205 }
206 },
207 ],
208 'pw' => [
209 '+pre' => sub {
210 my $method = pop @_;
211 my $self = shift @_;
212 my $args = \@_;
213 unless ( @$args == 0 or UNIVERSAL::isa((caller(1))[0], $method->{target_class}) ) {
214 croak "Method $method->{name} is write-protected";
215 }
216 },
217 ],
218 'ppw' => [
219 '+pre' => sub {
220 my $method = pop @_;
221 my $self = shift @_;
222 my $args = \@_;
223 unless ( @$args == 0 or (caller(1))[0] eq $method->{target_class} ) {
224 croak "Method $method->{name} is write-private";
225 }
226 },
227 ],
228 'r' => [
229 '+pre' => sub {
230 my $method = pop @_;
231 my $self = shift @_;
232 @{$method->{args}} = ($self) if ( scalar @_ );
233 },
234 ],
235 'ro' => [
236 '+pre' => sub {
237 my $method = pop @_;
238 my $self = shift @_;
239 my $args = \@_;
240 unless ( @$args == 0 ) {
241 croak("Method $method->{name} is read-only");
242 }
243 },
244 ],
245 'wo' => [
246 '+pre' => sub {
247 my $method = pop @_;
248 my $self = shift @_;
249 my $args = \@_;
250 if ( @$args == 0 ) {
251 croak("Method $method->{name} is write-only");
252 }
253 },
254 ],
255 'return_original' => [
256 '+pre' => sub {
257 my $method = pop @_;
258 my $self = shift @_;
259 my $args = \@_;
260 $method->{scratch}{return_original} = $method->{global_data};
261 },
262 '+post' => sub {
263 my $method = pop @_;
264 my $self = shift @_;
265 my $args = \@_;
266 $method->{result} = \{ $method->{scratch}{return_original} };
267 },
268 ],
269 );
270  
271 ########################################################################
272  
273 =head2 array - Global Ref Accessor
274  
275 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
276  
277 =over 4
278  
279 =item *
280  
281 May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on.
282  
283 =item *
284  
285 The global value will be a reference to an array (or undef).
286  
287 =item *
288  
289 If called without any arguments, returns the current array-ref value (or undef).
290  
291  
292 =item *
293  
294 If called with a single non-ref argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef).
295  
296 =item *
297  
298 If called with a single array ref argument, uses that list to return a slice of the referenced array.
299  
300 =item *
301  
302 If called with a list of argument pairs, each with a non-ref index and an associated value, stores the value at the given index in the referenced array. If the global value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value.
303  
304 =item *
305  
306 If called with a list of argument pairs, each with the first item being a reference to an array of up to two numbers, loops over each pair and uses those numbers to splice the value array.
307  
308 The first controlling number is the position at which the splice will begin. Zero will start before the first item in the list. Negative numbers count backwards from the end of the array.
309  
310 The second number is the number of items to be removed from the list. If it is omitted, or undefined, or zero, no items are removed. If it is a positive integer, that many items will be returned.
311  
312 If both numbers are omitted, or are both undefined, they default to containing the entire value array.
313  
314 If the second argument is undef, no values will be inserted; if it is a non-reference value, that one value will be inserted; if it is an array-ref, its values will be copied.
315  
316 The method returns the items that removed from the array, if any.
317  
318 =back
319  
320 Sample declaration and usage:
321
322 package MyClass;
323 use Class::MakeMethods::Composite::Global (
324 array => 'bar',
325 );
326 ...
327
328 # Clear and set contents of list
329 print MyClass->bar([ 'Spume', 'Frost' ] );
330
331 # Set values by position
332 MyClass->bar(0 => 'Foozle', 1 => 'Bang!');
333
334 # Positions may be overwritten, and in any order
335 MyClass->bar(2 => 'And Mash', 1 => 'Blah!');
336
337 # Retrieve value by position
338 print MyClass->bar(1);
339
340 # Direct access to referenced array
341 print scalar @{ MyClass->bar() };
342  
343 There are also calling conventions for slice and splice operations:
344  
345 # Retrieve slice of values by position
346 print join(', ', MyClass->bar( undef, [0, 2] ) );
347
348 # Insert an item at position in the array
349 MyClass->bar([3], 'Potatoes' );
350
351 # Remove 1 item from position 3 in the array
352 MyClass->bar([3, 1], undef );
353
354 # Set a new value at position 2, and return the old value
355 print MyClass->bar([2, 1], 'Froth' );
356  
357 =cut
358  
359  
360 use vars qw( %ArrayFragments );
361  
362 sub array {
363 (shift)->_build_composite( \%ArrayFragments, @_ );
364 }
365  
366 %ArrayFragments = (
367 '' => [
368 '+init' => sub {
369 my ($method) = @_;
370 $method->{hash_key} ||= $_->{name};
371 },
372 'do' => sub {
373 my $method = pop @_;
374 my $self = shift @_;
375 my $args = \@_;
376 if ( scalar(@$args) == 0 ) {
377 if ( $method->{auto_init} and
378 ! defined $method->{global_data} ) {
379 $method->{global_data} = [];
380 }
381 wantarray ? @{ $method->{global_data} } : $method->{global_data}
382 } elsif ( scalar(@_) == 1 and ref $_[0] eq 'ARRAY' ) {
383 $method->{global_data} = [ @{ $_[0] } ];
384 wantarray ? @{ $method->{global_data} } : $method->{global_data}
385 } else {
386 $method->{global_data} ||= [];
387 Class::MakeMethods::Composite::__array_ops(
388 $method->{global_data}, @$args );
389 }
390 },
391 ],
392 );
393  
394 ########################################################################
395  
396 =head2 hash - Global Ref Accessor
397  
398 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
399  
400 =over 4
401  
402 =item *
403  
404 May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on.
405  
406 =item *
407  
408 The global value will be a reference to a hash (or undef).
409  
410 =item *
411  
412 If called without any arguments, returns the contents of the hash in list context, or a hash reference in scalar context (or undef).
413  
414 =item *
415  
416 If called with one non-ref argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef).
417  
418 =item *
419  
420 If called with one array-ref argument, uses the contents of that array to retrieve a slice of the referenced hash.
421  
422 =item *
423  
424 If called with one hash-ref argument, sets the contents of the referenced hash to match that provided.
425  
426 =item *
427  
428 If called with a list of key-value pairs, stores the value under the given key in the referenced hash. If the global value was previously undefined, a new hash is autovivified. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the contents of the hash in list context, or a hash reference in scalar context.
429  
430 =back
431  
432 Sample declaration and usage:
433  
434 package MyClass;
435 use Class::MakeMethods::Composite::Global (
436 hash => 'baz',
437 );
438 ...
439
440 # Set values by key
441 MyClass->baz('foo' => 'Foozle', 'bar' => 'Bang!');
442
443 # Values may be overwritten, and in any order
444 MyClass->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle');
445
446 # Retrieve value by key
447 print MyClass->baz('foo');
448
449 # Retrive slice of values by position
450 print join(', ', MyClass->baz( ['foo', 'bar'] ) );
451
452 # Direct access to referenced hash
453 print keys %{ MyClass->baz() };
454
455 # Reset the hash contents to empty
456 @{ MyClass->baz() } = ();
457  
458 =cut
459  
460 use vars qw( %HashFragments );
461  
462 sub hash {
463 (shift)->_build_composite( \%HashFragments, @_ );
464 }
465  
466 %HashFragments = (
467 '' => [
468 '+init' => sub {
469 my ($method) = @_;
470 $method->{hash_key} ||= $_->{name};
471 },
472 'do' => sub {
473 my $method = pop @_;
474 my $self = shift @_;
475 my $args = \@_;
476 if ( scalar(@$args) == 0 ) {
477 if ( $method->{auto_init} and ! defined $method->{global_data} ) {
478 $method->{global_data} = {};
479 }
480 wantarray ? %{ $method->{global_data} } : $method->{global_data};
481 } elsif ( scalar(@$args) == 1 ) {
482 if ( ref($_[0]) eq 'HASH' ) {
483 %{$method->{global_data}} = %{$_[0]};
484 } elsif ( ref($_[0]) eq 'ARRAY' ) {
485 return @{$method->{global_data}}{ @{$_[0]} }
486 } else {
487 return $method->{global_data}->{ $_[0] }
488 }
489 } elsif ( scalar(@$args) % 2 ) {
490 croak "Odd number of items in assigment to $method->{name}";
491 } else {
492 while ( scalar(@$args) ) {
493 my $key = shift @$args;
494 $method->{global_data}->{ $key} = shift @$args;
495 }
496 wantarray ? %{ $method->{global_data} } : $method->{global_data};
497 }
498 },
499 ],
500 );
501  
502 ########################################################################
503  
504 =head2 object - Global Ref Accessor
505  
506 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
507  
508 =over 4
509  
510 =item *
511  
512 May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on.
513  
514 =item *
515  
516 The global value will be a reference to an object (or undef).
517  
518 =item *
519  
520 If called without any arguments returns the current value.
521  
522 =item *
523  
524 If called with an argument, stores that as the value, and returns it,
525  
526 =back
527  
528 Sample declaration and usage:
529  
530 package MyClass;
531 use Class::MakeMethods::Composite::Global (
532 object => 'foo',
533 );
534 ...
535
536 # Store value
537 MyClass->foo( Foozle->new() );
538
539 # Retrieve value
540 print MyClass->foo;
541  
542 =cut
543  
544 use vars qw( %ObjectFragments );
545  
546 sub object {
547 (shift)->_build_composite( \%ObjectFragments, @_ );
548 }
549  
550 %ObjectFragments = (
551 '' => [
552 '+init' => sub {
553 my ($method) = @_;
554 $method->{hash_key} ||= $_->{name};
555 },
556 'do' => sub {
557 my $method = pop @_;
558 my $self = shift;
559 if ( scalar @_ ) {
560 my $value = shift;
561 if ( $method->{class} and ! UNIVERSAL::isa( $value, $method->{class} ) ) {
562 croak "Wrong argument type ('$value') in assigment to $method->{name}";
563 }
564 $method->{global_data} = $value;
565 } else {
566 if ( $method->{auto_init} and ! defined $method->{global_data} ) {
567 my $class = $method->{class}
568 or die "Can't auto_init without a class";
569 my $new_method = $method->{new_method} || 'new';
570 $method->{global_data} = $class->$new_method();
571 }
572 $method->{global_data};
573 }
574 },
575 ],
576 );
577  
578 ########################################################################
579  
580 =head1 SEE ALSO
581  
582 See L<Class::MakeMethods> for general information about this distribution.
583  
584 See L<Class::MakeMethods::Composite> for more about this family of subclasses.
585  
586 =cut
587  
588 1;