Class::MakeMethods::Template::Generic - Templates for common meta-method types


NAME

Class::MakeMethods::Template::Generic - Templates for common meta-method types


SYNOPSIS

  package MyObject;
  use Class::MakeMethods (
    'Template::Hash:new'       => [ 'new' ],
    'Template::Hash:scalar'    => [ 'foo' ]
    'Template::Static:scalar'  => [ 'bar' ]
  );
  
  package main;
  my $obj = MyObject->new( foo => "Foozle", bar => "Bozzle" );
  print $obj->foo();
  $obj->bar("Bamboozle");


DESCRIPTION

This package provides a variety of abstract interfaces for constructors and accessor methods, which form a common foundation for meta-methods provided by the Hash, Scalar, Flyweight, Static, PackageVar, and ClassVar implementations.

Generally speaking, the Generic meta-methods define calling interfaces and behaviors which are bound to differently scoped data by each of those subclasses.

new Constructor

There are several types of hash-based object constructors to choose from.

Each of these methods creates and returns a reference to a new blessed instance. They differ in how their (optional) arguments are interpreted to set initial values, and in how they operate when called as class or instance methods.

Interfaces: The following interfaces are supported.

-with_values,
Provides the with_values behavior.

-with_init
Provides the with_init behavior.

-with_methods
Provides the with_methods behavior.

-new_and_init
Provides the with_init behavior for *, and the general purpose method_init behavior as an init method.

-copy_with_values
Provides the copy behavior.

Behaviors: The following types of constructor methods are available.

with_values
Creates and blesses a new instance.

If arguments are passed they are included in the instance, otherwise it will be empty.

Returns the new instance.

May be called as a class or instance method.

with_methods
Creates, blesses, and returns a new instance.

The arguments are treated as a hash of method-name/argument-value pairs, with each such pair causing a call $self->name($value).

with_init
Creates and blesses a new instance, then calls a method named init, passing along any arguments that were initially given.

Returns the new instance.

The init() method should be defined in the class declaring these methods.

May be called as a class or instance method.

and_then_init
Creates a new instance using method-name/argument-value pairs, like with_methods, but then calls a method named init before returning the new object. The init method does not receive any arguments.

The init() method should be defined in the class declaring these methods.

instance_with_methods
If called as a class method, creates, blesses, and returns a new instance. If called as an object method, operates on and returns the existing instance.

Accepts name-value pair arguments, or a reference to hash of such pairs, and calls the named method for each with the supplied value as a single argument. (See the Universal method_init behavior for more discussion of this pattern.)

copy_with values
Produce a copy of an instance. Can not be called as a class method.

The copy is a *shallow* copy; any references will be shared by the instance upon which the method is called and the returned newborn.

If a list of key-value pairs is passed as arguments to the method, they are added to the copy, overwriting any values with the same key that may have been copied from the original.

copy_with_methods
Produce a copy of an instance. Can not be called as a class method.

The copy is a *shallow* copy; any references will be shared by the instance upon which the method is called and the returned newborn.

Accepts name-value pair arguments, or a reference to hash of such pairs, and calls the named method on the copy for each with the supplied value as a single argument before the copy is returned.

copy_instance_with_values
If called as a class method, creates, blesses, and returns a new instance. If called as an object method, produces and returns a copy of an instance.

The copy is a *shallow* copy; any references will be shared by the instance upon which the method is called and the returned newborn.

If a list of key-value pairs is passed as arguments to the method, they are added to the copy, overwriting any values with the same key that may have been copied from the original.

copy_instance_with_methods
If called as a class method, creates, blesses, and returns a new instance. If called as an object method, produces and returns a copy of an instance.

The copy is a *shallow* copy; any references will be shared by the instance upon which the method is called and the returned newborn.

Accepts name-value pair arguments, or a reference to hash of such pairs, and calls the named method on the copy for each with the supplied value as a single argument before the copy is returned.

