For example, a simplified version of the Animal
class might be defined as follows:
package Animal;
use Class::MethodMaker
new_with_init => 'new',
get_set => [-eiffel => [qw(color height name age)]],
abstract => [qw(sound)],
;
sub init {
my $self = shift;
$self->set_color($self->default_color);
}
sub named {
my $self = shift->new;
$self->set_name(shift);
$self;
}
sub speak {
my $self = shift;
print $self->name, " goes ", $self->sound, "\n";
}
sub eat {
my $self = shift;
my $food = shift;
print $self->name, " eats $food\n";
}
sub default_color {
"brown";
}
The getters and setters for the four instance attributes (name,
height, color, and age) are defined automatically, using the method
color to get the color and
set_color to set the color. (The
eiffel flag says "do it the way
the Eiffel language does it," which is the way it
should be done here.) The messy blessing step is now hidden behind a
simple new method. The initial color is defined as
the default color, as before, because the init
method is automatically called from new.
However, you can still call Horse->named('Mr.
Ed') because it immediately calls the
new routine as well.