, , Moose. .
, , . , . - , . , , . - :
my @all_hobbies = uniq map $_->all_hobbies,
map $_->all_students, $school->all_classrooms;
. . , , . .
Moose . hobbies , .
package Child;
use Moose;
has [ 'name', 'hair_color', 'fav_color' ] => (
is => 'ro',
isa => 'Str',
required => 1,
);
has [ 'age', 'height', 'weight' ] => (
is => 'ro',
isa => 'Num',
required => 1,
);
has hobbies => (
is => 'ro',
isa => 'Int',
default => sub {[]},
traits => ['Array'],
handles => {
has_no_hobbies => 'is_empty',
num_hobbies => 'count',
has_hobbies => 'count',
add_hobby => 'push',
clear_hobbies => 'clear',
all_hobbies => 'elements',
},
);
__PACKAGE__->meta->make_immutable;
no Moose;
, Child:
use List::MoreUtils qw( zip );
@attributes = qw( name age height weight hair_color fav_color hobbies );
my @children = map Child->new( %$_ ),
map { zip @attributes, @$_ },
["James", 12, 1.62, 73, "dark brown", "blue", ["playing football", "eating ice-cream"]],
["Norah", 13, 1.75, 81, "black", "red", ["computer programming"]],
["Billy", 11, 1.31, 63, "red", "green", ["reading", "drawing"]],
;
my %children_by_name = map { $_->name, $_ } @children;
for my $c ( grep $_->has_hobbies, @children ) {
my $n = $c->name;
my $h = join ", ", $c->all_hobbies;
print "$n likes $h\n";
}