.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Moose::Cookbook::Basics::Genome_OverloadingSubtypesAndCoercion 3pm" .TH Moose::Cookbook::Basics::Genome_OverloadingSubtypesAndCoercion 3pm 2024-01-21 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Moose::Cookbook::Basics::Genome_OverloadingSubtypesAndCoercion \- Operator overloading, subtypes, and coercion .SH VERSION .IX Header "VERSION" version 2.2207 .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& package Human; \& \& use Moose; \& use Moose::Util::TypeConstraints; \& \& subtype \*(AqSex\*(Aq \& => as \*(AqStr\*(Aq \& => where { $_ =~ m{^[mf]$}s }; \& \& has \*(Aqsex\*(Aq => ( is => \*(Aqro\*(Aq, isa => \*(AqSex\*(Aq, required => 1 ); \& \& has \*(Aqmother\*(Aq => ( is => \*(Aqro\*(Aq, isa => \*(AqHuman\*(Aq ); \& has \*(Aqfather\*(Aq => ( is => \*(Aqro\*(Aq, isa => \*(AqHuman\*(Aq ); \& \& use overload \*(Aq+\*(Aq => \e&_overload_add, fallback => 1; \& \& sub _overload_add { \& my ( $one, $two ) = @_; \& \& die(\*(AqOnly male and female humans may create children\*(Aq) \& if ( $one\->sex() eq $two\->sex() ); \& \& my ( $mother, $father ) \& = ( $one\->sex eq \*(Aqf\*(Aq ? ( $one, $two ) : ( $two, $one ) ); \& \& my $sex = \*(Aqf\*(Aq; \& $sex = \*(Aqm\*(Aq if ( rand() >= 0.5 ); \& \& return Human\->new( \& sex => $sex, \& mother => $mother, \& father => $father, \& ); \& } .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This Moose cookbook recipe shows how operator overloading, coercion, and subtypes can be used to mimic the human reproductive system (well, the selection of genes at least). .SH INTRODUCTION .IX Header "INTRODUCTION" Our \f(CW\*(C`Human\*(C'\fR class uses operator overloading to allow us to "add" two humans together and produce a child. Our implementation does require that the two objects be of opposite sex. Remember, we're talking about biological reproduction, not marriage. .PP While this example works as-is, we can take it a lot further by adding genes into the mix. We'll add the two genes that control eye color, and use overloading to combine the genes from the parent to model the biology. .SS "What is Operator Overloading?" .IX Subsection "What is Operator Overloading?" Overloading is \fInot\fR a Moose-specific feature. It's a general OO concept that is implemented in Perl with the \f(CW\*(C`overload\*(C'\fR pragma. Overloading lets objects do something sane when used with Perl's built in operators, like addition (\f(CW\*(C`+\*(C'\fR) or when used as a string. .PP In this example we overload addition so we can write code like \&\f(CW\*(C`$child = $mother + $father\*(C'\fR. .SH GENES .IX Header "GENES" There are many genes which affect eye color, but there are two which are most important, \fIgey\fR and \fIbey2\fR. We will start by making a class for each gene. .SS Human::Gene::bey2 .IX Subsection "Human::Gene::bey2" .Vb 1 \& package Human::Gene::bey2; \& \& use Moose; \& use Moose::Util::TypeConstraints; \& \& type \*(Aqbey2_color\*(Aq => where { $_ =~ m{^(?:brown|blue)$} }; \& \& has \*(Aqcolor\*(Aq => ( is => \*(Aqro\*(Aq, isa => \*(Aqbey2_color\*(Aq ); .Ve .PP This class is trivial. We have a type constraint for the allowed colors, and a \f(CW\*(C`color\*(C'\fR attribute. .SS Human::Gene::gey .IX Subsection "Human::Gene::gey" .Vb 1 \& package Human::Gene::gey; \& \& use Moose; \& use Moose::Util::TypeConstraints; \& \& type \*(Aqgey_color\*(Aq => where { $_ =~ m{^(?:green|blue)$} }; \& \& has \*(Aqcolor\*(Aq => ( is => \*(Aqro\*(Aq, isa => \*(Aqgey_color\*(Aq ); .Ve .PP This is nearly identical to the \f(CW\*(C`Humane::Gene::bey2\*(C'\fR class, except that the \fIgey\fR gene allows for different colors. .SH "EYE COLOR" .IX Header "EYE COLOR" We could just give four attributes (two of each gene) to the \&\f(CW\*(C`Human\*(C'\fR class, but this is a bit messy. Instead, we'll abstract the genes into a container class, \f(CW\*(C`Human::EyeColor\*(C'\fR. Then a \f(CW\*(C`Human\*(C'\fR can have a single \f(CW\*(C`eye_color\*(C'\fR attribute. .PP .Vb 1 \& package Human::EyeColor; \& \& use Moose; \& use Moose::Util::TypeConstraints; \& \& coerce \*(AqHuman::Gene::bey2\*(Aq \& => from \*(AqStr\*(Aq \& => via { Human::Gene::bey2\->new( color => $_ ) }; \& \& coerce \*(AqHuman::Gene::gey\*(Aq \& => from \*(AqStr\*(Aq \& => via { Human::Gene::gey\->new( color => $_ ) }; \& \& has [qw( bey2_1 bey2_2 )] => \& ( is => \*(Aqro\*(Aq, isa => \*(AqHuman::Gene::bey2\*(Aq, coerce => 1 ); \& \& has [qw( gey_1 gey_2 )] => \& ( is => \*(Aqro\*(Aq, isa => \*(AqHuman::Gene::gey\*(Aq, coerce => 1 ); .Ve .PP The eye color class has two of each type of gene. We've also created a coercion for each class that coerces a string into a new object. Note that a coercion will fail if it attempts to coerce a string like "indigo", because that is not a valid color for either type of gene. .PP As an aside, you can see that we can define several identical attributes at once by supplying an array reference of names as the first argument to \f(CW\*(C`has\*(C'\fR. .PP We also need a method to calculate the actual eye color that results from a set of genes. The \fIbey2\fR brown gene is dominant over both blue and green. The \fIgey\fR green gene is dominant over blue. .PP .Vb 2 \& sub color { \& my ($self) = @_; \& \& return \*(Aqbrown\*(Aq \& if ( $self\->bey2_1\->color() eq \*(Aqbrown\*(Aq \& or $self\->bey2_2\->color() eq \*(Aqbrown\*(Aq ); \& \& return \*(Aqgreen\*(Aq \& if ( $self\->gey_1\->color() eq \*(Aqgreen\*(Aq \& or $self\->gey_2\->color() eq \*(Aqgreen\*(Aq ); \& \& return \*(Aqblue\*(Aq; \& } .Ve .PP We'd like to be able to treat a \f(CW\*(C`Human::EyeColor\*(C'\fR object as a string, so we define a string overloading for the class: .PP .Vb 1 \& use overload \*(Aq""\*(Aq => \e&color, fallback => 1; .Ve .PP Finally, we need to define overloading for addition. That way we can add together two \f(CW\*(C`Human::EyeColor\*(C'\fR objects and get a new one with a new (genetically correct) eye color. .PP .Vb 1 \& use overload \*(Aq+\*(Aq => \e&_overload_add, fallback => 1; \& \& sub _overload_add { \& my ( $one, $two ) = @_; \& \& my $one_bey2 = \*(Aqbey2_\*(Aq . _rand2(); \& my $two_bey2 = \*(Aqbey2_\*(Aq . _rand2(); \& \& my $one_gey = \*(Aqgey_\*(Aq . _rand2(); \& my $two_gey = \*(Aqgey_\*(Aq . _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 + int( rand(2) ); \& } .Ve .PP When two eye color objects are added together, the \f(CW_overload_add()\fR method will be passed two \f(CW\*(C`Human::EyeColor\*(C'\fR objects. These are the left and right side operands for the \f(CW\*(C`+\*(C'\fR operator. This method returns a new \f(CW\*(C`Human::EyeColor\*(C'\fR object. .ie n .SH "ADDING EYE COLOR TO ""Human""s" .el .SH "ADDING EYE COLOR TO \f(CWHuman\fPs" .IX Header "ADDING EYE COLOR TO Humans" Our original \f(CW\*(C`Human\*(C'\fR class requires just a few changes to incorporate our new \f(CW\*(C`Human::EyeColor\*(C'\fR class. .PP .Vb 1 \& use List::Util 1.56 qw( mesh ); \& \& coerce \*(AqHuman::EyeColor\*(Aq \& => from \*(AqArrayRef\*(Aq \& => via { my @genes = qw( bey2_1 bey2_2 gey_1 gey_2 ); \& return Human::EyeColor\->new( mesh ( \e@genes, $_ ) ); }; \& \& has \*(Aqeye_color\*(Aq => ( \& is => \*(Aqro\*(Aq, \& isa => \*(AqHuman::EyeColor\*(Aq, \& coerce => 1, \& required => 1, \& ); .Ve .PP We also need to modify \f(CW_overload_add()\fR in the \f(CW\*(C`Human\*(C'\fR class to account for eye color: .PP .Vb 6 \& return Human\->new( \& sex => $sex, \& eye_color => ( $one\->eye_color() + $two\->eye_color() ), \& mother => $mother, \& father => $father, \& ); .Ve .SH CONCLUSION .IX Header "CONCLUSION" The three techniques we used, overloading, subtypes, and coercion, combine to provide a powerful interface. .PP If you'd like to learn more about overloading, please read the documentation for the overload pragma. .PP To see all the code we created together, take a look at \&\fIt/recipes/basics_genome_overloadingsubtypesandcoercion.t\fR. .SH "NEXT STEPS" .IX Header "NEXT STEPS" Had this been a real project we'd probably want: .IP "Better Randomization with Crypt::Random" 4 .IX Item "Better Randomization with Crypt::Random" .PD 0 .IP "Characteristic Base Class" 4 .IX Item "Characteristic Base Class" .IP "Mutating Genes" 4 .IX Item "Mutating Genes" .IP "More Characteristics" 4 .IX Item "More Characteristics" .IP "Artificial Life" 4 .IX Item "Artificial Life" .PD .SH AUTHORS .IX Header "AUTHORS" .IP \(bu 4 Stevan Little .IP \(bu 4 Dave Rolsky .IP \(bu 4 Jesse Luehrs .IP \(bu 4 Shawn M Moore .IP \(bu 4 יובל קוג'מן (Yuval Kogman) .IP \(bu 4 Karen Etheridge .IP \(bu 4 Florian Ragwitz .IP \(bu 4 Hans Dieter Pearcey .IP \(bu 4 Chris Prather .IP \(bu 4 Matt S Trout .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This work is licensed under a Creative Commons Attribution 3.0 Unported License. .PP License details are at: