Templates for common meta-method types
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");
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.
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.
Provides the with_values behavior.
Provides the with_init behavior.
Provides the with_methods behavior.
Provides the with_init behavior for *, and the general purpose method_init behavior as an init method.
Provides the copy behavior.
Behaviors: The following types of constructor methods are available.
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.
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 \*(C`$self->name($value)\*(C'.
Creates and blesses a new instance, then calls a method named \*(C`init\*(C', 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.
Creates a new instance using method-name/argument-value pairs, like \*(C`with_methods\*(C', but then calls a method named \*(C`init\*(C' before returning the new object. The \*(C`init\*(C' method does not receive any arguments. The init() method should be defined in the class declaring these 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.)
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.
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.
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.
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:
The name of the method to call after creating a new instance. Defaults to 'init'.
A generic scalar-value accessor meta-method which serves as an abstraction for basic \*(L"get_set\*(R" 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.
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' ];
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.
Provides an get_set accessor for * that croaks if a new value is passed in from a package other than the declaring one.
Provides an accessor for * that does not modify its value. (Its initial value would have to be set by some other means.)
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' ];
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' ];
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_*.
Provides get_set behavior for *, and a clear_* method.
Behaviors: The following types of accessor methods are available.
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.
Returns the value from the current instance.
Sets the value for the current instance. If called with no arguments, the value is set to undef. Does not return a value.
Sets value to undef.
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;
Like get_set, but if called with an argument, returns the previous value before it was changed to the new one.
If the value is currently undefined, calls the init_method. Returns the value.
Parameters: The following parameters are supported:
The name of a method to be called to initialize this meta-method. Only used by the get_init behavior.
A generic scalar-value accessor meta-method which serves as an abstraction for basic \*(L"get_set\*(R" 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 \*(C`scalar\*(C', the following calling interfaces are available.
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 \*(C`scalar\*(C', the following types of accessor methods are available.
Concatenates the argument value with the existing value.
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 \*(C`scalar\*(C', the following parameters are supported.
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 => [ 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:
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.
Profiles:
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.
A generic scalar-value accessor meta-method which serves as an abstraction for basic \*(L"get_set\*(R" 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 \*(C`scalar\*(C', the following calling interfaces are available.
Provides the numeric get_set behavior for *, and numeric *_incr and *_reset methods.
Behaviors: In addition to those provided by \*(C`scalar\*(C', the following types of accessor methods are available.
The get_set behavior is similar to the default scalar behavior except that empty values are treated as zero.
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.
Sets the value to zero.
A generic scalar-value accessor meta-method which serves as an abstraction for basic \*(L"get_set\*(R" 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:
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 \*(C`scalar\*(C', the following types of accessor methods are available.
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.
Sets the value to one.
Sets the value to zero.
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.
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.
.
Basic Behaviors: The following types of bit-level accessor methods are available.
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.
Sets the value to true.
Sets the value to false.
Group Methods: The following types of methods manipulate the overall vector value.
Returns the vector containing all of the bit fields (remember however that a vector containing all 0 bits is still true).
Returns a hash of the flag-name/flag-value pairs.
Returns the number of bits that can fit into the current vector.
Returns the twos-complement of the vector.
Takes a single argument and returns the value of the bit stored in that position.
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.
Returns a list of all the flags by name.
Creates accessor methods for manipulating arrays of values.
Interfaces: The following calling interfaces are available.
Provides get_set behavior for *, and verb_* methods for the non-get behaviors below.
Provides get_set behavior for *, and *_verb methods for clear behavior.
Provides the get_set_items for *.
Provides get_push behavior for *, and *_verb methods for the non-get behaviors below.
Provides the get_set_ref for *.
Provides the get_set_ref for *, and verb_* methods for the non-get behaviors below.
Behaviors: The following types of accessor methods are available.
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.
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.
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.
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.
Append items to tail.
Remove an item from the tail.
Remove an item from the front.
Prepend items to front.
Remove or replace items.
Remove all items.
Returns the number of item in the list.
Creates accessor methods for manipulating hashes of key-value pairs.
Interfaces: The following calling interfaces are available.
Provides get_set behavior for *, and *_verb methods for most of the other behaviors below.
Provides the get_set_items for *.
Behaviors: The following types of accessor methods are available.
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.
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.
Like get_push, except if called with more then one argument, empties the current hash items before adding those arguments to the hash.
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.
Returns a list of the keys of the hash.
Returns a list of the values in the hash.
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.
Takes a single key, returns whether that key exists in the hash.
Takes a list, deletes each key from the hash, and returns the corresponding values.
Resets hash to empty.
A variant of Generic:hash which initializes the hash by tieing it to a caller-specified package.
See the documentation on \*(C`Generic:hash\*(C' for interfaces and behaviors.
Parameters: The following parameters must be provided:
Required. The name of the class to tie to. Make sure you have \*(C`use\*(C'd the required class.
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 => [] } ];
Creates accessor methods for manipulating hashes of array-refs.
Interfaces: The following calling interfaces are available.
Provides get behavior for *, and *_verb methods for the other behaviors below.
Behaviors: The following types of accessor methods are available.
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 [].
Returns the keys of the hash. As an arrayref in scalar context.
Takes a list of keys, and returns whether all of the key exists in the hash (i.e., the \*(C`and\*(C' of whether the individual keys exist).
Takes a list, deletes each key from the hash.
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.
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.
Like push, only the from the other end of the lists.
Like pop, only the from the other end of the lists.
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 \*(L"splice\*(R" in perlfunc.
Takes a list of keys. Resets each named list to empty (but does not delete the keys.)
Takes a list of keys. Returns the sum of the number of elements for each named list.
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.
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).
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.
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 \*(C`sub { $_[0] == $_[1] }\*(C'.
The list keys to sift through (as an arrayref). Unknown keys are ignored. Default: all the known keys.
The values to sift out (as an arrayref). Default: \*(C`[undef]\*(C'
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.
Provides get_set behavior for *, clear behavior for 'delete_*', and forwarding methods for any values in the method's 'delegate' or 'soft_delegate' parameters.
Provides named get method, set_x and clear_x methods.
Provides named get_init method, set_x and clear_x methods.
Behaviors: The following types of accessor methods are available.
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.
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.
If the instance is empty, creates and stores a new one. Returns the instance.
Returns the current value, which may be undefined if one has not been stored yet.
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.
Removes the reference value.
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:
Required. The type of object that will be stored.
The name of the method to call on the above class to create a new instance. Defaults to 'new'.
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.
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.
Creates methods to handle an instance of the calling class.
\s-1PROFILES\s0
Provides named get method, and verb_x set, new, and clear methods.
Provides named get_init method, and verb_x set, and clear methods.
Provides named get method, and x_verb set, new, and clear methods.
Behaviors: The following types of accessor methods are available.
Returns the value of the instance parameter, which may be undefined if one has not been stored yet.
If the instance is empty, creates and stores a new one. Returns the instance.
Takes a single argument and sets the instance to that value.
Creates and stores a new instance.
Sets the instance parameter to undef.
Parameters: The following parameters are supported:
Holds the instance reference. Defaults to undef
The name of the method to call when creating a new instance. Defaults to 'new'.
Creates accessor methods for manipulating references to arrays of object references.
Operates like \*(C`Generic:array\*(C', 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 \*(C`map\*(C'ing the method over each object in the array.
See the documentation on \*(C`Generic:array\*(C' for interfaces and behaviors.
Parameters: The following parameters are supported:
Required. The type of object that will be stored.
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.
The name of the method to call on the above class to create a new instance. Defaults to 'new'.
Creates accessor methods for manipulating references to subroutines.
Interfaces: The following calling interfaces are available.
Provides the call_set functionality.
Provides the call_method functionality.
Behaviors: The following types of accessor methods are available.
If called with one argument which is a \s-1CODE\s0 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.
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.
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.
Provides the call_set functionality.
Provides the call_method functionality.
Provides the named get_method, and a helper set_* method.
Behaviors: The following types of accessor methods are available.
If called with an argument, either a \s-1CODE\s0 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.
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 Class::MakeMethods for general information about this distribution.
See Class::MakeMethods::Template for information about this family of subclasses.