Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created July 1, 2012 20:09
Show Gist options
  • Save ynonp/3029432 to your computer and use it in GitHub Desktop.
Save ynonp/3029432 to your computer and use it in GitHub Desktop.
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