Parameters: The following parameters are supported:

init_method
The name of the method to call after creating a new instance. Defaults to 'init'.

scalar Accessor

A generic scalar-value accessor meta-method which serves as an abstraction for basic ``get_set'' methods and numerous related interfaces

  use Class::MakeMethods -MakerClass => "...", 
        scalar => [ 'foo', 'bar' ];
  ...
  $self->foo( 'my new foo value' );
  print $self->foo();

(Note that while you can use the scalar methods to store references to various data structures, there are other meta-methods defined below that may be more useful for managing references to arrays, hashes, and objects.)

Interfaces: The following calling interfaces are available.

get_set (default)
Provides get_set method for *.

Example: Create method foo, which sets the value of 'foo' for this instance if an argument is passed in, and then returns the value whether or not it's been changed:

  use Class::MakeMethods -MakerClass => "...", 
    scalar => [ 'foo' ];

get_protected_set
Provides an get_set accessor for * that croaks if a new value is passed in from a package that is not a subclass of the declaring one.

get_private_set
Provides an get_set accessor for * that croaks if a new value is passed in from a package other than the declaring one.

read_only
Provides an accessor for * that does not modify its value. (Its initial value would have to be set by some other means.)

eiffel
Provides get behavior as *, and set behavior as set_*.

Example: Create methods bar which returns the value of 'bar' for this instance (takes no arguments), and set_bar, which sets the value of 'bar' (no return):

  use Class::MakeMethods -MakerClass => "...", 
    scalar => [ --eiffel => 'bar' ];

java
Provides get behavior as get*, and set behavior as set*.

Example: Create methods getBaz which returns the value of 'Baz' for this instance (takes no arguments), and setBaz, which sets the value for this instance (no return):

  use Class::MakeMethods -MakerClass => "...", 
    scalar => [ --java => 'Baz' ];

init_and_get
Creates methods which cache their results in a hash key.

Provides the get_init behavior for *, and an delete behavior for clear_*. Specifies default value for init_method parameter of init_*.

with_clear
Provides get_set behavior for *, and a clear_* method.

Behaviors: The following types of accessor methods are available.

get_set
If no argument is provided, returns the value of the current instance. The value defaults to undef.

If an argument is provided, it is stored as the value of the current instance (even if the argument is undef), and that value is returned.

Also available as get_protected_set and get_private_set, which are available for public read-only access, but have access control limitations.

get
Returns the value from the current instance.

set
Sets the value for the current instance. If called with no arguments, the value is set to undef. Does not return a value.

clear
Sets value to undef.

get_set_chain
Like get_set, but if called with an argument, returns the object it was called on. This allows a series of mutators to be called as follows:
  package MyObject;
  use Class::MakeMethods (
    'Template::Hash:scalar --get_set_chain' => 'foo bar baz'
  );
  ...
  
  my $obj = MyObject->new->foo('Foozle');
  $obj->bar("none")->baz("Brazil");
  print $obj->foo, $obj->bar, $obj->baz;

get_set_prev
Like get_set, but if called with an argument, returns the previous value before it was changed to the new one.

get_init
If the value is currently undefined, calls the init_method. Returns the value.

Parameters: The following parameters are supported:

init_method
The name of a method to be called to initialize this meta-method.

Only used by the get_init behavior.

string Accessor

A generic scalar-value accessor meta-method which serves as an abstraction for basic ``get_set'' methods and numerous related interfaces

  use Class::MakeMethods -MakerClass => "...", 
        string => [ 'foo', 'bar' ];
  ...
  $self->foo( 'my new foo value' );
  print $self->foo();

This meta-method extends the scalar meta-method, and supports the same interfaces and parameters.

However, it generally treats values as strings, and can not be used to store references.

Interfaces: In addition to those provided by scalar, the following calling interfaces are available.

-get_concat
Provides the get_concat behavior for *, and a clear_* method.

