Created
July 1, 2012 20:09
-
-
Save ynonp/3029432 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package Starship; | |
use Moose; | |
has 'captain', is => 'ro', isa => 'Str', required => 1; | |
has 'crew', is => 'rw', isa => 'ArrayRef[Str]', required => 1; | |
sub BUILD { | |
my $self = shift; | |
if ( $self->captain ~~ $self->crew ) { | |
my $captain = $self->captain; | |
die "Validation Error: Cannot use $captain for both Captain and Crew"; | |
} | |
} | |
package Person; | |
use Moose; | |
has 'name', is => 'ro', isa => 'Str', required => 1; | |
around BUILDARGS => sub { | |
my $orig = shift; | |
my $class = shift; | |
my @params = @_; | |
# Sole parameter that is not a ref | |
# is considered the name | |
if ( ( @params == 1 ) && ( ! ref $params[0] ) ) { | |
return $class->$orig( name => $params[0] ); | |
} else { | |
return $class->$orig ( @params ); | |
} | |
}; # Watch the semicolon | |
package main; | |
# Validation error | |
my $enterprise = Starship->new( { | |
captain => 'James T Kirk', | |
crew => ['Dr. McCoy', 'Scott', 'Lt. Uhura' ], | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment