« blib/lib/Class/MakeMethods/Composite/Array.pm
↑ Summary
blib/lib/Class/MakeMethods/Emulator/accessors.pm »
| 1 | package Class::MakeMethods::Template::Hash; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 41 | ▶ | use Class::MakeMethods::Template::Generic '-isasubclass'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 41 | $VERSION = 1.008; | |||||||||||||||||||
| 6 | 41 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 41 | require 5.0; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 46 | ▶ | sub generic { | ||||||||||||||||||
|
Subroutine
generic: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 10 | { | ||||||||||||||||||||
| 11 | 46 | 'params' => { | |||||||||||||||||||
| 12 | 'hash_key' => '*', | ||||||||||||||||||||
| 13 | }, | ||||||||||||||||||||
| 14 | 'code_expr' => { | ||||||||||||||||||||
| 15 | _VALUE_ => '_SELF_->{_STATIC_ATTR_{hash_key}}', | ||||||||||||||||||||
| 16 | '-import' => { 'Template::Generic:generic' => '*' }, | ||||||||||||||||||||
| 17 | _EMPTY_NEW_INSTANCE_ => 'bless {}, _SELF_CLASS_', | ||||||||||||||||||||
| 18 | _SET_VALUES_FROM_HASH_ => 'while ( scalar @_ ) { local $_ = shift(); $self->{ $_ } = shift() }' | ||||||||||||||||||||
| 19 | }, | ||||||||||||||||||||
| 20 | 'behavior' => { | ||||||||||||||||||||
| 21 | 'hash_delete' => q{ delete _VALUE_ }, | ||||||||||||||||||||
| 22 | 'hash_exists' => q{ exists _VALUE_ }, | ||||||||||||||||||||
| 23 | }, | ||||||||||||||||||||
| 24 | 'modifier' => { | ||||||||||||||||||||
| 25 | # XXX the below doesn't work because modifiers can't have params, | ||||||||||||||||||||
| 26 | # although interfaces can... Either add support for default params | ||||||||||||||||||||
| 27 | # in modifiers, or else move this to another class. | ||||||||||||||||||||
| 28 | # X Should there be a version which uses caller() instead of target_class? | ||||||||||||||||||||
| 29 | 'class_keys' => { 'hash_key' => '"*{target_class}::*{name}"' }, | ||||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | ######################################################################## | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =head1 NAME | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | Class::MakeMethods::Template::Hash - Method interfaces for hash-based objects | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =head1 SYNOPSIS | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | package MyObject; | ||||||||||||||||||||
| 43 | use Class::MakeMethods::Template::Hash ( | ||||||||||||||||||||
| 44 | new => [ 'new' ], | ||||||||||||||||||||
| 45 | scalar => [ 'foo', 'bar' ] | ||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | package main; | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | my $obj = MyObject->new( foo => "Foozle", bar => "Bozzle" ); | ||||||||||||||||||||
| 51 | print $obj->foo(); | ||||||||||||||||||||
| 52 | $obj->bar("Bamboozle"); | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =head1 DESCRIPTION | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | These meta-methods create and access values within blessed hash objects. | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | B<Common Parameters>: The following parameters are defined for Hash meta-methods. | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | =over 4 | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =item hash_key | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | The hash key to use when retrieving values from each hash instance. Defaults to '*', the name of the meta-method. | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | Changing this allows you to change an accessor method name to something other than the name of the hash key used to retrieve its value. | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | Note that this parameter is not portable to the other implementations, such as Global or InsideOut. | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | You can take advantage of parameter expansion to define methods whose hash key is composed of the defining package's name and the individual method name, such as C<$self-E<gt>{I<MyObject>-I<foo>}>: | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 'hash_key' => '*{target_class}-*{name}' | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =back | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | B<Common Behaviors> | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =over 4 | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =item Behavior: delete | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | Deletes the named key and associated value from the current hash instance. | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =back | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =head2 Standard Methods | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | The following methods from Generic are all supported: | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | new | ||||||||||||||||||||
| 91 | scalar | ||||||||||||||||||||
| 92 | string | ||||||||||||||||||||
| 93 | string_index | ||||||||||||||||||||
| 94 | number | ||||||||||||||||||||
| 95 | boolean | ||||||||||||||||||||
| 96 | bits (*) | ||||||||||||||||||||
| 97 | array | ||||||||||||||||||||
| 98 | hash | ||||||||||||||||||||
| 99 | tiedhash | ||||||||||||||||||||
| 100 | hash_of_arrays | ||||||||||||||||||||
| 101 | object | ||||||||||||||||||||
| 102 | instance | ||||||||||||||||||||
| 103 | array_of_objects | ||||||||||||||||||||
| 104 | code | ||||||||||||||||||||
| 105 | code_or_scalar | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | See L<Class::MakeMethods::Template::Generic> for the interfaces and behaviors of these method types. | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 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. | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | # This is the only one that needs to be specifically defined. | ||||||||||||||||||||
| 114 | 2 | ▶ | sub bits { | ||||||||||||||||||
|
Subroutine
bits: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 115 | { | ||||||||||||||||||||
| 116 | 2 | '-import' => { 'Template::Generic:bits' => '*' }, | |||||||||||||||||||
| 117 | 'params' => { | ||||||||||||||||||||
| 118 | 'hash_key' => '*{target_class}__*{template_name}', | ||||||||||||||||||||
| 119 | }, | ||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | ######################################################################## | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =head2 struct | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | struct => [ qw / foo bar baz / ]; | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | Creates methods for setting, checking and clearing values which | ||||||||||||||||||||
| 130 | are stored by position in an array. All the slots created with this | ||||||||||||||||||||
| 131 | meta-method are stored in a single array. | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | The argument to struct should be a string or a reference to an | ||||||||||||||||||||
| 134 | array of strings. For each string meta-method x, it defines two | ||||||||||||||||||||
| 135 | methods: I<x> and I<clear_x>. x returns the value of the x-slot. | ||||||||||||||||||||
| 136 | If called with an argument, it first sets the x-slot to the argument. | ||||||||||||||||||||
| 137 | clear_x sets the slot to undef. | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | Additionally, struct defines three class method: I<struct>, which returns | ||||||||||||||||||||
| 140 | a list of all of the struct values, I<struct_fields>, which returns | ||||||||||||||||||||
| 141 | a list of all the slots by name, and I<struct_dump>, which returns a hash of | ||||||||||||||||||||
| 142 | the slot-name/slot-value pairs. | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | =cut | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 1 | ▶ | sub struct { | ||||||||||||||||||
|
Subroutine
struct: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||
| 147 | ( { | ||||||||||||||||||||
| 148 | 'interface' => { | ||||||||||||||||||||
| 149 | default => { | ||||||||||||||||||||
| 150 | '*'=>'get_set', 'clear_*'=>'clear', | ||||||||||||||||||||
| 151 | 'struct_fields'=>'struct_fields', | ||||||||||||||||||||
| 152 | 'struct'=>'struct', 'struct_dump'=>'struct_dump' | ||||||||||||||||||||
| 153 | }, | ||||||||||||||||||||
| 154 | }, | ||||||||||||||||||||
| 155 | 'params' => { | ||||||||||||||||||||
| 156 | 'hash_key' => '*{target_class}__*{template_name}', | ||||||||||||||||||||
| 157 | }, | ||||||||||||||||||||
| 158 | 'behavior' => { | ||||||||||||||||||||
| 159 | 5 | ▶ | '-init' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=21.3
| |||||||||||||||||||||
| 160 | 5 | my $m_info = $_[0]; | |||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 5 | ▶ | $m_info->{class} ||= $m_info->{target_class}; | ||||||||||||||||||
|
Condition33%1/3
$m_info->{"class"} ||= $m_info->{"target_class"}
MC/DC0%0/2
$m_info->{"class"} ||= $m_info->{"target_class"}
$m_info->{"class"}
$m_info->{"target_class"}
Truth table33%1/3
A ||= B
A= $m_info->{"class"}B= $m_info->{"target_class"}
| |||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | my $class_info = | ||||||||||||||||||||
| 165 | 5 | ▶ | ($Class::MakeMethods::Template::Hash::struct{$m_info->{class}} ||= []); | ||||||||||||||||||
|
Condition100%2/2
$Class::MakeMethods::Template::Hash::struct{$m_info->{"class"}} ||= []
MC/DC100%1/1
$Class::MakeMethods::Template::Hash::struct{$m_info->{"class"}} ||= []
$Class::MakeMethods::Template::Hash::struct{$m_info->{"class"}}
Truth table100%2/2
A
A= $Class::MakeMethods::Template::Hash::struct{$m_info->{"class"}}
| |||||||||||||||||||||
| 166 | 5 | ▶ | if ( ! defined $m_info->{sfp} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 167 | 5 | foreach ( 0..$#$class_info ) { | |||||||||||||||||||
| 168 | 10 | ▶ | if ( $class_info->[$_] eq $m_info->{'name'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 169 | 0 | $m_info->{sfp} = $_; | |||||||||||||||||||
| 170 | last | ||||||||||||||||||||
| 171 | 0 | } | |||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | 5 | ▶ | if ( ! defined $m_info->{sfp} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 174 | 5 | push @$class_info, $m_info->{'name'}; | |||||||||||||||||||
| 175 | 5 | $m_info->{sfp} = $#$class_info; | |||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | } | ||||||||||||||||||||
| 178 | 5 | return; | |||||||||||||||||||
| 179 | }, | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 5 | ▶ | 'struct_fields' => sub { my $m_info = $_[0]; sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 182 | my $class_info = | ||||||||||||||||||||
| 183 | 1 | ▶ | ( $Class::MakeMethods::Template::Hash::struct{$m_info->{class}} ||= [] ); | ||||||||||||||||||
|
Condition50%1/2
$Class::MakeMethods::Template::Hash::struct{$m_info->{"class"}} ||= []
MC/DC0%0/1
$Class::MakeMethods::Template::Hash::struct{$m_info->{"class"}} ||= []
$Class::MakeMethods::Template::Hash::struct{$m_info->{"class"}}
Truth table50%1/2
A
A= $Class::MakeMethods::Template::Hash::struct{$m_info->{"class"}}
| |||||||||||||||||||||
| 184 | 1 | @$class_info; | |||||||||||||||||||
| 185 | 5 | }}, | |||||||||||||||||||
| 186 | 5 | ▶ | 'struct' => sub { my $m_info = $_[0]; sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 187 | 1 | my $self = shift; | |||||||||||||||||||
| 188 | 1 | ▶ | $self->{$m_info->{hash_key}} ||= []; | ||||||||||||||||||
|
Condition50%1/2
$$self{$m_info->{"hash_key"}} ||= []
MC/DC0%0/1
$$self{$m_info->{"hash_key"}} ||= []
$$self{$m_info->{"hash_key"}}
Truth table50%1/2
A
A= $$self{$m_info->{"hash_key"}}
| |||||||||||||||||||||
| 189 | 1 | ▶ | if ( @_ ) { @{$self->{$m_info->{hash_key}}} = @_ } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 190 | 1 | @{$self->{$m_info->{hash_key}}}; | |||||||||||||||||||
| 191 | 5 | }}, | |||||||||||||||||||
| 192 | 5 | ▶ | 'struct_dump' => sub { my $m_info = $_[0]; sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 193 | 1 | my $self = shift; | |||||||||||||||||||
| 194 | my $class_info = | ||||||||||||||||||||
| 195 | 1 | ▶ | ( $Class::MakeMethods::Template::Hash::struct{$m_info->{class}} ||= [] ); | ||||||||||||||||||
|
Condition50%1/2
$Class::MakeMethods::Template::Hash::struct{$m_info->{"class"}} ||= []
MC/DC0%0/1
$Class::MakeMethods::Template::Hash::struct{$m_info->{"class"}} ||= []
$Class::MakeMethods::Template::Hash::struct{$m_info->{"class"}}
Truth table50%1/2
A
A= $Class::MakeMethods::Template::Hash::struct{$m_info->{"class"}}
| |||||||||||||||||||||
| 196 | 1 | map { ($_, $self->$_()) } @$class_info; | |||||||||||||||||||
| 197 | 5 | }}, | |||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 5 | ▶ | 'get_set' => sub { my $m_info = $_[0]; sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 200 | 14 | my $self = shift; | |||||||||||||||||||
| 201 | 14 | ▶ | $self->{$m_info->{hash_key}} ||= []; | ||||||||||||||||||
|
Condition100%2/2
$$self{$m_info->{"hash_key"}} ||= []
MC/DC100%1/1
$$self{$m_info->{"hash_key"}} ||= []
$$self{$m_info->{"hash_key"}}
Truth table100%2/2
A
A= $$self{$m_info->{"hash_key"}}
| |||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | 14 | ▶ | if ( @_ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 204 | 1 | $self->{$m_info->{hash_key}}->[ $m_info->{sfp} ] = shift; | |||||||||||||||||||
| 205 | } | ||||||||||||||||||||
| 206 | 14 | $self->{$m_info->{hash_key}}->[ $m_info->{sfp} ]; | |||||||||||||||||||
| 207 | 5 | }}, | |||||||||||||||||||
| 208 | 5 | ▶ | 'clear' => sub { my $m_info = $_[0]; sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 209 | 1 | my $self = shift; | |||||||||||||||||||
| 210 | 1 | ▶ | $self->{$m_info->{hash_key}} ||= []; | ||||||||||||||||||
|
Condition50%1/2
$$self{$m_info->{"hash_key"}} ||= []
MC/DC0%0/1
$$self{$m_info->{"hash_key"}} ||= []
$$self{$m_info->{"hash_key"}}
Truth table50%1/2
A
A= $$self{$m_info->{"hash_key"}}
| |||||||||||||||||||||
| 211 | 1 | $self->{$m_info->{hash_key}}->[ $m_info->{sfp} ] = undef; | |||||||||||||||||||
| 212 | 5 | }}, | |||||||||||||||||||
| 213 | }, | ||||||||||||||||||||
| 214 | } ) | ||||||||||||||||||||
| 215 | 1 | } | |||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | ######################################################################## | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | =head1 SEE ALSO | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | See L<Class::MakeMethods> for general information about this distribution. | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | See L<Class::MakeMethods::Template> for more about this family of subclasses. | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | See L<Class::MakeMethods::Template::Generic> for information about the various accessor interfaces subclassed herein. | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | =cut | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | 41 | 1; | |||||||||||||||||||