{
package Person;
use Moo;
has name => (is => 'ro', required => 1);
has age => (is => 'rw', required => 1);
sub introduce_yourself {
my $self = shift;
printf("My name is %s, and I am %d years old.\n", $self->name, $self->age);
}
}
my $alice = Person->new(name => 'Alice', age => 32);
$alice->introduce_yourself;
####
my $alice = Person->new(name => 'Alice', age => 32);
my $bob = Person->new(name => 'Bob', age => 31);
$_->introduce_yourself for $alice, $bob;
####
{
package Person;
use Moo;
has name => (is => 'ro', required => 1);
has age => (is => 'rw', required => 1);
sub introduce_yourself {
my $self = shift;
printf("My name is %s, and I am %d years old.\n", $self->name, $self->age);
}
}
{
package Breeding;
use Moo::Role;
has children => (is => 'ro', default => sub { return []; });
sub is_parent {
my $self = shift;
@{ $self->children };
}
sub bear_children {
my $self = shift;
my ($partner, @kids_names) = @_;
for my $name (@kids_names) {
my $kid = ref($self)->new(name => $name, age => 0);
push @{ $self->children }, $kid;
push @{ $partner->children }, $kid;
}
return;
}
}
{
package Person::Breeding;
use Moo;
extends 'Person';
with 'Breeding';
}
my $alice = Person::Breeding->new(name => 'Alice', age => 32);
my $bob = Person::Breeding->new(name => 'Bob', age => 31);
$_->introduce_yourself for $alice, $bob;
$alice->bear_children($bob, 'Carol', 'Dave', 'Eve');
print "Bob is a father!\n" if $bob->is_parent;
####
use Moops;
class Person {
has name => (is => 'ro', required => 1);
has age => (is => 'rw', required => 1);
method introduce_yourself () {
printf("My name is %s, and I am %d years old.\n", $self->name, $self->age);
}
}
role Breeding {
has children => (is => 'ro', default => sub { return []; });
method is_parent () {
@{ $self->children };
}
method bear_children ($partner, @kids_names) {
for my $name (@kids_names) {
my $kid = ref($self)->new(name => $name, age => 0);
push @{ $self->children }, $kid;
push @{ $partner->children }, $kid;
}
return;
}
}
class Person::Breeding extends Person with Breeding;
my $alice = Person::Breeding->new(name => 'Alice', age => 32);
my $bob = Person::Breeding->new(name => 'Bob', age => 31);
$_->introduce_yourself for $alice, $bob;
$alice->bear_children($bob, 'Carol', 'Dave', 'Eve');
say "Bob is a father!" if $bob->is_parent;