-
-
Save sheeju/b1941a7c5063b40dfaa8 to your computer and use it in GitHub Desktop.
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
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