package Human::EyeColor; use Moose; use Moose::Util::TypeConstraints; use Crypt::Random qw( makerandom ); use Human::Gene::bey2; use Human::Gene::gey; subtype 'bey2Gene' => as 'Object' => where { $_->isa('Human::Gene::bey2') }; coerce 'bey2Gene' => from 'Str' => via { Human::Gene::bey2->new( color => $_ ) }; subtype 'geyGene' => as 'Object' => where { $_->isa('Human::Gene::gey') }; coerce 'geyGene' => from 'Str' => via { Human::Gene::gey->new( color => $_ ) }; has 'bey2_1' => ( is => 'ro', isa => 'bey2Gene', coerce => 1 ); has 'bey2_2' => ( is => 'ro', isa => 'bey2Gene', coerce => 1 ); has 'gey_1' => ( is => 'ro', isa => 'geyGene', coerce => 1 ); has 'gey_2' => ( is => 'ro', isa => 'geyGene', coerce => 1 ); use overload '+' => \&_overload_add, fallback => 1; sub color { my ( $self ) = @_; return 'brown' if ($self->bey2_1->color() eq 'brown' or $self->bey2_2->color() eq 'brown'); return 'green' if ($self->gey_1->color() eq 'green' or $self->gey_2->color() eq 'green'); return 'blue'; } sub _overload_add { my ($one, $two) = @_; my $one_bey2 = 'bey2_' . _rand2(); my $two_bey2 = 'bey2_' . _rand2(); my $one_gey = 'gey_' . _rand2(); my $two_gey = 'gey_' . _rand2(); return Human::EyeColor->new( bey2_1 => $one->$one_bey2->color(), bey2_2 => $two->$two_bey2->color(), gey_1 => $one->$one_gey->color(), gey_2 => $two->$two_gey->color(), ); } sub _rand2 { return 1 + makerandom( Size => 1, Strength => 1, Uniform => 1 ); } 1;