Example:

  use Class::MakeMethods
    get_concat => { name => 'words', join => ", " };
  $obj->words('foo');
  $obj->words('bar');
  $obj->words() eq 'foo, bar';

Behaviors: In addition to those provided by scalar, the following types of accessor methods are available.

concat
Concatenates the argument value with the existing value.

get_concat
Like get_set except sets do not clear out the original value, but instead concatenate the new value to the existing one.

Parameters: In addition to those provided by scalar, the following parameters are supported.

join
If the join parameter is defined, each time the get_concat behavior is invoked, it will glue its argument onto any existing value with the join string as the separator. The join field is applied between values, not prior to the first or after the last. Defaults to undefined

string_index

  string_index => [ qw / foo bar baz / ]

Creates string accessor methods, like 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.

This is a unique index, so only one object can have a given key. 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.

Objects with undefined values are not stored in the index.

Note that to free items from memory, you must clear these values!

Methods:

Profiles:

number Accessor

A generic scalar-value accessor meta-method which serves as an abstraction for basic ``get_set'' methods and numerous related interfaces

  use Class::MakeMethods -MakerClass => "...", 
        string => [ 'foo', 'bar' ];
  ...
  $self->foo( 23 );
  print $self->foo();

This meta-method extends the scalar meta-method, and supports the same interfaces and parameters.

However, it generally treats values as numbers, and can not be used to store strings or references.

Interfaces: In addition to those provided by scalar, the following calling interfaces are available.

-counter
Provides the numeric get_set behavior for *, and numeric *_incr and *_reset methods.

Behaviors: In addition to those provided by scalar, the following types of accessor methods are available.

get_set
The get_set behavior is similar to the default scalar behavior except that empty values are treated as zero.

increment
If no argument is provided, increments the hash_key value by 1. If an argument is provided, the value is incremented by that amount. Returns the increased value.

clear
Sets the value to zero.

boolean Accessor

A generic scalar-value accessor meta-method which serves as an abstraction for basic ``get_set'' methods and numerous related interfaces

  use Class::MakeMethods -MakerClass => "...", 
        string => [ 'foo', 'bar' ];
  ...
  $self->foo( 1 );
  print $self->foo();
  $self->clear_foo;

This meta-method extends the scalar meta-method, and supports the same interfaces and parameters. However, it generally treats values as true-or-false flags, and can not be used to store strings, numbers, or references.

Interfaces:

flag_set_clear (default)
Provides the get_set behavior for *, and set_* and clear_* methods to set the value to true or false.

Behaviors: In addition to those provided by scalar, the following types of accessor methods are available.

get_set
The get_set behavior is similar to the get_set scalar behavior except that empty or false values are treated as zero, and true values are treated as zero.

set_true
Sets the value to one.

set_false
Sets the value to zero. =back

bits Accessor

A generic accessor for bit-field values.

The difference between 'Template::Generic:bits' and 'Template::Generic:boolean' is that all flags created with this meta-method are stored in a single vector for space efficiency.

Interfaces: The following calling interfaces are available.

default
Provides get_set behavior for *, a set_* method which sets the value to true and a clear_* method which sets the value to false.

Also defines methods named bits, bit_fields, and bit_dump with the behaviors below. These methods are shared across all of the boolean meta-methods defined by a single class.

class_methods
.

Basic Behaviors: The following types of bit-level accessor methods are available.

get_set
Returns the value of the named flag. If called with an argument, it first sets the named flag to the truth-value of the argument.

set_true
Sets the value to true.

set_false
Sets the value to false.

Group Methods: The following types of methods manipulate the overall vector value.

bits
Returns the vector containing all of the bit fields (remember however that a vector containing all 0 bits is still true).

bit_dump
Returns a hash of the flag-name/flag-value pairs.

bits_size
Returns the number of bits that can fit into the current vector.

bits_complement
Returns the twos-complement of the vector.

bit_pos_get
Takes a single argument and returns the value of the bit stored in that position.

