Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sample code for the introduction to Moose.
- Loading branch information
Showing
7 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package Person; | ||
use Moose; | ||
use Moose::Util::TypeConstraints; | ||
use DateTime; | ||
use DateTime::Format::Strptime; | ||
|
||
subtype 'BirthDate', | ||
as 'DateTime'; | ||
|
||
coerce 'BirthDate', | ||
from 'Str', | ||
via { | ||
return DateTime::Format::Strptime->new( | ||
pattern => '%Y-%m-%d', | ||
)->parse_datetime($_); | ||
}; | ||
|
||
subtype 'Gender', | ||
as 'Str', | ||
where { /^[FM]$/ }, | ||
message { "$_ is not a valid gender. It should be F or M" }; | ||
|
||
has name => ( | ||
isa => 'Str', | ||
is => 'rw', | ||
required => 1, | ||
); | ||
|
||
has dob => ( | ||
isa => 'BirthDate', | ||
is => 'ro', | ||
required => 1, | ||
coerce => 1, | ||
); | ||
|
||
has gender => ( | ||
isa => 'Gender', | ||
is => 'rw', | ||
required => 1, | ||
); | ||
|
||
sub age { | ||
my $self = shift; | ||
|
||
my $duration = DateTime->now - $self->dob; | ||
|
||
return $duration->years; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/perl | ||
use strict; | ||
use warnings; | ||
use 5.010; | ||
use DateTime; | ||
use Person; | ||
|
||
my $newborn = Person->new( | ||
name => 'Miles', | ||
dob => DateTime->now, | ||
gender => 'M' | ||
); | ||
|
||
say $newborn->name; # displays 'Miles' | ||
say $newborn->dob; # displays current date | ||
$newborn->name('Max'); # Parents change their minds | ||
say $newborn->name; # displays 'Max' | ||
say $newborn->dob->day_name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/perl | ||
use strict; | ||
use warnings; | ||
use 5.010; | ||
use DateTime; | ||
use Person; | ||
|
||
my $dob = DateTime->new(year => 1962, month => 9, day => 7); | ||
|
||
my $dave = Person->new( | ||
name => 'Dave', | ||
dob => $dob, | ||
gender => 'M' | ||
); | ||
|
||
say $dave->dob->day_name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/perl | ||
use strict; | ||
use warnings; | ||
use 5.010; | ||
use DateTime::Format::Strptime; | ||
use Person; | ||
|
||
my $date_parser = DateTime::Format::Strptime->new( | ||
pattern => '%Y-%m-%d', | ||
); | ||
|
||
my $dob = $date_parser->parse_datetime('1962-09-07'); | ||
|
||
my $dave = Person->new( | ||
name => 'Dave', | ||
dob => $dob, | ||
gender => 'M' | ||
); | ||
|
||
say $dave->dob->day_name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/perl | ||
use strict; | ||
use warnings; | ||
use 5.010; | ||
use Person; | ||
|
||
my $dave = Person->new( | ||
name => 'Dave', | ||
dob => '1962-09-07', | ||
gender => 'M' | ||
); | ||
|
||
say $dave->dob->day_name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/perl | ||
use strict; | ||
use warnings; | ||
use 5.010; | ||
use Person; | ||
|
||
my $dave = Person->new( | ||
name => 'Dave', | ||
dob => '1962-09-07', | ||
gender => 'X' | ||
); | ||
|
||
say $dave->dob->day_name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/perl | ||
use strict; | ||
use warnings; | ||
use 5.010; | ||
use Person; | ||
|
||
my $dave = Person->new( | ||
name => 'Dave', | ||
dob => '1962-09-07', | ||
gender => 'M' | ||
); | ||
|
||
say $dave->age; |