File Coverage

blib/lib/Class/MakeMethods/Template/ClassVar.pm
Criterion Covered Total %
statement 11 22 50.0
branch n/a
condition n/a
subroutine 5 7 71.4
pod 3 4 75.0
total 19 33 57.5


line stmt bran cond sub pod time code
1             package Class::MakeMethods::Template::ClassVar;
2              
3 4     4   3894 use Class::MakeMethods::Template::Generic '-isasubclass';
  4         9  
  4         215  
4              
5             $VERSION = 1.008;
6 4     4   25 use strict;
  4         7  
  4         138  
7             require 5.0;
8 4     4   22 use Carp;
  4         8  
  4         1660  
9              
10             =head1 NAME
11              
12             Class::MakeMethods::Template::ClassVar - Static methods with subclass variation
13              
14             =head1 SYNOPSIS
15              
16             package MyObject;
17             use Class::MakeMethods::Template::ClassVar (
18             scalar => [ 'foo' ]
19             );
20            
21             package main;
22              
23             MyObject->foo('bar')
24             print MyObject->foo();
25              
26             $MyObject::foo = 'bazillion';
27             print MyObject->foo();
28              
29             =head1 DESCRIPTION
30              
31             These meta-methods provide access to package (class global) variables,
32             with the package determined at run-time.
33              
34             This is basically the same as the PackageVar meta-methods, except
35             that PackageVar methods find the named variable in the package that
36             defines the method, while ClassVar methods use the package the object
37             is blessed into. As a result, subclasses will each store a distinct
38             value for a ClassVar method, but will share the same value for a
39             PackageVar or Static method.
40              
41             B: The following parameters are defined for ClassVar meta-methods.
42              
43             =over 4
44              
45             =item variable
46              
47             The name of the variable to store the value in. Defaults to the same name as the method.
48              
49             =back
50              
51             =cut
52              
53             sub generic {
54             {
55 4     4 0 51 '-import' => {
56             'Template::Generic:generic' => '*'
57             },
58             'params' => {
59             'variable' => '*'
60             },
61             'modifier' => {
62             '-all' => [ q{ no strict; * } ],
63             },
64             'code_expr' => {
65             '_VALUE_' => '${_SELF_CLASS_."::"._ATTR_{variable}}',
66             },
67             }
68             }
69              
70             ########################################################################
71              
72             =head2 Standard Methods
73              
74             The following methods from Generic should all be supported:
75              
76             scalar
77             string
78             string_index (?)
79             number
80             boolean
81             bits (?)
82             array (*)
83             hash (*)
84             tiedhash (?)
85             hash_of_arrays (?)
86             object (?)
87             instance (?)
88             array_of_objects (?)
89             code (?)
90             code_or_scalar (?)
91              
92             See L for the interfaces and behaviors of these method types.
93              
94             The items marked with a * above are specifically defined in this package, whereas the others are formed automatically by the interaction of this package's generic settings with the code templates provided by the Generic superclass.
95              
96             The items marked with a ? above have not been tested sufficiently; please inform the author if they do not function as you would expect.
97              
98             =cut
99              
100             ########################################################################
101              
102             sub array {
103             {
104 1     1 1 9 '-import' => {
105             'Template::Generic:array' => '*',
106             },
107             'modifier' => {
108             '-all' => q{ no strict; _ENSURE_REF_VALUE_; * },
109             },
110             'code_expr' => {
111             '_ENSURE_REF_VALUE_' => q{
112             _REF_VALUE_ or @{_SELF_CLASS_."::"._ATTR_{variable}} = ();
113             },
114             '_VALUE_' => '(\@{_SELF_CLASS_."::"._ATTR_{variable}})',
115             },
116             }
117             }
118              
119             ########################################################################
120              
121             sub hash {
122             {
123 0     0 1   '-import' => {
124             'Template::Generic:hash' => '*',
125             },
126             'modifier' => {
127             '-all' => q{ no strict; _ENSURE_REF_VALUE_; * },
128             },
129             'code_expr' => {
130             '_ENSURE_REF_VALUE_' => q{
131             _REF_VALUE_ or %{_SELF_CLASS_."::"._ATTR_{variable}} = ();
132             },
133             '_VALUE_' => '(\%{_SELF_CLASS_."::"._ATTR_{variable}})',
134             },
135             }
136             }
137              
138             ########################################################################
139              
140             =head2 vars
141              
142             This rewrite rule converts package variable names into ClassVar methods of the equivalent data type.
143              
144             Here's an example declaration:
145              
146             package MyClass;
147            
148             use Class::MakeMethods::Template::ClassVar (
149             vars => '$VERSION @ISA'
150             );
151              
152             MyClass now has methods that get and set the contents of its $MyClass::VERSION and @MyClass::ISA package variables:
153              
154             MyClass->VERSION( 2.4 );
155             MyClass->push_ISA( 'Exporter' );
156              
157             Subclasses can use these methods to adjust their own variables:
158              
159             package MySubclass;
160             MySubclass->MyClass::push_ISA( 'MyClass' );
161             MySubclass->VERSION( 1.0 );
162              
163             =cut
164              
165             sub vars {
166 0     0 1   my $mm_class = shift;
167 0           my @rewrite = map [ "Template::ClassVar:$_" ], qw( scalar array hash );
168 0           my %rewrite = ( '$' => 0, '@' => 1, '%' => 2 );
169 0           while (@_) {
170 0           my $name = shift;
171 0           my $data = shift;
172 0           $data =~ s/\A(.)//;
173 0           push @{ $rewrite[ $rewrite{ $1 } ] }, { 'name'=>$name, 'variable'=>$data };
  0            
174             }
175 0           return @rewrite;
176             }
177              
178             1;