bit_pos_set
Takes two arguments and sets the bit stored in the position of the first argument to the value of the second argument.

Class Methods: The following types of class methods are available.

bit_names
Returns a list of all the flags by name.

array Accessor

Creates accessor methods for manipulating arrays of values.

Interfaces: The following calling interfaces are available.

default
Provides get_set behavior for *, and verb_* methods for the non-get behaviors below.

minimal
Provides get_set behavior for *, and *_verb methods for clear behavior.

get_set_items
Provides the get_set_items for *.

x_verb
Provides get_push behavior for *, and *_verb methods for the non-get behaviors below.

get_set_ref
Provides the get_set_ref for *.

get_set_ref_help
Provides the get_set_ref for *, and verb_* methods for the non-get behaviors below.

Behaviors: The following types of accessor methods are available.

get_set_items
Called with no arguments returns a reference to the array stored in the slot.

Called with one simple scalar argument it treats the argument as an index and returns the value stored under that index.

Called with more than one argument, treats them as a series of index/value pairs and adds them to the array.

get_push
If arguments are passed, these values are pushed on to the list; if a single array ref is passed, its values are used as the arguments.

This method returns the list of values stored in the slot. In an array context it returns them as an array and in a scalar context as a reference to the array.

get_set_ref
If arguments are passed, these values are placed on the list, replacing the current contents; if a single array ref is passed, its values are used as the arguments.

This method returns the list of values stored in the slot. In an array context it returns them as an array and in a scalar context as a reference to the array.

get_set
If arguments are passed, these values are placed on the list, replacing the current contents.

This method returns the list of values stored in the slot. In an array context it returns them as an array and in a scalar context as a reference to the array.

push
Append items to tail.

pop
Remove an item from the tail.

shift
Remove an item from the front.

unshift
Prepend items to front.

splice
Remove or replace items.

clear
Remove all items.

count
Returns the number of item in the list.

hash Accessor

Creates accessor methods for manipulating hashes of key-value pairs.

Interfaces: The following calling interfaces are available.

default
Provides get_set behavior for *, and *_verb methods for most of the other behaviors below.

get_set_items
Provides the get_set_items for *.

Behaviors: The following types of accessor methods are available.

get_set_items
Called with no arguments returns a reference to the hash stored.

Called with one simple scalar argument it treats the argument as a key and returns the value stored under that key.

Called with more than one argument, treats them as a series of key/value pairs and adds them to the hash.

get_push
Called with no arguments returns the hash stored, as a hash in a list context or as a reference in a scalar context.

Called with one simple scalar argument it treats the argument as a key and returns the value stored under that key.

Called with one array reference argument, the array elements are considered to be be keys of the hash. x returns the list of values stored under those keys (also known as a hash slice.)

Called with one hash reference argument, the keys and values of the hash are added to the hash.

Called with more than one argument, treats them as a series of key/value pairs and adds them to the hash.

get_set
Like get_push, except if called with more then one argument, empties the current hash items before adding those arguments to the hash.

push
Called with one hash reference argument, the keys and values of the hash are added to the hash.

Called with more than one argument, treats them as a series of key/value pairs and adds them to the hash.

keys
Returns a list of the keys of the hash.

values
Returns a list of the values in the hash.

tally
Takes a list of arguments and for each scalar in the list increments the value stored in the hash and returns a list of the current (after the increment) values.

exists
Takes a single key, returns whether that key exists in the hash.

delete
Takes a list, deletes each key from the hash, and returns the corresponding values.

clear
Resets hash to empty.

tiedhash Accessor

A variant of Generic:hash which initializes the hash by tieing it to a caller-specified package.

See the documentation on Generic:hash for interfaces and behaviors.

Parameters: The following parameters must be provided:

tie
Required. The name of the class to tie to. Make sure you have used the required class.

args
Required. Additional arguments for the tie, as an array ref.

