From: Jesse Luehrs Date: Sat, 7 May 2011 07:04:56 +0000 (-0500) Subject: simplify a bit by giving this a default X-Git-Tag: 2.0100~135 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3b322deeadd12b026265eb031ea77807af8f61a9;p=gitmo%2FMoose.git simplify a bit by giving this a default --- diff --git a/lib/Moose/Cookbook/Basics/Recipe4.pod b/lib/Moose/Cookbook/Basics/Recipe4.pod index 58a935b..65fbb03 100644 --- a/lib/Moose/Cookbook/Basics/Recipe4.pod +++ b/lib/Moose/Cookbook/Basics/Recipe4.pod @@ -50,18 +50,23 @@ use Test::Requires { has 'name' => ( is => 'rw', isa => 'Str', required => 1 ); has 'address' => ( is => 'rw', isa => 'Address' ); - has 'employees' => ( is => 'rw', isa => 'ArrayRef[Employee]' ); + has 'employees' => ( + is => 'rw', + isa => 'ArrayRef[Employee]' + default => sub { [] }, + ); sub BUILD { my ( $self, $params ) = @_; - foreach my $employee ( @{ $self->employees || [] } ) { + foreach my $employee ( @{ $self->employees } ) { $employee->employer($self); } } after 'employees' => sub { my ( $self, $employees ) = @_; - foreach my $employee ( @{ $employees || [] } ) { + return unless $employees; + foreach my $employee ( @$employees ) { $employee->employer($self); } }; @@ -175,12 +180,16 @@ constraint allows that. The next attribute, C, uses a I type constraint: - has 'employees' => ( is => 'rw', isa => 'ArrayRef[Employee]' ); + has 'employees' => ( + is => 'rw', + isa => 'ArrayRef[Employee]' + default => sub { [] }, + ); This constraint says that C must be an array reference where each element of the array is an C object. It's worth noting that an I array reference also satisfies this -constraint. +constraint, such as the value given as the default here. Parameterizable type constraints (or "container types"), such as C, can be made more specific with a type parameter. In @@ -209,7 +218,7 @@ C attribute: sub BUILD { my ( $self, $params ) = @_; - foreach my $employee ( @{ $self->employees || [] } ) { + foreach my $employee ( @{ $self->employees } ) { $employee->employer($self); } } @@ -227,7 +236,8 @@ To do this we can use an C modifier: after 'employees' => sub { my ( $self, $employees ) = @_; - foreach my $employee ( @{ $employees || [] } ) { + return unless $employees; + foreach my $employee ( @$employees ) { $employee->employer($self); } }; @@ -236,6 +246,9 @@ Again, as with the C method, we know that the type constraint check has already happened, so we know that if C<$employees> is defined it will contain an array reference of C objects. +Note that C is a read/write accessor, so we must return early if +it's called as a reader. + The B class does not really demonstrate anything new. It has several C attributes. It also has a C method, which we first used in L.