Guest User

Untitled

a guest
Nov 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. package Course;
  2. use Moose;
  3.  
  4. has 'students' => (
  5. is => 'ro',
  6. isa => 'ArrayRef[Student]',
  7. default => sub { [] },
  8. );
  9.  
  10. sub add_student {
  11. my $self = shift;
  12. my @new_students = @_;
  13.  
  14. $self->meta->get_attribute('students')
  15. ->type_constraint->assert_valid(\@new_students);
  16.  
  17. $_->learns_at( $self ) for @new_students;
  18. push $self->students, @new_students;
  19. }
  20.  
  21. package Student;
  22. use Moose;
  23. has 'name', is => 'ro', required => 1;
  24. has 'learns_at', is => 'rw', weak_ref => 1;
  25.  
  26. package main;
  27. my $c = Course->new;
  28.  
  29. $c->add_student( Student->new( name => 'Mike') );
  30. warn $c->dump;
Add Comment
Please, Sign In to add comment