« tests/xemulator/class_methodmaker/Test.pm
↑ Summary
blib/lib/Class/MakeMethods/Template/ClassVar.pm »
| 1 | package Class::MakeMethods::Template::PackageVar; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Class::MakeMethods::Template::Generic '-isasubclass'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | $VERSION = 1.008; | |||||||||
| 6 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | require 5.0; | |||||||||
| 8 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | Class::MakeMethods::Template::PackageVar - Static methods with global variables | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | package MyObject; | ||||||||||
| 17 | use Class::MakeMethods::Template::PackageVar ( | ||||||||||
| 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 | These are essentially the same as the Static meta-methods, except | ||||||||||
| 33 | that they use a global variable in the declaring package to store | ||||||||||
| 34 | their values. | ||||||||||
| 35 | |||||||||||
| 36 | B<Common Parameters>: The following parameters are defined for PackageVar meta-methods. | ||||||||||
| 37 | |||||||||||
| 38 | =over 4 | ||||||||||
| 39 | |||||||||||
| 40 | =item variable | ||||||||||
| 41 | |||||||||||
| 42 | The name of the variable to store the value in. Defaults to the same name as the method. | ||||||||||
| 43 | |||||||||||
| 44 | =back | ||||||||||
| 45 | |||||||||||
| 46 | =cut | ||||||||||
| 47 | |||||||||||
| 48 | 1 | ▶ | sub generic { | ||||||||
|
Subroutine
generic: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 49 | { | ||||||||||
| 50 | 1 | '-import' => { | |||||||||
| 51 | 'Template::Generic:generic' => '*' | ||||||||||
| 52 | }, | ||||||||||
| 53 | 'params' => { | ||||||||||
| 54 | 'variable' => '*' | ||||||||||
| 55 | }, | ||||||||||
| 56 | 'modifier' => { | ||||||||||
| 57 | '-all' => [ q{ no strict; * } ], | ||||||||||
| 58 | }, | ||||||||||
| 59 | 'code_expr' => { | ||||||||||
| 60 | '_VALUE_' => '${_ATTR_{target_class}."::"._ATTR_{variable}}', | ||||||||||
| 61 | }, | ||||||||||
| 62 | } | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | ######################################################################## | ||||||||||
| 66 | |||||||||||
| 67 | =head2 Standard Methods | ||||||||||
| 68 | |||||||||||
| 69 | The following methods from Generic should all be supported: | ||||||||||
| 70 | |||||||||||
| 71 | scalar | ||||||||||
| 72 | string | ||||||||||
| 73 | string_index (?) | ||||||||||
| 74 | number | ||||||||||
| 75 | boolean | ||||||||||
| 76 | bits (?) | ||||||||||
| 77 | array (*) | ||||||||||
| 78 | hash (*) | ||||||||||
| 79 | tiedhash (?) | ||||||||||
| 80 | hash_of_arrays (?) | ||||||||||
| 81 | object (?) | ||||||||||
| 82 | instance (?) | ||||||||||
| 83 | array_of_objects (?) | ||||||||||
| 84 | code (?) | ||||||||||
| 85 | code_or_scalar (?) | ||||||||||
| 86 | |||||||||||
| 87 | See L<Class::MakeMethods::Template::Generic> for the interfaces and behaviors of these method types. | ||||||||||
| 88 | |||||||||||
| 89 | 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. | ||||||||||
| 90 | |||||||||||
| 91 | The items marked with a ? above have not been tested sufficiently; please inform the author if they do not function as you would expect. | ||||||||||
| 92 | |||||||||||
| 93 | =cut | ||||||||||
| 94 | |||||||||||
| 95 | ######################################################################## | ||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | sub array { | ||||||||
|
Subroutine
array: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 98 | { | ||||||||||
| 99 | 0 | '-import' => { | |||||||||
| 100 | 'Template::Generic:array' => '*', | ||||||||||
| 101 | }, | ||||||||||
| 102 | 'modifier' => { | ||||||||||
| 103 | '-all' => q{ no strict; _ENSURE_REF_VALUE_; * }, | ||||||||||
| 104 | }, | ||||||||||
| 105 | 'code_expr' => { | ||||||||||
| 106 | '_ENSURE_REF_VALUE_' => q{ | ||||||||||
| 107 | _REF_VALUE_ or @{_ATTR_{target_class}."::"._ATTR_{variable}} = (); | ||||||||||
| 108 | }, | ||||||||||
| 109 | '_VALUE_' => '\@{_ATTR_{target_class}."::"._ATTR_{variable}}', | ||||||||||
| 110 | }, | ||||||||||
| 111 | } | ||||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | ######################################################################## | ||||||||||
| 115 | |||||||||||
| 116 | 0 | ▶ | sub hash { | ||||||||
|
Subroutine
hash: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 117 | { | ||||||||||
| 118 | 0 | '-import' => { | |||||||||
| 119 | 'Template::Generic:hash' => '*', | ||||||||||
| 120 | }, | ||||||||||
| 121 | 'modifier' => { | ||||||||||
| 122 | '-all' => q{ no strict; _ENSURE_REF_VALUE_; * }, | ||||||||||
| 123 | }, | ||||||||||
| 124 | 'code_expr' => { | ||||||||||
| 125 | '_ENSURE_REF_VALUE_' => q{ | ||||||||||
| 126 | _REF_VALUE_ or %{_ATTR_{target_class}."::"._ATTR_{variable}} = (); | ||||||||||
| 127 | }, | ||||||||||
| 128 | '_VALUE_' => '\%{_ATTR_{target_class}."::"._ATTR_{variable}}', | ||||||||||
| 129 | }, | ||||||||||
| 130 | } | ||||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | ######################################################################## | ||||||||||
| 134 | |||||||||||
| 135 | =head2 PackageVar:vars | ||||||||||
| 136 | |||||||||||
| 137 | This rewrite rule converts package variable names into PackageVar methods of the equivalent data type. | ||||||||||
| 138 | |||||||||||
| 139 | Here's an example declaration: | ||||||||||
| 140 | |||||||||||
| 141 | package MyClass; | ||||||||||
| 142 | |||||||||||
| 143 | use Class::MakeMethods::Template::PackageVar ( | ||||||||||
| 144 | vars => '$DEBUG %Index' | ||||||||||
| 145 | ); | ||||||||||
| 146 | |||||||||||
| 147 | MyClass now has methods that get and set the contents of its $MyClass::DEBUG and %MyClass::Index package variables: | ||||||||||
| 148 | |||||||||||
| 149 | MyClass->DEBUG( 1 ); | ||||||||||
| 150 | MyClass->Index( 'foo' => 'bar' ); | ||||||||||
| 151 | |||||||||||
| 152 | =cut | ||||||||||
| 153 | |||||||||||
| 154 | 0 | ▶ | sub vars { | ||||||||
|
Subroutine
vars: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 155 | 0 | my $mm_class = shift; | |||||||||
| 156 | 0 | my @rewrite = map [ "Template::PackageVar:$_" ], qw( scalar array hash ); | |||||||||
| 157 | 0 | my %rewrite = ( '$' => 0, '@' => 1, '%' => 2 ); | |||||||||
| 158 | 0 | ▶ | while (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 159 | 0 | my $name = shift; | |||||||||
| 160 | 0 | my $data = shift; | |||||||||
| 161 | 0 | $data =~ s/\A(.)//; | |||||||||
| 162 | 0 | push @{ $rewrite[ $rewrite{ $1 } ] }, { 'name'=>$name, 'variable'=>$data }; | |||||||||
| 163 | } | ||||||||||
| 164 | 0 | return @rewrite; | |||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | |||||||||||
| 168 | 1 | 1; | |||||||||