Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created July 1, 2012 18:21
Show Gist options
  • Save ynonp/3029164 to your computer and use it in GitHub Desktop.
Save ynonp/3029164 to your computer and use it in GitHub Desktop.
package Pet;
use Moose;
has 'name', is => 'ro', isa => 'Str', default => 'Nemo';
has 'past_owners', is => 'ro', isa => 'ArrayRef[Str]';
package main;
my $dog = Pet->new( past_owners => ['James', 'Mike'] );
# show dog's info. No need to import Data::Dumper
warn $dog->dump;
# DOES returns true for objects of the class, subclasses or
# implementors of the role
print "Good boy" if $dog->DOES('Pet');
package Starship;
use Moose;
has 'captain', is => 'ro', isa => 'Str', required => 1;
has 'crew', is => 'rw', isa => 'ArrayRef[Str]', required => 1;
package main;
# Pass a hash ref to prevent copying
my $enterprise = Starship->new( {
captain => 'James T Kirk',
crew => ['Dr. McCoy', 'Scott', 'Lt. Uhura'],
});
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 main;
# Validation error
my $enterprise = Starship->new( {
captain => 'James T Kirk',
crew => ['Dr. McCoy', 'Scott', 'Lt. Uhura', 'James T Kirk'],
});
gist: 3028759
gist: 3028758
gist: cff21b913058a58389a9
gist: 26bb64c76d5e40c5a525 moose
gist: 3009833 Bash Syntax Exercises
gist: 2877490
gist: 2862674 Case bash example
gist: 2794368 indented heredoc example
gist: 2773724
gist: 2633347 cool zombies
gist: 2566283 Unix Ex1
gist: 66c84a9c27d246121ed1
gist: e0ac3821bfae6369d042 anagrams.pl
gist: 33c6da4d24c9723a6881 golf
gist: 2486898 perl default var
gist: 2466264 html file
gist: 2462852 filters out expired certificates
gist: 1f15ffcde0264210efe2 golf head
gist: 2225265
gist: 2224433 permutations game
gist: 2200399 given/when bug
gist: ada7710c6892198fcfbc
gist: 86bcf93bcc2f785ea0c6
gist: e5e39c67de37d45c9385
gist: 066e4a295e740d158203
gist: 26c01f8da1166528d1cd
gist: 1862134
gist: ffdda8771a9107d86406
gist: 43ee98ce565415e359e6
gist: 03de80c73cb1a0cdf8f8
more...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment