Class::MakeMethods::Template::Generic - Templates for common meta-method types |
Class::MakeMethods::Template::Generic - 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.
Behaviors: The following types of constructor methods are available.
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.
The arguments are treated as a hash of method-name/argument-value
pairs, with each such pair causing a call $self->name($value)
.
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.
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.
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.)
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.
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.
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.
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:
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.
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' ];
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' ];
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' ];
Provides the get_init behavior for *, and an delete behavior for clear_*. Specifies default value for init_method parameter of init_*.
Behaviors: The following types of accessor methods are available.
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.
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;
Parameters: The following parameters are supported:
Only used by the get_init behavior.
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.
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.
Parameters: In addition to those provided by scalar
, the following parameters are supported.
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:
'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 ``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.
Behaviors: In addition to those provided by scalar
, the following types of accessor methods are available.
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:
Behaviors: In addition to those provided by scalar
, the following types of accessor methods are available.
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.
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.
Group Methods: The following types of methods manipulate the overall vector value.
Class Methods: The following types of class methods are available.
Creates accessor methods for manipulating arrays of values.
Interfaces: The following calling interfaces are available.
Behaviors: The following types of accessor methods are available.
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.
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.
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.
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.
Creates accessor methods for manipulating hashes of key-value pairs.
Interfaces: The following calling interfaces are available.
Behaviors: The following types of accessor methods are available.
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 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.
Called with more than one argument, treats them as a series of key/value pairs and adds them to the hash.
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:
use
d the required class.
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.
Behaviors: The following types of accessor methods are available.
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 [].
and
of whether the individual keys exist).
sub { $_[0] == $_[1] }
.
[undef]
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.
Behaviors: The following types of accessor methods are available.
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.
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 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.
Parameters: The following parameters are supported:
Creates methods to handle an instance of the calling class.
PROFILES
Behaviors: The following types of accessor methods are available.
Parameters: The following parameters are supported:
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 map
ing the method over each object in the array.
See the documentation on Generic:array
for interfaces and behaviors.
Parameters: The following parameters are supported:
Creates accessor methods for manipulating references to subroutines.
Interfaces: The following calling interfaces are available.
Behaviors: The following types of accessor methods are available.
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.
Behaviors: The following types of accessor methods are available.
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 |