NAME
Rose::Class::MakeMethods::Generic - Create simple class methods.
SYNOPSIS
package MyClass;
use Rose::Class::MakeMethods::Generic
(
scalar =>
[
'error',
'type' => { interface => 'get_set_init' },
],
inheritable_scalar => 'name',
);
sub init_type { 'special' }
...
package MySubClass;
our @ISA = qw(MyClass);
...
MyClass->error(123);
print MyClass->type; # 'special'
MyClass->name('Fred');
print MySubClass->name; # 'Fred'
MyClass->name('Wilma');
print MySubClass->name; # 'Wilma'
MySubClass->name('Bam');
print MyClass->name; # 'Wilma'
print MySubClass->name; # 'Bam'
DESCRIPTION
Rose::Class::MakeMethods::Generic
is a method maker that inherits from Rose::Object::MakeMethods
. See the Rose::Object::MakeMethods
documentation to learn about the interface. The method types provided by this module are described below. All methods work only with classes, not objects.
METHODS TYPES
- scalar
-
Create get/set methods for scalar class attributes.
- Options
-
init_method
-
The name of the class method to call when initializing the value of an undefined attribute. This option is only applicable when using the
get_set_init
interface. Defaults to the method name with the prefixinit_
added. interface
-
Choose one of the two possible interfaces. Defaults to
get_set
.
- Interfaces
-
get_set
-
Creates a simple get/set accessor method for a class attribute. When called with an argument, the value of the attribute is set. The current value of the attribute is returned.
get_set_init
-
Behaves like the
get_set
interface unless the value of the attribute is undefined. In that case, the class method specified by theinit_method
option is called and the attribute is set to the return value of that method.
Example:
package MyClass; use Rose::Class::MakeMethods::Generic ( scalar => 'power', 'scalar --get_set_init' => 'name', ); sub init_name { 'Fred' } ... MyClass->power(99); # returns 99 MyClass->name; # returns "Fred" MyClass->name('Bill'); # returns "Bill"
- inheritable_scalar
-
Create get/set methods for scalar class attributes that are inherited by subclasses until/unless their values are changed.
- Options
-
interface
-
Choose the interface. This is kind of pointless since there is only one interface right now. Defaults to
get_set
, obviously.
- Interfaces
-
get_set
-
Creates a get/set accessor method for a class attribute. When called with an argument, the value of the attribute is set and then returned.
If called with no arguments, and if the attribute was never set for this class, then a left-most, breadth-first search of the parent classes is initiated. The value returned is taken from first parent class encountered that has ever had this attribute set.
Example:
package MyClass; use Rose::Class::MakeMethods::Generic ( inheritable_scalar => 'name', ); ... package MySubClass; our @ISA = qw(MyClass); ... package MySubSubClass; our @ISA = qw(MySubClass); ... $x = MyClass->name; # undef $y = MySubClass->name; # undef $z = MySubSubClass->name; # undef MyClass->name('Fred'); $x = MyClass->name; # 'Fred' $y = MySubClass->name; # 'Fred' $z = MySubSubClass->name; # 'Fred' MyClass->name('Wilma'); $x = MyClass->name; # 'Wilma' $y = MySubClass->name; # 'Wilma' $z = MySubSubClass->name; # 'Wilma' MySubClass->name('Bam'); $x = MyClass->name; # 'Wilma' $y = MySubClass->name; # 'Bam' $z = MySubSubClass->name; # 'Bam' MyClass->name('Koop'); MySubClass->name(undef); $x = MyClass->name; # 'Koop' $y = MySubClass->name; # undef $z = MySubSubClass->name; # undef MySubSubClass->name('Sam'); $x = MyClass->name; # 'Koop' $y = MySubClass->name; # undef $z = MySubSubClass->name; # 'Sam'
AUTHOR
John C. Siracusa (siracusa@mindspring.com)