Takes a single string or a reference to an array of strings as its argument. For each string creates a method of the form:
sub <string> {
my ($class, @args) = @_;
my $self = {};
bless $self, $class;
}
Takes a single string or a reference to an array of strings as its argument. For each string creates a method of the form listed below.
sub <string> {
my ($class, @args) = @_;
my $self = {};
bless $self, $class;
$self->init(@args);
$self;
}
Takes a single string or a reference to an array of strings as its
argument. For each string creates a method of the form listed below. Note
that this method can be called on an existing objec, which allows it to be
combined with new_with_init (see above) to provide some default values.
(Basically, declare a new_with_init method, say 'new' and a new_hash_init
method, for example, 'hash_init' and then in the init method, you can call
modify or add to the %args hash and then call hash_init.)
sub <string> {
my ($class, %args) = @_;
my $self = {};
bless $self, $class;
foreach (keys %args) {
$self->$_($args{$_});
}
$self;
}
sub x {
my ($self, $new) = @_;
defined $new and $self->{$name} = $new;
$self->{$name};
}
sub clear_x
my ($self) = @_;
$self->{$name} = undef;
}
This is your basic get/set method, and can be used for slots containing any scalar value, including references to non-scalar data. Note, however, that MethodMaker has meta-methods that define more useful sets of methods for slots containing references to lists, hashes, and objects.
grouped_fields methods
some_group => [ qw / field1 field2 field3 / ];
Its argument list is parsed as a hash of group-name => field-list pairs. Get-set methods are defined for all the fields and a method with the name of the group is defined which returns the list of fields in the group.
object => [
'Foo' => 'phooey',
'Bar' => [ qw / bar1 bar2 bar3 / ],
'Baz' => {
slot => 'foo',
comp_mthds => [ qw / bar baz / ]
},
];
This is a hairy one. The main argument should be a reference to an array. The array should contain pairs of class => sub-argument pairs. The sub-argument's are further parsed thusly:
If the sub-argument is a simple string or a reference to an array of strings (as is the case for Foo and Bar above), for each string a get/set method is created that can store an object of that class. (The get/set method, if called with a reference to an object of the given class as the first argument, stores it in the slot. If the slot isn't filled yet it creates an object by calling the given class's new method. Any arguments passed to the get/set method are passed on to new. In all cases the object now stored in the slot is returned.
If the sub-argument is a ref to a hash (as with Baz, above) then the hash should have two keys: slot and comp_mthds. The value indexed by 'slot' will be interpreted as the is in (a). The value or values (ref to an array if plural) indexed by 'comp_mthds' are the names of methods which should be ``inherited'' from the object stored in the slot. That is, using the example above, a method, foo, is created in the class that calls MethodMaker, which can get and set the value of a slot containing an object of class Baz. Class Baz in turn defines two methods, 'bar', and 'baz'. Two more methods are created in the class using MethodMaker, named 'bar' and 'baz' which result in a call to the 'bar' and 'baz' methods, through the Baz object stored in slot foo.
boolean => [ qw / foo bar baz / ]
Creates methods for setting, checking and clearing flags. All flags created
with this meta-method are stored in a single vector for space efficiency.
The argument to boolean should be a string or a reference to an array of
strings. For each string x it defines several methods: x, set_x, and clear
x. x returns the value of the x-flag. If called with an argument, it first
sets the x-flag to the truth-value of the argument. set_x is equivalent to
x(1) and clear_x is equivalent to x(0).
Additionally, boolean defines three class method: bits, which returns the vector containing all of the bit fields (remember however that a vector containing all 0 bits is still true), boolean_fields, which returns a list of all the flags by name, and bit_dump, which returns a hash of the flag-name/flag-value pairs.
struct => [ 'foo' => [ qw / foo bar baz / ] ];
XXX these docs aren't right yet.
Creates methods for setting, checking and clearing slots in a struct. All the slots created with this meta-method are stored in a single array for speed efficiency. The argument to struct should be a string or a reference to an array of strings. For each string 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 the array containing all of the bit fields (remember however that a vector containing all 0 bits is still true), boolean_fields, which returns a list of all the slots by name, and bit_dump, which returns a hash of the slot-name/slot-value pairs.
listed_attrib => [ qw / foo bar baz / ]
Like boolean, listed_attrib creates x, set_x, and clear_x methods. However, it also defines a class method x_objects which returns a list of the objects which presently have the x-flag set to true. N.B. listed_attrib does not use the same space efficient implementation as boolean, so boolean should be prefered unless the x_objects method is actually needed.
key_attrib => [ qw / foo bar baz / ]
Creates get/set methods like get/set but also maintains a hash 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.
key_with_create => [ qw / foo bar baz / ]
Just like key_attrib 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.
code => [ qw / foo bar baz / ]
Creates a slot that holds a code reference. Takes a string or a reference to a list of string and for each string, x, creates a method x which 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.
method => [ qw / foo bar baz / ]
Just like code, except the code is called like a method, with $self as it's
first argument. Basically, you're creating a method which can be different
for each object. Which is sort of weird. But perhaps useful.
interface => [ qw / foo bar baz / ]
For example a simple sub-class that defines a method type upper_case_get_set might look like this:
package Class::MethodMakerSubclass;
use strict; use Class::MethodMaker;
@Class::MethodMakerSubclass::ISA = qw ( Class::MethodMaker );
sub upper_case_get_set {
shift; # we don't need the class name
my ($name) = @_;
my %results;
$results{$name} =
sub {
my ($self, $new) = @_;
defined $new and $self->{$name} = uc $new;
$self->{$name};
};
%results;
}
1;