Example:

  use Class::MakeMethods
    tie_hash => [ hits => { tie => q/Tie::RefHash/, args => [] } ];
  use Class::MakeMethods
    tie_hash => [ [qw(hits errors)] => { tie => q/Tie::RefHash/, args => [] } ];
  use Class::MakeMethods
    tie_hash => [ { name => hits, tie => q/Tie::RefHash/, args => [] } ];

hash_of_arrays Accessor

Creates accessor methods for manipulating hashes of array-refs.

Interfaces: The following calling interfaces are available.

default
Provides get behavior for *, and *_verb methods for the other behaviors below.

Behaviors: The following types of accessor methods are available.

get
Returns all the values for all the given keys, in order. If no keys are given, returns all the values (in an unspecified key order).

The result is returned as an arrayref in scalar context. This arrayref is not part of the data structure; messing with it will not affect the contents directly (even if a single key was provided as argument.)

If any argument is provided which is an arrayref, then the members of that array are used as keys. Thus, the trivial empty-key case may be utilized with an argument of [].

keys
Returns the keys of the hash. As an arrayref in scalar context.

exists
Takes a list of keys, and returns whether all of the key exists in the hash (i.e., the and of whether the individual keys exist).

delete
Takes a list, deletes each key from the hash.

push
Takes a key, and some values. Pushes the values onto the list denoted by the key. If the first argument is an arrayref, then each element of that arrayref is treated as a key and the elements pushed onto each appropriate list.

pop
Takes a list of keys, and pops each one. Returns the list of popped elements. undef is returned in the list for each key that is has an empty list.

unshift
Like push, only the from the other end of the lists.

shift
Like pop, only the from the other end of the lists.

splice
Takes a key, offset, length, and a values list. Splices the list named by the key. Anything from the offset argument (inclusive) may be omitted. See splice in the perlfunc manpage.

clear
Takes a list of keys. Resets each named list to empty (but does not delete the keys.)

count
Takes a list of keys. Returns the sum of the number of elements for each named list.

index
Takes a key, and a list of indices. Returns a list of each item at the corresponding index in the list of the given key. Uses undef for indices beyond range.

remove
Takes a key, and a list of indices. Removes each corresponding item from the named list. The indices are effectively looked up at the point of call -- thus removing indices 3, 1 from list (a, b, c, d) will remove (d) and (b).

sift
Takes a key, and a set of named arguments, which may be a list or a hash ref. Removes list members based on a grep-like approach.
filter
The filter function used (as a coderef). Is passed two arguments, the value compared against, and the value in the list that is potential for grepping out. If returns true, the value is removed. Default is sub { $_[0] == $_[1] }.

keys
The list keys to sift through (as an arrayref). Unknown keys are ignored. Default: all the known keys.

values
The values to sift out (as an arrayref). Default: [undef]

object Accessor

Creates accessor methods for manipulating references to objects.

In addition to creating a method to get and set the object reference, the meta-method can also define forwarded methods that automatically pass calls onto the object stored in that slot; see the description of the 'delegate' parameter below.

Interfaces: The following calling interfaces are available.

default
Provides get_set behavior for *, clear behavior for 'delete_*', and forwarding methods for any values in the method's 'delegate' or 'soft_delegate' parameters.

get_and_set
Provides named get method, set_x and clear_x methods.

get_init_and_set
Provides named get_init method, set_x and clear_x methods.

Behaviors: The following types of accessor methods are available.

get_set
The get_set method, if called with a reference to an object of the given class as the first argument, stores it.

If called with any other arguments, creates and stores a new object, passing the arguemnts to the new() method for the object.

If called without arguments, returns the current value, which may be undefined if one has not been stored yet.

get_set_init
The get_set_init method, if called with a reference to an object of the given class as the first argument, stores it.

If the slot is not filled yet it creates an object by calling the given new method of the given class. Any arguments passed to the get_set_init method are passed on to new.

In all cases the object now stored is returned.

