NAME
Class::MakeMethods::Template::Hash - Method interfaces for hash-based objects
SYNOPSIS
package MyObject;
use Class::MakeMethods::Template::Hash (
new => [ 'new' ],
scalar => [ 'foo', 'bar' ]
);
package main;
my $obj = MyObject->new( foo => "Foozle", bar => "Bozzle" );
print $obj->foo();
$obj->bar("Bamboozle");
DESCRIPTION
These meta-methods create and access values within blessed hash objects.
Common Parameters: The following parameters are defined for Hash meta-methods.
- hash_key
-
The hash key to use when retrieving values from each hash instance. Defaults to '*', the name of the meta-method.
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.
Note that this parameter is not portable to the other implementations, such as Static or Flyweight.
Common Behaviors
new
There are several types of hash-based object constructors to choose from.
Each of these methods creates a new blessed hash and returns a reference to it. They differ in how their (optional) arguments are interpreted to set initial values within the hash, and in how they operate when called as class or instance methods.
See the documentation on Generic:new
for interfaces and behaviors.
scalar
Creates hash-key accessor methods for scalar values.
See the documentation on Generic:scalar
for interfaces and behaviors.
string_index
string_index => [ qw / foo bar baz / ]
Creates string accessor methods for hash objects, like Hash:string above, but also maintains a static hash index in which each object is stored under the value of the field when the slot is set. If an object has a slot set to a value which another object is already set to the object currently set to that value has that slot set to undef and the new object will be put into the hash under that value. (I.e. only one object can have a given key.)
The method find_x is defined which if called with any arguments returns a list of the objects stored under those values in the hash. Called with no arguments, it returns a reference to the hash.
Objects with undefined values are not stored in the index.
Note that to free items from memory, you must clear these values!
find_or_new
'string_index -find_or_new' => [ qw / foo bar baz / ]
Just like string_index except the find_x method is defined to call the new method to create an object if there is no object already stored under any of the keys you give as arguments.
array
Creates hash-key accessor methods for array-ref values.
See the documentation on Generic:array
for interfaces and behaviors.
struct
struct => [ qw / foo bar baz / ];
Creates methods for setting, checking and clearing values which are stored by position in an array. All the slots created with this meta-method are stored in a single array.
The argument to struct should be a string or a reference to an array of strings. For each string meta-method x, it defines two methods: x and clear_x. x returns the value of the x-slot. If called with an argument, it first sets the x-slot to the argument. clear_x sets the slot to undef.
Additionally, struct defines three class method: struct, which returns a list of all of the struct values, struct_fields, which returns a list of all the slots by name, and struct_dump, which returns a hash of the slot-name/slot-value pairs.
hash
Creates hash-key accessor methods for hash-ref values.
See the documentation on Generic:hash
for interfaces and behaviors.
tiedhash
A variant of Hash:hash which initializes the hash by tieing it to a caller-specified package.
See the documentation on Generic:tiedhash
for interfaces and behaviors, and for required additional parameters.
hash_of_arrays
Creates hash-key accessor methods for references to hashes of array-refs.
See the documentation on Generic:hash_of_arrays
for interfaces and behaviors.
object
Creates meta-methods for hash objects which contain references to other objects.
See the documentation on Generic:object
for interfaces, behaviors, and parameters.
array_of_objects
Creates meta-methods for hash objects which contain an array of object references.
See the documentation on Generic:array_of_objects
for interfaces, behaviors, and parameters.
code
Creates meta-methods for hash objects which contain an subroutine reference.
See the documentation on Generic:code
for interfaces, behaviors, and parameters.
bits
Creates hash-key accessor methods for bit-field values.
See the documentation on Generic:bits
for interfaces and behaviors.
The difference between 'Hash:bits' and 'Hash:boolean' is that all flags created with this meta-method are stored in a single vector for space efficiency.
Parameters:
- hash_key
-
Initialized to '*{target_class}__boolean'.
SEE ALSO
Class::MakeMethods