X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FCookbook%2FRecipe4.pod;h=09ad5ec3c9c86101543b088dea14e4043c381573;hb=a0542df9dfb565447bb2d2b84f7bb4838b7ad6f2;hp=11b0f0eece17c3863a204367adf290ba565c69f4;hpb=2f04a0fc6f8f3a3fb93a899a6194ad8ccd6ca304;p=gitmo%2FMoose.git diff --git a/lib/Moose/Cookbook/Recipe4.pod b/lib/Moose/Cookbook/Recipe4.pod index 11b0f0e..09ad5ec 100644 --- a/lib/Moose/Cookbook/Recipe4.pod +++ b/lib/Moose/Cookbook/Recipe4.pod @@ -40,9 +40,7 @@ Moose::Cookbook::Recipe4 - Subtypes, and modeling a simple B class hier has 'name' => (is => 'rw', isa => 'Str', required => 1); has 'address' => (is => 'rw', isa => 'Address'); - has 'employees' => (is => 'rw', isa => subtype ArrayRef => where { - (blessed($_) && $_->isa('Employee') || return) for @$_; 1 - }); + has 'employees' => (is => 'rw', isa => 'ArrayRef[Employee]'); sub BUILD { my ($self, $params) = @_; @@ -170,14 +168,13 @@ will always have a value. The next attribute option is not actually new, but a new variant of options we have already introduced: - has 'employees' => (is => 'rw', isa => subtype ArrayRef => where { - (blessed($_) && $_->isa('Employee') || return) for @$_; 1 - }); - -Here, instead of passing a string to the C option, we are passing -an anonymous subtype of the C type constraint. This subtype -basically checks that all the values in the ARRAY ref are instances of -the B class. + has 'employees' => (is => 'rw', isa => 'ArrayRef[Employee]'); + +Here, we are passing a more complex string to the C option, we +are passing a container type constraint. Container type constraints +can either be C or C with a contained type given +inside the square brackets. This basically checks that all the values +in the ARRAY ref are instances of the B class. This will ensure that our employees will all be of the correct type. However, the B object (which we will see in a moment) also maintains a