get_init
If the instance is empty, creates and stores a new one. Returns the instance.

get
Returns the current value, which may be undefined if one has not been stored yet.

set
If called with a reference to an object of the given class as the first argument, stores it.

If called with any other arguments, creates and stores a new object, passing the arguments to the new() method.

If called without arguments, creates and stores a new object, without any arguments to the new() method.

clear
Removes the reference value.

forwarding
If a 'delegate' or 'soft_delegate' parameter is provided, methods with those names are created that are forwarded directly to the object in the slot, as described below.

Parameters: The following parameters are supported:

class
Required. The type of object that will be stored.

new_method
The name of the method to call on the above class to create a new instance. Defaults to 'new'.

delegate
The methods to forward to the object. Can contain a method name, a string of space-spearated method names, or an array of method names. This type of method will croak if it is called when the target object is not defined.

soft_delegate
The methods to forward to the object, if it is present. Can contain a method name, a string of space-spearated method names, or an array of method names. This type of method will return nothing if it is called when the target object is not defined.

instance Accessor

Creates methods to handle an instance of the calling class.

PROFILES

default
Provides named get method, and verb_x set, new, and clear methods.

-implicit_new
Provides named get_init method, and verb_x set, and clear methods.

-x_verb
Provides named get method, and x_verb set, new, and clear methods.

Behaviors: The following types of accessor methods are available.

get
Returns the value of the instance parameter, which may be undefined if one has not been stored yet.

get_init
If the instance is empty, creates and stores a new one. Returns the instance.

set
Takes a single argument and sets the instance to that value.

new
Creates and stores a new instance.

clear
Sets the instance parameter to undef.

Parameters: The following parameters are supported:

instance
Holds the instance reference. Defaults to undef

new_method
The name of the method to call when creating a new instance. Defaults to 'new'.

array_of_objects Accessor

Creates accessor methods for manipulating references to arrays of object references.

Operates like Generic:array, but prior to adding any item to the array, it first checks to see if it is an instance of the designated class, and if not passes it as an argument to that class's new method and stores the result instead.

Forwarded methods return a list of the results returned by maping the method over each object in the array.

See the documentation on Generic:array for interfaces and behaviors.

Parameters: The following parameters are supported:

class
Required. The type of object that will be stored.

delegate
The methods to forward to the object. Can contain a method name, a string of space-spearated method names, or an array of method names.

new_method
The name of the method to call on the above class to create a new instance. Defaults to 'new'.

code Accessor

Creates accessor methods for manipulating references to subroutines.

Interfaces: The following calling interfaces are available.

default
Provides the call_set functionality.

method
Provides the call_method functionality.

Behaviors: The following types of accessor methods are available.

call_set
If called with one argument which is a CODE reference, it installs that code in the slot. Otherwise it runs the code stored in the slot with whatever arguments (including none) were passed in.

call_method
Just like call_set, except the code is called like a method, with $self as its first argument. Basically, you are creating a method which can be different for each object.

code_or_scalar Accessor

Creates accessor methods for manipulating either strings or references to subroutines.

You can store any scalar value; code refs are executed when you retrieve the value, while other scalars are returned as-is.

Interfaces: The following calling interfaces are available.

default
Provides the call_set functionality.

method
Provides the call_method functionality.

eiffel
Provides the named get_method, and a helper set_* method.

Behaviors: The following types of accessor methods are available.

get_set_call
If called with an argument, either a CODE reference or some other scalar, it installs that code in the slot. Otherwise, if the current value runs the code stored in the slot with whatever arguments (including none) were passed in.

get_set_method
Just like call_set, except the code is called like a method, with $self as its first argument. Basically, you are creating a method which can be different for each object.


SEE ALSO

See the Class::MakeMethods manpage for general information about this distribution.

See the Class::MakeMethods::Template manpage for information about this family of subclasses.

 Class::MakeMethods::Template::Generic - Templates for common meta-method types