13 use Moose::Util::TypeConstraints;
17 => where { $_ =~ m{^[mf]$}s };
19 has 'sex' => ( is => 'ro', isa => 'Sex', required => 1 );
21 has 'mother' => ( is => 'ro', isa => 'Human' );
22 has 'father' => ( is => 'ro', isa => 'Human' );
24 use overload '+' => \&_overload_add, fallback => 1;
27 my ( $one, $two ) = @_;
29 die('Only male and female humans may create children')
30 if ( $one->sex() eq $two->sex() );
32 my ( $mother, $father )
33 = ( $one->sex eq 'f' ? ( $one, $two ) : ( $two, $one ) );
36 $sex = 'm' if ( rand() >= 0.5 );
40 eye_color => ( $one->eye_color() + $two->eye_color() ),
46 use List::MoreUtils qw( zip );
48 coerce 'Human::EyeColor'
50 => via { my @genes = qw( bey2_1 bey2_2 gey_1 gey_2 );
51 return Human::EyeColor->new( zip( @genes, @{$_} ) ); };
55 isa => 'Human::EyeColor',
63 package Human::Gene::bey2;
66 use Moose::Util::TypeConstraints;
68 type 'bey2_color' => where { $_ =~ m{^(?:brown|blue)$} };
70 has 'color' => ( is => 'ro', isa => 'bey2_color' );
74 package Human::Gene::gey;
77 use Moose::Util::TypeConstraints;
79 type 'gey_color' => where { $_ =~ m{^(?:green|blue)$} };
81 has 'color' => ( is => 'ro', isa => 'gey_color' );
85 package Human::EyeColor;
88 use Moose::Util::TypeConstraints;
90 coerce 'Human::Gene::bey2'
92 => via { Human::Gene::bey2->new( color => $_ ) };
94 coerce 'Human::Gene::gey'
96 => via { Human::Gene::gey->new( color => $_ ) };
98 has [qw( bey2_1 bey2_2 )] =>
99 ( is => 'ro', isa => 'Human::Gene::bey2', coerce => 1 );
101 has [qw( gey_1 gey_2 )] =>
102 ( is => 'ro', isa => 'Human::Gene::gey', coerce => 1 );
108 if ( $self->bey2_1->color() eq 'brown'
109 or $self->bey2_2->color() eq 'brown' );
112 if ( $self->gey_1->color() eq 'green'
113 or $self->gey_2->color() eq 'green' );
118 use overload '""' => \&color, fallback => 1;
120 use overload '+' => \&_overload_add, fallback => 1;
123 my ( $one, $two ) = @_;
125 my $one_bey2 = 'bey2_' . _rand2();
126 my $two_bey2 = 'bey2_' . _rand2();
128 my $one_gey = 'gey_' . _rand2();
129 my $two_gey = 'gey_' . _rand2();
131 return Human::EyeColor->new(
132 bey2_1 => $one->$one_bey2->color(),
133 bey2_2 => $two->$two_bey2->color(),
134 gey_1 => $one->$one_gey->color(),
135 gey_2 => $two->$two_gey->color(),
140 return 1 + int( rand(2) );
144 my $gene_color_sets = [
145 [ qw( blue blue blue blue ) => 'blue' ],
146 [ qw( blue blue green blue ) => 'green' ],
147 [ qw( blue blue blue green ) => 'green' ],
148 [ qw( blue blue green green ) => 'green' ],
149 [ qw( brown blue blue blue ) => 'brown' ],
150 [ qw( brown brown green green ) => 'brown' ],
151 [ qw( blue brown green blue ) => 'brown' ],
154 foreach my $set (@$gene_color_sets) {
155 my $expected_color = pop(@$set);
157 my $person = Human->new(
163 $person->eye_color(),
175 [qw( blue blue blue blue )],
176 [qw( blue blue blue blue )] => 'blue'
179 [qw( blue blue blue blue )],
180 [qw( brown brown green blue )] => 'brown'
183 [qw( blue blue green green )],
184 [qw( blue blue green green )] => 'green'
188 foreach my $set (@$parent_sets) {
189 my $expected_color = pop(@$set);
191 my $mother = Human->new(
193 eye_color => shift(@$set),
196 my $father = Human->new(
198 eye_color => shift(@$set),
201 my $child = $mother + $father;
207 . $mother->eye_color()
209 . $father->eye_color()
215 # Hmm, not sure how to test for random selection of genes since
216 # I could theoretically run an infinite number of iterations and
217 # never find proof that a child has inherited a particular gene.
219 # AUTHOR: Aran Clary Deltac <bluefeet@cpan.org>