| 1 | package Class::Methodist; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 9 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | Class::Methodist - define methods for instance variables in a class | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | package My::Shiny::New::Class; | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | use Class::Methodist | ||||||||||||||||||||
| 16 | ( | ||||||||||||||||||||
| 17 | scalar => 'global_config_path', | ||||||||||||||||||||
| 18 | hash => 'unique_words', | ||||||||||||||||||||
| 19 | list => 'file_names', | ||||||||||||||||||||
| 20 | object => { name => 'thing', class => 'My::Thing:Class' }, | ||||||||||||||||||||
| 21 | enum => { name => 'color', domain => [ 'red', 'green', 'blue' ] }, | ||||||||||||||||||||
| 22 | scalars => [ 'alpha', 'beta' ] | ||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | sub new { | ||||||||||||||||||||
| 26 | my ($class, $alpha) = @_; | ||||||||||||||||||||
| 27 | $class->beget(alpha => $alpha, beta => 42); | ||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | This package creates instance variables and methods on a class for | ||||||||||||||||||||
| 33 | accessing and manipulating those instance variables. | ||||||||||||||||||||
| 34 | C<Class::Methodist> is similar in spirit to C<Class::MakeMethods>, but | ||||||||||||||||||||
| 35 | with a simpler interface and more sensible semantics. | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | Instance variables to be defined are given as a list of I<instance | ||||||||||||||||||||
| 38 | variable specifications> (a.k.a. I<specification>) when the module is | ||||||||||||||||||||
| 39 | used. A specification consists of a pair whose first element is the | ||||||||||||||||||||
| 40 | I<type> of the variable (e.g., C<scalar>, C<hash>, C<list>) and whose | ||||||||||||||||||||
| 41 | second element is the I<name> of the variable to be defined. The | ||||||||||||||||||||
| 42 | latter must be a valid Perl identifier name. | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | For each specification, the module defines a type-specific set of | ||||||||||||||||||||
| 45 | methods on the calling class. The names of these methods usually | ||||||||||||||||||||
| 46 | include the name of the instance variable. In the following sections, | ||||||||||||||||||||
| 47 | we refer to the instance variable name by the generic identifier | ||||||||||||||||||||
| 48 | I<inst_var>. | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | In your constructor you must call the C<beget> class method to | ||||||||||||||||||||
| 51 | instantiate and initialize each instance of the class. | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =head1 CLASS METHODS | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =over | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =item beget() | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | This class method instantiates and initializes an object of the class. | ||||||||||||||||||||
| 60 | It takes the place of an explicit call to the Perl C<bless> function | ||||||||||||||||||||
| 61 | (which it invokes under the hood). | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | You may pass arguments to C<beget> to initialize the new object. | ||||||||||||||||||||
| 64 | These arguments must appear in pairs (as for a hash initializer). The | ||||||||||||||||||||
| 65 | first item in each pair should be the name of an attribute defined by | ||||||||||||||||||||
| 66 | your use of C<Class::Methodist> and the second item in each pair | ||||||||||||||||||||
| 67 | should be the value to which that attribute is initialized. Note that | ||||||||||||||||||||
| 68 | if you initialize I<list> or I<hash> attributes, you must pass the | ||||||||||||||||||||
| 69 | initializer value as a I<reference> to an array or hash, respectively. | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | The C<beget> method blesses the new object into the class and returns | ||||||||||||||||||||
| 72 | the blessed object. You can either assign the return value to a | ||||||||||||||||||||
| 73 | variable (often, C<self>) for further construction, or may simply | ||||||||||||||||||||
| 74 | invoke C<beget> as the final statement in your constructor, which | ||||||||||||||||||||
| 75 | arranges to return the newly minted object to the caller. | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =item import() | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | This method satisfies the C<import> semantics required of any module | ||||||||||||||||||||
| 80 | that uses C<Class::Methodist>. It takes as arguments the list of | ||||||||||||||||||||
| 81 | specifications provided in the C<use> directive in the calling module. | ||||||||||||||||||||
| 82 | The method defines the instance variables and their associated methods | ||||||||||||||||||||
| 83 | in the namespace of the I<calling class>, also referred to as the | ||||||||||||||||||||
| 84 | I<destination class>. | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 12 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=12 SCAR=25.2
| |||||||||||||||||||||
| 89 | 12 | my ($my_class, @args) = @_; | |||||||||||||||||||
| 90 | 12 | my $dest_class = caller; # Caller's class for importing methods. | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 12 | ▶ | while (my ($type, $spec) = splice(@args, 0, 2)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | SWITCH: | ||||||||||||||||||||
| 94 | 31 | for ($type) { | |||||||||||||||||||
| 95 | 31 | ▶ | /ctor/ and do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 10 | define_constructor($dest_class, $spec); | |||||||||||||||||||
| 97 | 10 | last SWITCH; | |||||||||||||||||||
| 98 | }; | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 21 | ▶ | /enum/ and do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 2 | define_enum_methods($dest_class, $spec); | |||||||||||||||||||
| 102 | 2 | last SWITCH; | |||||||||||||||||||
| 103 | }; | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 19 | ▶ | /hash$/ and do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 4 | define_hash_methods($dest_class, $spec); | |||||||||||||||||||
| 107 | 4 | last SWITCH; | |||||||||||||||||||
| 108 | }; | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 15 | ▶ | /hash_of_lists/ and do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | 1 | define_hash_of_lists_methods($dest_class, $spec); | |||||||||||||||||||
| 112 | 1 | last SWITCH; | |||||||||||||||||||
| 113 | }; | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 14 | ▶ | /list/ and do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 4 | define_list_methods($dest_class, $spec); | |||||||||||||||||||
| 117 | 4 | last SWITCH; | |||||||||||||||||||
| 118 | }; | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 10 | ▶ | /object/ and do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 5 | define_object_methods($dest_class, $spec); | |||||||||||||||||||
| 122 | 5 | last SWITCH; | |||||||||||||||||||
| 123 | }; | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 5 | ▶ | /scalar$/ and do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 5 | define_scalar_methods($dest_class, $spec); | |||||||||||||||||||
| 127 | 5 | last SWITCH; | |||||||||||||||||||
| 128 | }; | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 0 | ▶ | /scalars/ and do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | 0 | define_scalar_methods($dest_class, $_) foreach @$spec; | |||||||||||||||||||
| 132 | 0 | last SWITCH; | |||||||||||||||||||
| 133 | }; | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 0 | confess "Invalid type '$type'"; | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 12 | define_utility_methods($dest_class); | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | #---------------------------------------------------------------- | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | =item verify_method_not_defined($dest_class, $method) | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | We don't want to overwrite methods already defined in the calling | ||||||||||||||||||||
| 147 | class. Check whether C<$method> is defined in the destination class. | ||||||||||||||||||||
| 148 | If so, throw an exception. | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =cut | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 0 | ▶ | sub verify_method_not_defined { | ||||||||||||||||||
|
Subroutine
verify_method_not_defined: not called CC=2 SCAR=12.2
Pod
documented
| |||||||||||||||||||||
| 153 | 0 | my ($dest_class, $method) = @_; | |||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 9 | ▶ | use Devel::Symdump; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 156 | 0 | my @functions = Devel::Symdump->new($dest_class)->functions(); | |||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 0 | ▶ | if (grep { $_ eq $method } @functions) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | 0 | confess "Method '$method' already exists in class '$dest_class'"; | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | #---------------------------------------------------------------- | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =item define_method($dest_class, $method, $sub_ref) | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | Define a method named C<$method> in the destination class | ||||||||||||||||||||
| 168 | C<$dest_class> to be the subroutine refererenced by C<$sub_ref>. It | ||||||||||||||||||||
| 169 | is an error to define a method that already exists. This method is | ||||||||||||||||||||
| 170 | the business end of this module in that all the following | ||||||||||||||||||||
| 171 | type-specific methods invoke C<define_method> in order to create the | ||||||||||||||||||||
| 172 | method(s) associated with each instance variable. | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =back | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =cut | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | 197 | ▶ | sub define_method { | ||||||||||||||||||
|
Subroutine
define_method: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 179 | 197 | my ($dest_class, $method, $sub_ref) = @_; | |||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | # Try turning this off for now. Called a *lot*. May not be of much | ||||||||||||||||||||
| 182 | # benefit. | ||||||||||||||||||||
| 183 | # | ||||||||||||||||||||
| 184 | # verify_method_not_defined($dest_class, $method); | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 197 | my $fq_name = sprintf('%s::%s', $dest_class, $method); | |||||||||||||||||||
| 187 | { | ||||||||||||||||||||
| 188 | 9 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 189 | 197 | *{$fq_name} = $sub_ref; | |||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | } | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | #---------------------------------------------------------------- | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | { | ||||||||||||||||||||
| 196 | 9 | my %methodist_info; | |||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | ## Return whether the named class has Methodist-internal data. This | ||||||||||||||||||||
| 199 | ## subroutine was added to allow us to handle properly inheritance | ||||||||||||||||||||
| 200 | ## from classes that don't use Methodist (e.g., Class::Singleton). | ||||||||||||||||||||
| 201 | 22 | ▶ | sub _has_methodist_info { | ||||||||||||||||||
|
Subroutine
_has_methodist_info: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 202 | 22 | my $dest_class = shift; | |||||||||||||||||||
| 203 | 22 | exists $methodist_info{$dest_class}; | |||||||||||||||||||
| 204 | } | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | ## Add @values to the list stored under $key in the Methodist-internal | ||||||||||||||||||||
| 207 | ## data for this class. | ||||||||||||||||||||
| 208 | 18 | ▶ | sub _add_methodist_info { | ||||||||||||||||||
|
Subroutine
_add_methodist_info: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 209 | 18 | my ($dest_class, $key, @values) = @_; | |||||||||||||||||||
| 210 | 18 | push @{$methodist_info{$dest_class}{$key}}, @values; | |||||||||||||||||||
| 211 | } | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | ## Return the list of values stored under $key in the | ||||||||||||||||||||
| 214 | ## Methodist-internal data for this class. If no data are stored | ||||||||||||||||||||
| 215 | ## under that key, return an empty list. | ||||||||||||||||||||
| 216 | 30 | ▶ | sub _get_methodist_info { | ||||||||||||||||||
|
Subroutine
_get_methodist_info: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 217 | 30 | my ($dest_class, $key) = @_; | |||||||||||||||||||
| 218 | 30 | ▶ | $methodist_info{$dest_class}{$key} ||= [ ]; | ||||||||||||||||||
|
Condition100%2/2
$methodist_info{$dest_class}{$key} ||= []
MC/DC100%1/1
$methodist_info{$dest_class}{$key} ||= []
$methodist_info{$dest_class}{$key}
Truth table100%2/2
A
A= $methodist_info{$dest_class}{$key}
| |||||||||||||||||||||
| 219 | 30 | return @{$methodist_info{$dest_class}{$key}}; | |||||||||||||||||||
| 220 | } | ||||||||||||||||||||
| 221 | } | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | #---------------------------------------------------------------- | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | =head2 Constructor | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | Define a constructor in the destination class as follows: | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | ctor => 'new' | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | The generated constructor simply blesses an anonymous hash into the | ||||||||||||||||||||
| 232 | destination class. | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | =cut | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | 9 | ▶ | sub define_constructor { | ||||||||||||||||||
|
Subroutine
define_constructor: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 237 | 10 | my ($dest_class, $name) = @_; | |||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | ## Bless a hash reference into the destination class. | ||||||||||||||||||||
| 240 | define_method($dest_class, $name, | ||||||||||||||||||||
| 241 | 15 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 242 | 15 | $dest_class->beget(); | |||||||||||||||||||
| 243 | 10 | }); | |||||||||||||||||||
| 244 | } | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | #---------------------------------------------------------------- | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | =head2 Enum | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | Define methods in the destination class for a scalar-valued instance | ||||||||||||||||||||
| 251 | variable that is constrained to take one of an enumerated series of | ||||||||||||||||||||
| 252 | values. | ||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | enum => { name => 'colors', | ||||||||||||||||||||
| 255 | domain => [ qw/red green blue/ ], | ||||||||||||||||||||
| 256 | default => 'blue' } | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | The C<name> and C<domain> attributes are required. If the C<default> | ||||||||||||||||||||
| 259 | attribute is provided, its value must evaluate to a member of the | ||||||||||||||||||||
| 260 | domain. | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | =over | ||||||||||||||||||||
| 263 | |||||||||||||||||||||
| 264 | =cut | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | 2 | ▶ | sub define_enum_methods { | ||||||||||||||||||
|
Subroutine
define_enum_methods: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 267 | 2 | my ($dest_class, $spec) = @_; | |||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | 2 | my $name = $spec->{name}; | |||||||||||||||||||
| 270 | 2 | my @domain = @{$spec->{domain}}; | |||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | 2 | _add_methodist_info($dest_class, attributes => [ enum => $name ]); | |||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | 2 | ▶ | if (defined $spec->{default}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 275 | croak sprintf("Default (%s) not among %s", | ||||||||||||||||||||
| 276 | 1 | ▶ | $spec->{default}, join(', ', @domain)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 277 | unless (grep(/$spec->{default}/, @domain)); | ||||||||||||||||||||
| 278 | _add_methodist_info($dest_class, | ||||||||||||||||||||
| 279 | 1 | default => [ $name => $spec->{default} ]); | |||||||||||||||||||
| 280 | } | ||||||||||||||||||||
| 281 | |||||||||||||||||||||
| 282 | =item I<inst_var>(...) | ||||||||||||||||||||
| 283 | |||||||||||||||||||||
| 284 | The method named the same as the instance variable is the setter and | ||||||||||||||||||||
| 285 | getter. If called with no arguments, returns the current value of the | ||||||||||||||||||||
| 286 | enumerated attribute. If called with an argument, the scalar is set | ||||||||||||||||||||
| 287 | to that value, provided the value is one of the values enumerated in | ||||||||||||||||||||
| 288 | the C<domain> list. If the value is not in the domain, throws an | ||||||||||||||||||||
| 289 | error. | ||||||||||||||||||||
| 290 | |||||||||||||||||||||
| 291 | =back | ||||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | =cut | ||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | define_method($dest_class, $name, | ||||||||||||||||||||
| 296 | 24 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 297 | 24 | my ($self, $arg) = @_; | |||||||||||||||||||
| 298 | 24 | ▶ | if (defined($arg)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 299 | 12 | ▶ | if (grep(/$arg/, @domain)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 300 | 10 | $self->{$name} = $arg; | |||||||||||||||||||
| 301 | } else { | ||||||||||||||||||||
| 302 | 2 | croak sprintf("%s not among %s", | |||||||||||||||||||
| 303 | $arg, join(', ', @domain)); | ||||||||||||||||||||
| 304 | } | ||||||||||||||||||||
| 305 | } | ||||||||||||||||||||
| 306 | 22 | $self->{$name}; | |||||||||||||||||||
| 307 | 2 | }); | |||||||||||||||||||
| 308 | } | ||||||||||||||||||||
| 309 | |||||||||||||||||||||
| 310 | #---------------------------------------------------------------- | ||||||||||||||||||||
| 311 | |||||||||||||||||||||
| 312 | =head2 Hash | ||||||||||||||||||||
| 313 | |||||||||||||||||||||
| 314 | Define methods in the destination class for a hash-valued instance | ||||||||||||||||||||
| 315 | variable called I<inst_var> as follows: | ||||||||||||||||||||
| 316 | |||||||||||||||||||||
| 317 | hash => 'inst_var' | ||||||||||||||||||||
| 318 | |||||||||||||||||||||
| 319 | This specification defines the following methods in the destination | ||||||||||||||||||||
| 320 | class: | ||||||||||||||||||||
| 321 | |||||||||||||||||||||
| 322 | =over | ||||||||||||||||||||
| 323 | |||||||||||||||||||||
| 324 | =cut | ||||||||||||||||||||
| 325 | |||||||||||||||||||||
| 326 | 4 | ▶ | sub define_hash_methods { | ||||||||||||||||||
|
Subroutine
define_hash_methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 327 | 4 | my ($dest_class, $name) = @_; | |||||||||||||||||||
| 328 | |||||||||||||||||||||
| 329 | 4 | _add_methodist_info($dest_class, attributes => [ hash => $name ]); | |||||||||||||||||||
| 330 | |||||||||||||||||||||
| 331 | =item I<inst_var>($key, [$value]) | ||||||||||||||||||||
| 332 | |||||||||||||||||||||
| 333 | The method having the same name as the instance variable is the setter | ||||||||||||||||||||
| 334 | and getter: | ||||||||||||||||||||
| 335 | |||||||||||||||||||||
| 336 | my $value = $obj->inst_var('key'); | ||||||||||||||||||||
| 337 | $obj->inst_var(key => 'value'); | ||||||||||||||||||||
| 338 | |||||||||||||||||||||
| 339 | When called with a single argument, there are two cases. First, if | ||||||||||||||||||||
| 340 | the argument is a hash reference, replace the contents of the hash | ||||||||||||||||||||
| 341 | with that of the referenced hash. Second, if it is not a hash | ||||||||||||||||||||
| 342 | reference, treat it as a key; the method returns the value stored | ||||||||||||||||||||
| 343 | under that key. | ||||||||||||||||||||
| 344 | |||||||||||||||||||||
| 345 | When called with more than one argument, treat the arguments as | ||||||||||||||||||||
| 346 | key-value pairs and store them in the hash. There must be an even | ||||||||||||||||||||
| 347 | number of arguments (i.e., they must be pairs). Return the value of | ||||||||||||||||||||
| 348 | the last pair. | ||||||||||||||||||||
| 349 | |||||||||||||||||||||
| 350 | =cut | ||||||||||||||||||||
| 351 | |||||||||||||||||||||
| 352 | define_method($dest_class, $name, | ||||||||||||||||||||
| 353 | 13 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 354 | 13 | my ($self, @args) = @_; | |||||||||||||||||||
| 355 | 13 | my $rtn = undef; | |||||||||||||||||||
| 356 | |||||||||||||||||||||
| 357 | 13 | ▶ | if (@args == 1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 358 | 8 | ▶ | if (ref $args[0] eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 359 | 1 | $self->{$name} = $args[0]; | |||||||||||||||||||
| 360 | } else { | ||||||||||||||||||||
| 361 | 7 | $rtn = $self->{$name}{$args[0]}; | |||||||||||||||||||
| 362 | } | ||||||||||||||||||||
| 363 | } else { | ||||||||||||||||||||
| 364 | 5 | ▶ | while (my ($key, $val) = splice(@args, 0, 2)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 365 | 8 | $rtn = $self->{$name}{$key} = $val; | |||||||||||||||||||
| 366 | } | ||||||||||||||||||||
| 367 | } | ||||||||||||||||||||
| 368 | 13 | return $rtn; | |||||||||||||||||||
| 369 | 4 | }); | |||||||||||||||||||
| 370 | |||||||||||||||||||||
| 371 | =item I<inst_var>_exists($key) | ||||||||||||||||||||
| 372 | |||||||||||||||||||||
| 373 | Method that returns whether a key exists in the hash. | ||||||||||||||||||||
| 374 | |||||||||||||||||||||
| 375 | if ($obj->inst_var_exists('key')) { ... } | ||||||||||||||||||||
| 376 | |||||||||||||||||||||
| 377 | =cut | ||||||||||||||||||||
| 378 | |||||||||||||||||||||
| 379 | define_method($dest_class, "${name}_exists", | ||||||||||||||||||||
| 380 | 5 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 381 | 5 | my ($self, $key) = @_; | |||||||||||||||||||
| 382 | 5 | ▶ | confess "Must supply key" unless defined $key; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 383 | 5 | ▶ | exists $self->{$name}{$key} ? 1 : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 384 | 4 | }); | |||||||||||||||||||
| 385 | |||||||||||||||||||||
| 386 | =item I<inst_var>_keys() | ||||||||||||||||||||
| 387 | |||||||||||||||||||||
| 388 | Method that returns the list of keys in the hash. | ||||||||||||||||||||
| 389 | |||||||||||||||||||||
| 390 | my @keys = $obj->inst_var_keys(); | ||||||||||||||||||||
| 391 | |||||||||||||||||||||
| 392 | =cut | ||||||||||||||||||||
| 393 | |||||||||||||||||||||
| 394 | define_method($dest_class, "${name}_keys", | ||||||||||||||||||||
| 395 | 2 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 396 | 2 | my $self = shift; | |||||||||||||||||||
| 397 | 2 | sort keys %{$self->{$name}}; | |||||||||||||||||||
| 398 | 4 | }); | |||||||||||||||||||
| 399 | |||||||||||||||||||||
| 400 | =item I<inst_var>_values() | ||||||||||||||||||||
| 401 | |||||||||||||||||||||
| 402 | Method that returns the list of values in the hash. | ||||||||||||||||||||
| 403 | |||||||||||||||||||||
| 404 | my @values = $obj->inst_var_values(); | ||||||||||||||||||||
| 405 | |||||||||||||||||||||
| 406 | =cut | ||||||||||||||||||||
| 407 | |||||||||||||||||||||
| 408 | define_method($dest_class, "${name}_values", | ||||||||||||||||||||
| 409 | 2 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 410 | 2 | my $self = shift; | |||||||||||||||||||
| 411 | 2 | sort values %{$self->{$name}} | |||||||||||||||||||
| 412 | 4 | }); | |||||||||||||||||||
| 413 | |||||||||||||||||||||
| 414 | =item I<inst_var>_clear() | ||||||||||||||||||||
| 415 | |||||||||||||||||||||
| 416 | Method that clears the hash. | ||||||||||||||||||||
| 417 | |||||||||||||||||||||
| 418 | $obj->inst_var_clear(); | ||||||||||||||||||||
| 419 | |||||||||||||||||||||
| 420 | =cut | ||||||||||||||||||||
| 421 | |||||||||||||||||||||
| 422 | define_method($dest_class, "${name}_clear", | ||||||||||||||||||||
| 423 | 3 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 424 | 3 | my $self = shift; | |||||||||||||||||||
| 425 | 3 | $self->{$name} = { }; | |||||||||||||||||||
| 426 | 4 | }); | |||||||||||||||||||
| 427 | |||||||||||||||||||||
| 428 | =item I<inst_var>_delete($key) | ||||||||||||||||||||
| 429 | |||||||||||||||||||||
| 430 | Delete the hash element with the given key. | ||||||||||||||||||||
| 431 | |||||||||||||||||||||
| 432 | $obj->inst_var_delete($key) | ||||||||||||||||||||
| 433 | |||||||||||||||||||||
| 434 | =cut | ||||||||||||||||||||
| 435 | |||||||||||||||||||||
| 436 | define_method($dest_class, "${name}_delete", | ||||||||||||||||||||
| 437 | 0 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 438 | 0 | my ($self, $key) = @_; | |||||||||||||||||||
| 439 | 0 | delete $self->{$name}{$key}; | |||||||||||||||||||
| 440 | 4 | }); | |||||||||||||||||||
| 441 | |||||||||||||||||||||
| 442 | =item I<inst_var>_size() | ||||||||||||||||||||
| 443 | |||||||||||||||||||||
| 444 | Return the number of key-value pairs stored in the hash. | ||||||||||||||||||||
| 445 | |||||||||||||||||||||
| 446 | my $size = inst_var_size(); | ||||||||||||||||||||
| 447 | |||||||||||||||||||||
| 448 | =cut | ||||||||||||||||||||
| 449 | |||||||||||||||||||||
| 450 | define_method($dest_class, "${name}_size", | ||||||||||||||||||||
| 451 | 2 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 452 | 2 | my $self = shift; | |||||||||||||||||||
| 453 | 2 | scalar keys %{$self->{$name}}; | |||||||||||||||||||
| 454 | 4 | }); | |||||||||||||||||||
| 455 | |||||||||||||||||||||
| 456 | =item I<inst_var>_inc($key, [$n]) | ||||||||||||||||||||
| 457 | |||||||||||||||||||||
| 458 | Add the value of C<$n> to the value found under C<$key> in the hash. | ||||||||||||||||||||
| 459 | The value of C<$n> defaults to one, yielding a simple increment | ||||||||||||||||||||
| 460 | operation. Return the new value. | ||||||||||||||||||||
| 461 | |||||||||||||||||||||
| 462 | =back | ||||||||||||||||||||
| 463 | |||||||||||||||||||||
| 464 | =cut | ||||||||||||||||||||
| 465 | |||||||||||||||||||||
| 466 | define_method($dest_class, "${name}_inc", | ||||||||||||||||||||
| 467 | 3 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 468 | 3 | my ($self, $key, $n) = @_; | |||||||||||||||||||
| 469 | 3 | ▶ | $n = 1 unless defined $n; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 470 | 3 | $self->{$name}{$key} += $n; | |||||||||||||||||||
| 471 | 4 | }); | |||||||||||||||||||
| 472 | } | ||||||||||||||||||||
| 473 | |||||||||||||||||||||
| 474 | #---------------------------------------------------------------- | ||||||||||||||||||||
| 475 | |||||||||||||||||||||
| 476 | =head2 Hash of Lists | ||||||||||||||||||||
| 477 | |||||||||||||||||||||
| 478 | Define methods in the destination class for a hash-of-lists instance | ||||||||||||||||||||
| 479 | variable called I<inst_var> as follows: | ||||||||||||||||||||
| 480 | |||||||||||||||||||||
| 481 | hash_of_lists => 'inst_var' | ||||||||||||||||||||
| 482 | |||||||||||||||||||||
| 483 | This specification defines the following methods in the destination | ||||||||||||||||||||
| 484 | class: | ||||||||||||||||||||
| 485 | |||||||||||||||||||||
| 486 | =over | ||||||||||||||||||||
| 487 | |||||||||||||||||||||
| 488 | =cut | ||||||||||||||||||||
| 489 | |||||||||||||||||||||
| 490 | 1 | ▶ | sub define_hash_of_lists_methods { | ||||||||||||||||||
|
Subroutine
define_hash_of_lists_methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 491 | 1 | my ($dest_class, $name) = @_; | |||||||||||||||||||
| 492 | |||||||||||||||||||||
| 493 | 1 | _add_methodist_info($dest_class, attributes => [ hash => $name ]); | |||||||||||||||||||
| 494 | |||||||||||||||||||||
| 495 | =item I<inst_var>(...) | ||||||||||||||||||||
| 496 | |||||||||||||||||||||
| 497 | The method having the same name as the instance variable is the setter | ||||||||||||||||||||
| 498 | and getter. Its behavior depends on the number of arguments passed to | ||||||||||||||||||||
| 499 | the method. | ||||||||||||||||||||
| 500 | |||||||||||||||||||||
| 501 | When called with no arguments, the method returns all the values | ||||||||||||||||||||
| 502 | stored in all the lists. | ||||||||||||||||||||
| 503 | |||||||||||||||||||||
| 504 | When called with one argument, it is treated as a key into the hash | ||||||||||||||||||||
| 505 | and returns the values stored in the list having that hash key. | ||||||||||||||||||||
| 506 | |||||||||||||||||||||
| 507 | The method returns a list in array context and a reference to a list | ||||||||||||||||||||
| 508 | in scalar context. | ||||||||||||||||||||
| 509 | |||||||||||||||||||||
| 510 | =cut | ||||||||||||||||||||
| 511 | |||||||||||||||||||||
| 512 | define_method($dest_class, $name, | ||||||||||||||||||||
| 513 | 4 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.1
| |||||||||||||||||||||
| 514 | 4 | my ($self, @args) = @_; | |||||||||||||||||||
| 515 | |||||||||||||||||||||
| 516 | 4 | my @rtn; | |||||||||||||||||||
| 517 | 4 | ▶ | if (@args == 0) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 518 | # Called with no arguments. Return all the values | ||||||||||||||||||||
| 519 | # stored in all the lists. | ||||||||||||||||||||
| 520 | 1 | push @rtn, @$_ foreach values %{$self->{$name}}; | |||||||||||||||||||
| 521 | } elsif (@args == 1) { | ||||||||||||||||||||
| 522 | # Called with one argument. Return all the values | ||||||||||||||||||||
| 523 | # stored in the list having that value as a key | ||||||||||||||||||||
| 524 | 2 | my $key = $args[0]; | |||||||||||||||||||
| 525 | $self->{$name}{$key} = [ ] | ||||||||||||||||||||
| 526 | 2 | ▶ | unless defined $self->{$name}{$key}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 527 | 2 | @rtn = @{$self->{$name}{$key}}; | |||||||||||||||||||
| 528 | } else { | ||||||||||||||||||||
| 529 | 1 | confess "Must have zero or one arguments"; | |||||||||||||||||||
| 530 | } | ||||||||||||||||||||
| 531 | |||||||||||||||||||||
| 532 | # Return values as a list in list context and as a | ||||||||||||||||||||
| 533 | # list reference in scalar context. | ||||||||||||||||||||
| 534 | 3 | ▶ | if (wantarray) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 535 | 3 | return @rtn; | |||||||||||||||||||
| 536 | } else { | ||||||||||||||||||||
| 537 | 0 | return \@rtn; | |||||||||||||||||||
| 538 | } | ||||||||||||||||||||
| 539 | 1 | }); | |||||||||||||||||||
| 540 | |||||||||||||||||||||
| 541 | =item I<inst_var>_push($key, @args) | ||||||||||||||||||||
| 542 | |||||||||||||||||||||
| 543 | Push C<@args> on the list stored under C<$key>. | ||||||||||||||||||||
| 544 | |||||||||||||||||||||
| 545 | =cut | ||||||||||||||||||||
| 546 | |||||||||||||||||||||
| 547 | define_method($dest_class, "${name}_push", | ||||||||||||||||||||
| 548 | 7 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 549 | 7 | my ($self, $key, @args) = @_; | |||||||||||||||||||
| 550 | 7 | push @{$self->{$name}{$key}}, @args; | |||||||||||||||||||
| 551 | 1 | }); | |||||||||||||||||||
| 552 | |||||||||||||||||||||
| 553 | =item I<inst_var>_keys() | ||||||||||||||||||||
| 554 | |||||||||||||||||||||
| 555 | Return a list of all the keys in the hash. | ||||||||||||||||||||
| 556 | |||||||||||||||||||||
| 557 | =back | ||||||||||||||||||||
| 558 | |||||||||||||||||||||
| 559 | =cut | ||||||||||||||||||||
| 560 | |||||||||||||||||||||
| 561 | define_method($dest_class, "${name}_keys", | ||||||||||||||||||||
| 562 | 1 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 563 | 1 | my $self = shift; | |||||||||||||||||||
| 564 | 1 | keys %{$self->{$name}}; | |||||||||||||||||||
| 565 | 1 | }); | |||||||||||||||||||
| 566 | } | ||||||||||||||||||||
| 567 | |||||||||||||||||||||
| 568 | #---------------------------------------------------------------- | ||||||||||||||||||||
| 569 | |||||||||||||||||||||
| 570 | =head2 List | ||||||||||||||||||||
| 571 | |||||||||||||||||||||
| 572 | Define methods in the destination class for a list-valued instance | ||||||||||||||||||||
| 573 | variable called I<inst_var> as follows: | ||||||||||||||||||||
| 574 | |||||||||||||||||||||
| 575 | list => 'inst_var' | ||||||||||||||||||||
| 576 | |||||||||||||||||||||
| 577 | This specification defines the following methods in the destination | ||||||||||||||||||||
| 578 | class: | ||||||||||||||||||||
| 579 | |||||||||||||||||||||
| 580 | =over | ||||||||||||||||||||
| 581 | |||||||||||||||||||||
| 582 | =cut | ||||||||||||||||||||
| 583 | |||||||||||||||||||||
| 584 | 4 | ▶ | sub define_list_methods { | ||||||||||||||||||
|
Subroutine
define_list_methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 585 | 4 | my ($dest_class, $name) = @_; | |||||||||||||||||||
| 586 | |||||||||||||||||||||
| 587 | 4 | _add_methodist_info($dest_class, attributes => [ list => $name ]); | |||||||||||||||||||
| 588 | |||||||||||||||||||||
| 589 | =item I<inst_var>(...) | ||||||||||||||||||||
| 590 | |||||||||||||||||||||
| 591 | The method named the same as the instance variable is the setter and | ||||||||||||||||||||
| 592 | getter. Its behavior depends on the number of arguments with which it | ||||||||||||||||||||
| 593 | is invoked. | ||||||||||||||||||||
| 594 | |||||||||||||||||||||
| 595 | When called with no arguments, return the contents of the list (when | ||||||||||||||||||||
| 596 | called in array context) or a reference to the list (when called in | ||||||||||||||||||||
| 597 | scalar context). | ||||||||||||||||||||
| 598 | |||||||||||||||||||||
| 599 | When called with one argument that is a I<reference> to a list, | ||||||||||||||||||||
| 600 | I<replace> the contents of the list with the contents of the | ||||||||||||||||||||
| 601 | referenced list. Otherwise, I<replace> the contents of the list with | ||||||||||||||||||||
| 602 | the arguments. | ||||||||||||||||||||
| 603 | |||||||||||||||||||||
| 604 | =cut | ||||||||||||||||||||
| 605 | |||||||||||||||||||||
| 606 | define_method($dest_class, $name, | ||||||||||||||||||||
| 607 | 29 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||
| 608 | 29 | my ($self, @args) = @_; | |||||||||||||||||||
| 609 | |||||||||||||||||||||
| 610 | 29 | ▶ | if (@args == 0) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 611 | # Called without arguments. Return the contents | ||||||||||||||||||||
| 612 | # of the list. as a list in list context and as a | ||||||||||||||||||||
| 613 | # list reference in scalar context. | ||||||||||||||||||||
| 614 | 16 | ▶ | if (wantarray) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 615 | 12 | return @{$self->{$name}}; | |||||||||||||||||||
| 616 | } else { | ||||||||||||||||||||
| 617 | 4 | return $self->{$name}; | |||||||||||||||||||
| 618 | } | ||||||||||||||||||||
| 619 | ▶ | } elsif (@args == 1 and ref $args[0] eq 'ARRAY') { | |||||||||||||||||||
|
Condition100%3/3
@args == 1 and ref $args[0] eq "ARRAY"
MC/DC100%2/2
@args == 1 and ref $args[0] eq "ARRAY"
@args == 1
ref $args[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @args == 1B= ref $args[0] eq "ARRAY"
| |||||||||||||||||||||
| 620 | # Called with reference to a list. Replace the | ||||||||||||||||||||
| 621 | # contents of the list with the elements | ||||||||||||||||||||
| 622 | # referenced. | ||||||||||||||||||||
| 623 | 1 | $self->{$name} = $args[0]; | |||||||||||||||||||
| 624 | } else { | ||||||||||||||||||||
| 625 | ## Called with multiple arguments. Replace the | ||||||||||||||||||||
| 626 | ## contents of the list with those arguments | ||||||||||||||||||||
| 627 | 12 | $self->{$name} = \@args; | |||||||||||||||||||
| 628 | } | ||||||||||||||||||||
| 629 | 4 | }); | |||||||||||||||||||
| 630 | |||||||||||||||||||||
| 631 | =item push_I<inst_var>(@args) | ||||||||||||||||||||
| 632 | |||||||||||||||||||||
| 633 | Given a list of values, push them on to the end of the list. Return | ||||||||||||||||||||
| 634 | the new number of list elements. | ||||||||||||||||||||
| 635 | |||||||||||||||||||||
| 636 | =cut | ||||||||||||||||||||
| 637 | |||||||||||||||||||||
| 638 | define_method($dest_class, "push_$name", | ||||||||||||||||||||
| 639 | 3 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 640 | 3 | my ($self, @args) = @_; | |||||||||||||||||||
| 641 | 3 | push @{$self->{$name}}, @args; | |||||||||||||||||||
| 642 | 3 | scalar @{$self->{$name}}; | |||||||||||||||||||
| 643 | 4 | }); | |||||||||||||||||||
| 644 | |||||||||||||||||||||
| 645 | =item push_I<inst_var>_if_new(@args) | ||||||||||||||||||||
| 646 | |||||||||||||||||||||
| 647 | Given a list of values, push them on to the end of the list unless | ||||||||||||||||||||
| 648 | they already exist on he list. Returns the new number of list | ||||||||||||||||||||
| 649 | elements. Note that this method uses Perl's C<grep> function and so | ||||||||||||||||||||
| 650 | is only suitable for short lists. | ||||||||||||||||||||
| 651 | |||||||||||||||||||||
| 652 | =cut | ||||||||||||||||||||
| 653 | |||||||||||||||||||||
| 654 | define_method($dest_class, "push_${name}_if_new", | ||||||||||||||||||||
| 655 | 9 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 656 | 9 | my ($self, @args) = @_; | |||||||||||||||||||
| 657 | 9 | foreach my $arg (@args) { | |||||||||||||||||||
| 658 | 6 | push @{$self->{$name}}, $arg | |||||||||||||||||||
| 659 | 10 | ▶ | unless grep($_ eq $arg, @{$self->{$name}}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 660 | } | ||||||||||||||||||||
| 661 | 9 | scalar @{$self->{$name}}; | |||||||||||||||||||
| 662 | 4 | }); | |||||||||||||||||||
| 663 | |||||||||||||||||||||
| 664 | =item pop_I<inst_var> | ||||||||||||||||||||
| 665 | |||||||||||||||||||||
| 666 | Pop a single value from the end of the list and return it. | ||||||||||||||||||||
| 667 | |||||||||||||||||||||
| 668 | =cut | ||||||||||||||||||||
| 669 | |||||||||||||||||||||
| 670 | define_method($dest_class, "pop_$name", | ||||||||||||||||||||
| 671 | 3 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 672 | 3 | my $self = shift; | |||||||||||||||||||
| 673 | 3 | pop @{$self->{$name}}; | |||||||||||||||||||
| 674 | 4 | }); | |||||||||||||||||||
| 675 | |||||||||||||||||||||
| 676 | =item unshift_I<inst_var>(@args) | ||||||||||||||||||||
| 677 | |||||||||||||||||||||
| 678 | Given a list of values, unshift them on to the front of the list. | ||||||||||||||||||||
| 679 | Return the new number of list elements. | ||||||||||||||||||||
| 680 | |||||||||||||||||||||
| 681 | =cut | ||||||||||||||||||||
| 682 | |||||||||||||||||||||
| 683 | define_method($dest_class, "unshift_$name", | ||||||||||||||||||||
| 684 | 1 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 685 | 1 | my ($self, @args) = @_; | |||||||||||||||||||
| 686 | 1 | unshift @{$self->{$name}}, @args; | |||||||||||||||||||
| 687 | 1 | scalar @{$self->{$name}}; | |||||||||||||||||||
| 688 | 4 | }); | |||||||||||||||||||
| 689 | |||||||||||||||||||||
| 690 | =item shift_I<inst_var>() | ||||||||||||||||||||
| 691 | |||||||||||||||||||||
| 692 | Shift a single value from the front of the list and return it. | ||||||||||||||||||||
| 693 | |||||||||||||||||||||
| 694 | =cut | ||||||||||||||||||||
| 695 | |||||||||||||||||||||
| 696 | define_method($dest_class, "shift_$name", | ||||||||||||||||||||
| 697 | 4 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 698 | 4 | my $self = shift; | |||||||||||||||||||
| 699 | 4 | shift @{$self->{$name}}; | |||||||||||||||||||
| 700 | 4 | }); | |||||||||||||||||||
| 701 | |||||||||||||||||||||
| 702 | =item first_of_I<inst_var>() | ||||||||||||||||||||
| 703 | |||||||||||||||||||||
| 704 | Return (but do not remove) the first element in the list. If the list | ||||||||||||||||||||
| 705 | is empty, return undef. | ||||||||||||||||||||
| 706 | |||||||||||||||||||||
| 707 | =cut | ||||||||||||||||||||
| 708 | |||||||||||||||||||||
| 709 | define_method($dest_class, "first_of_$name", | ||||||||||||||||||||
| 710 | 3 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 711 | 3 | my $self = shift; | |||||||||||||||||||
| 712 | 3 | ▶ | @{$self->{$name}} ? $self->{$name}[0] : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 713 | 4 | }); | |||||||||||||||||||
| 714 | |||||||||||||||||||||
| 715 | =item last_of_I<inst_var>() | ||||||||||||||||||||
| 716 | |||||||||||||||||||||
| 717 | Return (but do not remove) the last element in the list. If the list | ||||||||||||||||||||
| 718 | is empty, return undef. | ||||||||||||||||||||
| 719 | |||||||||||||||||||||
| 720 | =cut | ||||||||||||||||||||
| 721 | |||||||||||||||||||||
| 722 | define_method($dest_class, "last_of_$name", | ||||||||||||||||||||
| 723 | 3 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 724 | 3 | my $self = shift; | |||||||||||||||||||
| 725 | 3 | ▶ | @{$self->{$name}} ? $self->{$name}[-1] : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 726 | 4 | }); | |||||||||||||||||||
| 727 | |||||||||||||||||||||
| 728 | =item count_I<inst_var>() | ||||||||||||||||||||
| 729 | |||||||||||||||||||||
| 730 | Return the number of elements currently on the list. | ||||||||||||||||||||
| 731 | |||||||||||||||||||||
| 732 | =cut | ||||||||||||||||||||
| 733 | |||||||||||||||||||||
| 734 | define_method($dest_class, "count_$name", | ||||||||||||||||||||
| 735 | 14 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 736 | 14 | my $self = shift; | |||||||||||||||||||
| 737 | 14 | scalar @{$self->{$name}}; | |||||||||||||||||||
| 738 | 4 | }); | |||||||||||||||||||
| 739 | |||||||||||||||||||||
| 740 | =item clear_I<inst_var>() | ||||||||||||||||||||
| 741 | |||||||||||||||||||||
| 742 | Delete the contents of the list. | ||||||||||||||||||||
| 743 | |||||||||||||||||||||
| 744 | =cut | ||||||||||||||||||||
| 745 | |||||||||||||||||||||
| 746 | define_method($dest_class, "clear_$name", | ||||||||||||||||||||
| 747 | 5 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 748 | 5 | my $self = shift; | |||||||||||||||||||
| 749 | 5 | $self->{$name} = [ ]; | |||||||||||||||||||
| 750 | 4 | }); | |||||||||||||||||||
| 751 | |||||||||||||||||||||
| 752 | =item join_I<inst_var>([$glue]) | ||||||||||||||||||||
| 753 | |||||||||||||||||||||
| 754 | Return the join of the list. The list is not modified. If C<$glue> | ||||||||||||||||||||
| 755 | is defined, join the list with the given glue. Otherwise, join the | ||||||||||||||||||||
| 756 | list with the empty string. | ||||||||||||||||||||
| 757 | |||||||||||||||||||||
| 758 | =cut | ||||||||||||||||||||
| 759 | |||||||||||||||||||||
| 760 | define_method($dest_class, "join_$name", | ||||||||||||||||||||
| 761 | 12 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 762 | 12 | my ($self, $glue) = @_; | |||||||||||||||||||
| 763 | 12 | ▶ | $glue = '' unless defined $glue; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 764 | 12 | join($glue, @{$self->{$name}}); | |||||||||||||||||||
| 765 | 4 | }); | |||||||||||||||||||
| 766 | |||||||||||||||||||||
| 767 | =item grep_I<inst_var>($re) | ||||||||||||||||||||
| 768 | |||||||||||||||||||||
| 769 | Return the list generated by grepping the list against C<$re>, which | ||||||||||||||||||||
| 770 | must be a compiled regular express (usually using C<qr//>). | ||||||||||||||||||||
| 771 | |||||||||||||||||||||
| 772 | =back | ||||||||||||||||||||
| 773 | |||||||||||||||||||||
| 774 | =cut | ||||||||||||||||||||
| 775 | |||||||||||||||||||||
| 776 | define_method($dest_class, "grep_$name", | ||||||||||||||||||||
| 777 | 5 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 778 | 5 | my ($self, $re) = @_; | |||||||||||||||||||
| 779 | 5 | grep(/$re/, @{$self->{$name}}); | |||||||||||||||||||
| 780 | 4 | }); | |||||||||||||||||||
| 781 | } | ||||||||||||||||||||
| 782 | |||||||||||||||||||||
| 783 | #---------------------------------------------------------------- | ||||||||||||||||||||
| 784 | |||||||||||||||||||||
| 785 | =head2 Object | ||||||||||||||||||||
| 786 | |||||||||||||||||||||
| 787 | Define methods in the destination class for an object-valued instance | ||||||||||||||||||||
| 788 | variable called I<inst_var>. | ||||||||||||||||||||
| 789 | |||||||||||||||||||||
| 790 | For specifications of this form (scalar-valued): | ||||||||||||||||||||
| 791 | |||||||||||||||||||||
| 792 | object => 'inst_var' | ||||||||||||||||||||
| 793 | |||||||||||||||||||||
| 794 | the scalar is used as the name of the instance variable. | ||||||||||||||||||||
| 795 | |||||||||||||||||||||
| 796 | For specifications of this form (hash-reference-valued), the instance | ||||||||||||||||||||
| 797 | variable is defined by attribute-value pairs in the referenced hash: | ||||||||||||||||||||
| 798 | |||||||||||||||||||||
| 799 | object => { name => 'inst_var', | ||||||||||||||||||||
| 800 | class => 'Class::Name', | ||||||||||||||||||||
| 801 | delegate => [ 'method1', 'method2' ] } | ||||||||||||||||||||
| 802 | |||||||||||||||||||||
| 803 | The I<required> C<name> attribute gives the name of the instance | ||||||||||||||||||||
| 804 | variable. | ||||||||||||||||||||
| 805 | |||||||||||||||||||||
| 806 | The I<optional> C<class> attribute gives the name of the class (or one | ||||||||||||||||||||
| 807 | of its superclasses) whose objects can be assigned to this instance | ||||||||||||||||||||
| 808 | variable. Attempting to set the instance variable to instances of | ||||||||||||||||||||
| 809 | other classes throws an exception. | ||||||||||||||||||||
| 810 | |||||||||||||||||||||
| 811 | The I<optional> C<delegate> attribute takes a reference to a list of | ||||||||||||||||||||
| 812 | method names. These methods are defined in the destination class as | ||||||||||||||||||||
| 813 | methods that invoke the identically-named methods on the object | ||||||||||||||||||||
| 814 | referenced by the instance variable. | ||||||||||||||||||||
| 815 | |||||||||||||||||||||
| 816 | This specification defines the following methods in the destination | ||||||||||||||||||||
| 817 | class: | ||||||||||||||||||||
| 818 | |||||||||||||||||||||
| 819 | =over | ||||||||||||||||||||
| 820 | |||||||||||||||||||||
| 821 | =cut | ||||||||||||||||||||
| 822 | |||||||||||||||||||||
| 823 | 5 | ▶ | sub define_object_methods { | ||||||||||||||||||
|
Subroutine
define_object_methods: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||
| 824 | 5 | my ($dest_class, $spec) = @_; | |||||||||||||||||||
| 825 | |||||||||||||||||||||
| 826 | 5 | my $name = undef; | |||||||||||||||||||
| 827 | 5 | my $required_class = undef; | |||||||||||||||||||
| 828 | 5 | my @delegate; | |||||||||||||||||||
| 829 | |||||||||||||||||||||
| 830 | 5 | ▶ | if (ref($spec) eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 831 | 3 | $name = $spec->{name}; | |||||||||||||||||||
| 832 | 3 | $required_class = $spec->{class}; | |||||||||||||||||||
| 833 | 3 | ▶ | @delegate = @{$spec->{delegate}} if exists $spec->{delegate}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 834 | } else { | ||||||||||||||||||||
| 835 | 2 | $name = $spec; | |||||||||||||||||||
| 836 | } | ||||||||||||||||||||
| 837 | |||||||||||||||||||||
| 838 | 5 | ▶ | confess "No name specified" unless defined $name; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 839 | |||||||||||||||||||||
| 840 | =item I<inst_var>(...) | ||||||||||||||||||||
| 841 | |||||||||||||||||||||
| 842 | The method named the same as the instance variable is its getter and | ||||||||||||||||||||
| 843 | setter. When called with an argument, the instance variable is set to | ||||||||||||||||||||
| 844 | that value. If the specification includes a C<class> attribute, the | ||||||||||||||||||||
| 845 | argument must be an object of that class or its subclasses (tested | ||||||||||||||||||||
| 846 | using Perl's C<isa> built-in). Returns the value of the instance | ||||||||||||||||||||
| 847 | variable (which may have just been set). | ||||||||||||||||||||
| 848 | |||||||||||||||||||||
| 849 | =cut | ||||||||||||||||||||
| 850 | |||||||||||||||||||||
| 851 | define_method($dest_class, $name, | ||||||||||||||||||||
| 852 | 12 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||
| 853 | 12 | my ($self, $arg) = @_; | |||||||||||||||||||
| 854 | |||||||||||||||||||||
| 855 | 12 | ▶ | if (defined $arg) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 856 | # Called with an argument. | ||||||||||||||||||||
| 857 | 7 | ▶ | confess "Must pass an object as value" unless ref $arg; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 858 | 7 | ▶ | if ($required_class) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 859 | # The 'class' attribute was supplied; the | ||||||||||||||||||||
| 860 | # argument must be of the specified class. | ||||||||||||||||||||
| 861 | 4 | my $arg_class = ref $arg; | |||||||||||||||||||
| 862 | 4 | ▶ | confess "Requires '$required_class', not '$arg_class'" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 863 | unless $arg->isa($required_class); | ||||||||||||||||||||
| 864 | } | ||||||||||||||||||||
| 865 | # Assign the value to the argument. | ||||||||||||||||||||
| 866 | 6 | $self->{$name} = $arg; | |||||||||||||||||||
| 867 | } | ||||||||||||||||||||
| 868 | |||||||||||||||||||||
| 869 | # Return the current object (whether arguments or not). | ||||||||||||||||||||
| 870 | 11 | $self->{$name}; | |||||||||||||||||||
| 871 | 5 | }); | |||||||||||||||||||
| 872 | |||||||||||||||||||||
| 873 | # Created delegates, if any. | ||||||||||||||||||||
| 874 | 5 | foreach my $delegate (@delegate) { | |||||||||||||||||||
| 875 | define_method($dest_class, $delegate, | ||||||||||||||||||||
| 876 | 2 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 877 | 2 | my ($self, @args) = @_; | |||||||||||||||||||
| 878 | 2 | $self->{$name}->$delegate(@args); | |||||||||||||||||||
| 879 | 2 | }); | |||||||||||||||||||
| 880 | } | ||||||||||||||||||||
| 881 | |||||||||||||||||||||
| 882 | =item clear_I<inst_var>() | ||||||||||||||||||||
| 883 | |||||||||||||||||||||
| 884 | Undefine the object instance variable. This method is so named to | ||||||||||||||||||||
| 885 | make it consistent with other methods defined by this module. | ||||||||||||||||||||
| 886 | |||||||||||||||||||||
| 887 | =back | ||||||||||||||||||||
| 888 | |||||||||||||||||||||
| 889 | =cut | ||||||||||||||||||||
| 890 | |||||||||||||||||||||
| 891 | define_method($dest_class, "clear_$name", | ||||||||||||||||||||
| 892 | 1 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 893 | 1 | my $self = shift; | |||||||||||||||||||
| 894 | 1 | $self->{$name} = undef; | |||||||||||||||||||
| 895 | 5 | }); | |||||||||||||||||||
| 896 | } | ||||||||||||||||||||
| 897 | |||||||||||||||||||||
| 898 | #---------------------------------------------------------------- | ||||||||||||||||||||
| 899 | |||||||||||||||||||||
| 900 | =head2 Scalar | ||||||||||||||||||||
| 901 | |||||||||||||||||||||
| 902 | Define methods in the destination class for a scalar-valued instance | ||||||||||||||||||||
| 903 | variable called I<inst_var> as follows: | ||||||||||||||||||||
| 904 | |||||||||||||||||||||
| 905 | scalar => 'inst_var' | ||||||||||||||||||||
| 906 | |||||||||||||||||||||
| 907 | Alternatively, you may supply a hash reference as the argument to the | ||||||||||||||||||||
| 908 | C<scalar> specification as follows: | ||||||||||||||||||||
| 909 | |||||||||||||||||||||
| 910 | scalar => { name => 'inst_var', default => 42 } | ||||||||||||||||||||
| 911 | |||||||||||||||||||||
| 912 | In this case, the I<required> C<name> attribute gives the name of the | ||||||||||||||||||||
| 913 | scalar in the destination class. The I<optional> C<default> attribute | ||||||||||||||||||||
| 914 | supplies an initial value for the scalar in the destination class. | ||||||||||||||||||||
| 915 | |||||||||||||||||||||
| 916 | This specification defines the following methods in the destination | ||||||||||||||||||||
| 917 | class: | ||||||||||||||||||||
| 918 | |||||||||||||||||||||
| 919 | =over | ||||||||||||||||||||
| 920 | |||||||||||||||||||||
| 921 | =cut | ||||||||||||||||||||
| 922 | |||||||||||||||||||||
| 923 | 5 | ▶ | sub define_scalar_methods { | ||||||||||||||||||
|
Subroutine
define_scalar_methods: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 924 | 5 | my ($dest_class, $spec) = @_; | |||||||||||||||||||
| 925 | |||||||||||||||||||||
| 926 | 5 | my $name = undef; | |||||||||||||||||||
| 927 | |||||||||||||||||||||
| 928 | 5 | ▶ | if (ref($spec) eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 929 | 1 | $name = $spec->{name}; | |||||||||||||||||||
| 930 | |||||||||||||||||||||
| 931 | 1 | ▶ | if (defined $spec->{default}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 932 | _add_methodist_info($dest_class, | ||||||||||||||||||||
| 933 | 1 | default => [ $name => $spec->{default} ]); | |||||||||||||||||||
| 934 | } | ||||||||||||||||||||
| 935 | } else { | ||||||||||||||||||||
| 936 | 4 | $name = $spec; | |||||||||||||||||||
| 937 | } | ||||||||||||||||||||
| 938 | |||||||||||||||||||||
| 939 | 5 | _add_methodist_info($dest_class, attributes => [ scalar => $name ]); | |||||||||||||||||||
| 940 | |||||||||||||||||||||
| 941 | =item I<inst_var>(...) | ||||||||||||||||||||
| 942 | |||||||||||||||||||||
| 943 | The method named the same as the instance variable is the setter and | ||||||||||||||||||||
| 944 | getter. If called with no arguments, returns the current value of the | ||||||||||||||||||||
| 945 | scalar. If called with an argument, the scalar is assigned that | ||||||||||||||||||||
| 946 | value. | ||||||||||||||||||||
| 947 | |||||||||||||||||||||
| 948 | =cut | ||||||||||||||||||||
| 949 | |||||||||||||||||||||
| 950 | define_method($dest_class, $name, | ||||||||||||||||||||
| 951 | 22 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 952 | 22 | my ($self, $arg) = @_; | |||||||||||||||||||
| 953 | 22 | ▶ | if (defined $arg) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 954 | 7 | return $self->{$name} = $arg; | |||||||||||||||||||
| 955 | } | ||||||||||||||||||||
| 956 | 15 | $self->{$name}; | |||||||||||||||||||
| 957 | 5 | }); | |||||||||||||||||||
| 958 | |||||||||||||||||||||
| 959 | =item clear_I<inst_var>() | ||||||||||||||||||||
| 960 | |||||||||||||||||||||
| 961 | Undefine the instance variable. This method is so named to make it | ||||||||||||||||||||
| 962 | consistent with other methods defined by this module. | ||||||||||||||||||||
| 963 | |||||||||||||||||||||
| 964 | =cut | ||||||||||||||||||||
| 965 | |||||||||||||||||||||
| 966 | define_method($dest_class, "clear_$name", | ||||||||||||||||||||
| 967 | 1 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 968 | 1 | my $self = shift; | |||||||||||||||||||
| 969 | 1 | $self->{$name} = undef; | |||||||||||||||||||
| 970 | 5 | }); | |||||||||||||||||||
| 971 | |||||||||||||||||||||
| 972 | =item add_to_I<inst_var>($val) | ||||||||||||||||||||
| 973 | |||||||||||||||||||||
| 974 | Add numeric $val to the current contents of the scalar. | ||||||||||||||||||||
| 975 | |||||||||||||||||||||
| 976 | =cut | ||||||||||||||||||||
| 977 | |||||||||||||||||||||
| 978 | define_method($dest_class, "add_to_$name", | ||||||||||||||||||||
| 979 | 2 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 980 | 2 | my ($self, $val) = @_; | |||||||||||||||||||
| 981 | 2 | $self->{$name} += $val; | |||||||||||||||||||
| 982 | 5 | }); | |||||||||||||||||||
| 983 | |||||||||||||||||||||
| 984 | =item inc_I<inst_var>() | ||||||||||||||||||||
| 985 | |||||||||||||||||||||
| 986 | Increment the scalar by one and return its new value. | ||||||||||||||||||||
| 987 | |||||||||||||||||||||
| 988 | =cut | ||||||||||||||||||||
| 989 | |||||||||||||||||||||
| 990 | define_method($dest_class, "inc_$name", | ||||||||||||||||||||
| 991 | 2 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 992 | 2 | my $self = shift; | |||||||||||||||||||
| 993 | 2 | $self->{$name}++; | |||||||||||||||||||
| 994 | 5 | }); | |||||||||||||||||||
| 995 | |||||||||||||||||||||
| 996 | =item dec_I<inst_var>() | ||||||||||||||||||||
| 997 | |||||||||||||||||||||
| 998 | Decrement the scalar by one and return its new value. | ||||||||||||||||||||
| 999 | |||||||||||||||||||||
| 1000 | =cut | ||||||||||||||||||||
| 1001 | |||||||||||||||||||||
| 1002 | define_method($dest_class, "dec_$name", | ||||||||||||||||||||
| 1003 | 3 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 1004 | 3 | my $self = shift; | |||||||||||||||||||
| 1005 | 3 | $self->{$name}--; | |||||||||||||||||||
| 1006 | 5 | }); | |||||||||||||||||||
| 1007 | |||||||||||||||||||||
| 1008 | =item append_to_I<inst_var>($val) | ||||||||||||||||||||
| 1009 | |||||||||||||||||||||
| 1010 | Append string $val to the current contents of the scalar. | ||||||||||||||||||||
| 1011 | |||||||||||||||||||||
| 1012 | =back | ||||||||||||||||||||
| 1013 | |||||||||||||||||||||
| 1014 | =cut | ||||||||||||||||||||
| 1015 | |||||||||||||||||||||
| 1016 | define_method($dest_class, "append_to_$name", | ||||||||||||||||||||
| 1017 | 1 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 1018 | 1 | my ($self, $val) = @_; | |||||||||||||||||||
| 1019 | 1 | $self->{$name} .= $val; | |||||||||||||||||||
| 1020 | 5 | }); | |||||||||||||||||||
| 1021 | } | ||||||||||||||||||||
| 1022 | |||||||||||||||||||||
| 1023 | #---------------------------------------------------------------- | ||||||||||||||||||||
| 1024 | |||||||||||||||||||||
| 1025 | =head2 Scalars | ||||||||||||||||||||
| 1026 | |||||||||||||||||||||
| 1027 | Define methods in the destination class for multiple scalar-valued | ||||||||||||||||||||
| 1028 | instance variables as follows: | ||||||||||||||||||||
| 1029 | |||||||||||||||||||||
| 1030 | scalars => [ 'alpha', 'beta', 'gamma' ] | ||||||||||||||||||||
| 1031 | |||||||||||||||||||||
| 1032 | This specification is a convenience for defining multiple | ||||||||||||||||||||
| 1033 | scalar-valued instance variables. It takes a reference to a list of | ||||||||||||||||||||
| 1034 | names and invokes the C<scalar> specification for each. Hence, the | ||||||||||||||||||||
| 1035 | above specification is entirely equivalent to this one: | ||||||||||||||||||||
| 1036 | |||||||||||||||||||||
| 1037 | scalar => 'alpha', | ||||||||||||||||||||
| 1038 | scalar => 'beta', | ||||||||||||||||||||
| 1039 | scalar => 'gamma' | ||||||||||||||||||||
| 1040 | |||||||||||||||||||||
| 1041 | Note that there is no way to define a default value for each scalar in | ||||||||||||||||||||
| 1042 | the C<scalars> construct; use multiple C<scalar> specifications | ||||||||||||||||||||
| 1043 | instead. | ||||||||||||||||||||
| 1044 | |||||||||||||||||||||
| 1045 | =cut | ||||||||||||||||||||
| 1046 | |||||||||||||||||||||
| 1047 | #---------------------------------------------------------------- | ||||||||||||||||||||
| 1048 | |||||||||||||||||||||
| 1049 | =head2 Utility | ||||||||||||||||||||
| 1050 | |||||||||||||||||||||
| 1051 | Define various utility methods. | ||||||||||||||||||||
| 1052 | |||||||||||||||||||||
| 1053 | =over | ||||||||||||||||||||
| 1054 | |||||||||||||||||||||
| 1055 | =cut | ||||||||||||||||||||
| 1056 | |||||||||||||||||||||
| 1057 | { | ||||||||||||||||||||
| 1058 | 9 | ▶ | use Class::ISA; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 1059 | |||||||||||||||||||||
| 1060 | # Cache previous results from self_and_super_path, which takes a | ||||||||||||||||||||
| 1061 | # fair amount of time and is called a *lot* because it's in beget(). | ||||||||||||||||||||
| 1062 | 9 | my %self_and_super; | |||||||||||||||||||
| 1063 | |||||||||||||||||||||
| 1064 | # Invoke self_and_super_path (which returns the ordered list of | ||||||||||||||||||||
| 1065 | # names of classes that Perl would search in order to find a | ||||||||||||||||||||
| 1066 | # method). Cache and return results. | ||||||||||||||||||||
| 1067 | 16 | ▶ | sub _self_and_super { | ||||||||||||||||||
|
Subroutine
_self_and_super: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 1068 | 16 | my $class = shift; | |||||||||||||||||||
| 1069 | |||||||||||||||||||||
| 1070 | 16 | my $rtn = undef; | |||||||||||||||||||
| 1071 | 16 | ▶ | if (exists $self_and_super{$class}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 1072 | 6 | $rtn = $self_and_super{$class}; | |||||||||||||||||||
| 1073 | } else { | ||||||||||||||||||||
| 1074 | 10 | my @self_and_super = Class::ISA::self_and_super_path($class); | |||||||||||||||||||
| 1075 | 10 | $rtn = $self_and_super{$class} = \@self_and_super; | |||||||||||||||||||
| 1076 | } | ||||||||||||||||||||
| 1077 | |||||||||||||||||||||
| 1078 | 16 | return $rtn; | |||||||||||||||||||
| 1079 | } | ||||||||||||||||||||
| 1080 | } | ||||||||||||||||||||
| 1081 | |||||||||||||||||||||
| 1082 | 9 | ▶ | sub define_utility_methods { | ||||||||||||||||||
|
Subroutine
define_utility_methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 1083 | 12 | my $dest_class = shift; | |||||||||||||||||||
| 1084 | |||||||||||||||||||||
| 1085 | define_method($dest_class, 'beget', | ||||||||||||||||||||
| 1086 | 16 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 1087 | 16 | my ($dest_class, %initializers) = @_; | |||||||||||||||||||
| 1088 | |||||||||||||||||||||
| 1089 | 16 | my $self = bless { }, $dest_class; | |||||||||||||||||||
| 1090 | 16 | $self->equip(%initializers); | |||||||||||||||||||
| 1091 | 12 | }); | |||||||||||||||||||
| 1092 | |||||||||||||||||||||
| 1093 | define_method($dest_class, 'equip', | ||||||||||||||||||||
| 1094 | 16 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.0
| |||||||||||||||||||||
| 1095 | 16 | my ($self, %initializers) = @_; | |||||||||||||||||||
| 1096 | |||||||||||||||||||||
| 1097 | 16 | foreach my $class (@{_self_and_super(ref $self)}) { | |||||||||||||||||||
| 1098 | 22 | ▶ | next unless _has_methodist_info($class); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 1099 | |||||||||||||||||||||
| 1100 | 15 | foreach my $pair (_get_methodist_info($class, | |||||||||||||||||||
| 1101 | 'attributes')) { | ||||||||||||||||||||
| 1102 | 29 | my ($type, $name) = @$pair; | |||||||||||||||||||
| 1103 | SWITCH: | ||||||||||||||||||||
| 1104 | 29 | for ($type) { | |||||||||||||||||||
| 1105 | 29 | ▶ | /scalar|enum/ and do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 1106 | 10 | $self->{$name} = undef; | |||||||||||||||||||
| 1107 | 10 | last SWITCH; | |||||||||||||||||||
| 1108 | }; | ||||||||||||||||||||
| 1109 | 19 | ▶ | /list/ and do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 1110 | 10 | $self->{$name} = [ ]; | |||||||||||||||||||
| 1111 | 10 | last SWITCH; | |||||||||||||||||||
| 1112 | }; | ||||||||||||||||||||
| 1113 | 9 | ▶ | /hash/ and do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 1114 | 9 | $self->{$name} = { }; | |||||||||||||||||||
| 1115 | 9 | last SWITCH; | |||||||||||||||||||
| 1116 | }; | ||||||||||||||||||||
| 1117 | 0 | confess "Invalid type '$type'"; | |||||||||||||||||||
| 1118 | } | ||||||||||||||||||||
| 1119 | } | ||||||||||||||||||||
| 1120 | |||||||||||||||||||||
| 1121 | 15 | foreach my $pair (_get_methodist_info($class, | |||||||||||||||||||
| 1122 | 'default')) { | ||||||||||||||||||||
| 1123 | 2 | my ($name, $default) = @$pair; | |||||||||||||||||||
| 1124 | 2 | $self->{$name} = $default; | |||||||||||||||||||
| 1125 | } | ||||||||||||||||||||
| 1126 | } | ||||||||||||||||||||
| 1127 | |||||||||||||||||||||
| 1128 | 16 | ▶ | while (my ($key, $value) = each %initializers) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 1129 | 1 | $self->$key($value); | |||||||||||||||||||
| 1130 | } | ||||||||||||||||||||
| 1131 | |||||||||||||||||||||
| 1132 | 16 | return $self; | |||||||||||||||||||
| 1133 | 12 | }); | |||||||||||||||||||
| 1134 | |||||||||||||||||||||
| 1135 | 9 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 1136 | 12 | $Data::Dumper::Indent = 1; | |||||||||||||||||||
| 1137 | |||||||||||||||||||||
| 1138 | =item toString() | ||||||||||||||||||||
| 1139 | |||||||||||||||||||||
| 1140 | Define a method to convert an object to a string using | ||||||||||||||||||||
| 1141 | C<Data::Dumper>. | ||||||||||||||||||||
| 1142 | |||||||||||||||||||||
| 1143 | =cut | ||||||||||||||||||||
| 1144 | |||||||||||||||||||||
| 1145 | define_method($dest_class, 'toString', | ||||||||||||||||||||
| 1146 | 0 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 1147 | 0 | my $self = shift; | |||||||||||||||||||
| 1148 | 0 | Data::Dumper->Dump([ $self ], [ ref $self ]); | |||||||||||||||||||
| 1149 | 12 | }); | |||||||||||||||||||
| 1150 | |||||||||||||||||||||
| 1151 | =item attributes_as_string(@attributes) | ||||||||||||||||||||
| 1152 | |||||||||||||||||||||
| 1153 | Return a string representation of the object, including attribute | ||||||||||||||||||||
| 1154 | name-value pairs for attributes named in parameter list. | ||||||||||||||||||||
| 1155 | |||||||||||||||||||||
| 1156 | =cut | ||||||||||||||||||||
| 1157 | |||||||||||||||||||||
| 1158 | 3 | ▶ | sub ansi_magenta { "\e[35m" } | ||||||||||||||||||
|
Subroutine
ansi_magenta: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 1159 | 0 | ▶ | sub ansi_underline { "\e[4m" } | ||||||||||||||||||
|
Subroutine
ansi_underline: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 1160 | 3 | ▶ | sub ansi_reset { "\e[0m" } | ||||||||||||||||||
|
Subroutine
ansi_reset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 1161 | |||||||||||||||||||||
| 1162 | define_method($dest_class, 'attributes_as_string', | ||||||||||||||||||||
| 1163 | 3 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.2
| |||||||||||||||||||||
| 1164 | 3 | my ($self, @attributes) = @_; | |||||||||||||||||||
| 1165 | 3 | my @pairs; | |||||||||||||||||||
| 1166 | 3 | foreach my $attribute (@attributes) { | |||||||||||||||||||
| 1167 | 3 | ▶ | my $value = $self->{$attribute} || 'UNDEF'; | ||||||||||||||||||
|
Condition50%1/2
$self->{$attribute} || 'UNDEF'
MC/DC0%0/1
$self->{$attribute} || 'UNDEF'
$self->{$attribute}
Truth table50%1/2
A
A= $self->{$attribute}
| |||||||||||||||||||||
| 1168 | |||||||||||||||||||||
| 1169 | 3 | ▶ | if (ref($value) eq 'ARRAY') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 1170 | 1 | $value = sprintf('[%s]', join(',', @$value)); | |||||||||||||||||||
| 1171 | |||||||||||||||||||||
| 1172 | } elsif (ref($value) eq 'HASH') { | ||||||||||||||||||||
| 1173 | my @contents = | ||||||||||||||||||||
| 1174 | 2 | ▶ | map { sprintf("%s=%s", $_, $value->{$_} || 'UNDEF') } | ||||||||||||||||||
|
Condition50%1/2
$value->{$_} || 'UNDEF'
MC/DC0%0/1
$value->{$_} || 'UNDEF'
$value->{$_}
Truth table50%1/2
A
A= $value->{$_}
| |||||||||||||||||||||
| 1175 | sort keys %$value; | ||||||||||||||||||||
| 1176 | 2 | $value = sprintf('{%s}', join(',', @contents)); | |||||||||||||||||||
| 1177 | } | ||||||||||||||||||||
| 1178 | |||||||||||||||||||||
| 1179 | # Use ANSI colorization for attribute name. | ||||||||||||||||||||
| 1180 | 3 | push @pairs, | |||||||||||||||||||
| 1181 | sprintf('%s%s%s=%s', | ||||||||||||||||||||
| 1182 | ansi_magenta, $attribute, ansi_reset, $value) | ||||||||||||||||||||
| 1183 | } | ||||||||||||||||||||
| 1184 | 3 | sprintf('(%s %s)', ref($self), join(',', @pairs)); | |||||||||||||||||||
| 1185 | 12 | }); | |||||||||||||||||||
| 1186 | |||||||||||||||||||||
| 1187 | =item dump([$msge]) | ||||||||||||||||||||
| 1188 | |||||||||||||||||||||
| 1189 | Define a method to dump an object using C<Data::Dumper>. If C<$msge> | ||||||||||||||||||||
| 1190 | is defined, print it as a brief descriptive message before dumping the | ||||||||||||||||||||
| 1191 | object. These methods are defined on all classes that use | ||||||||||||||||||||
| 1192 | C<Methodist>. | ||||||||||||||||||||
| 1193 | |||||||||||||||||||||
| 1194 | =cut | ||||||||||||||||||||
| 1195 | |||||||||||||||||||||
| 1196 | define_method($dest_class, 'dump', | ||||||||||||||||||||
| 1197 | 0 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 1198 | 0 | my ($self, $msge) = @_; | |||||||||||||||||||
| 1199 | |||||||||||||||||||||
| 1200 | 0 | ▶ | print "==== $msge ====\n" if $msge; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 1201 | 0 | print $self->toString(); | |||||||||||||||||||
| 1202 | 12 | }); | |||||||||||||||||||
| 1203 | } | ||||||||||||||||||||
| 1204 | |||||||||||||||||||||
| 1205 | 9 | 'SDG'; # Return true | |||||||||||||||||||
| 1206 | |||||||||||||||||||||
| 1207 | __END__ | ||||||||||||||||||||