Skip to content

Instantly share code, notes, and snippets.

@sheeju
Forked from ynonp/76.pl
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheeju/b1941a7c5063b40dfaa8 to your computer and use it in GitHub Desktop.
Save sheeju/b1941a7c5063b40dfaa8 to your computer and use it in GitHub Desktop.
package Course;
use Moose;
has 'students' => (
is => 'ro',
isa => 'ArrayRef[Student]',
default => sub { [] },
);
sub add_student {
my $self = shift;
my @new_students = @_;
$self->meta->get_attribute('students')
->type_constraint->assert_valid(\@new_students);
$_->learns_at( $self ) for @new_students;
push $self->students, @new_students;
}
package Student;
use Moose;
has 'name', is => 'ro', required => 1;
has 'learns_at', is => 'rw', weak_ref => 1;
package main;
my $c = Course->new;
$c->add_student( Student->new( name => 'Mike') );
warn $c->dump;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment