From: Dave Rolsky Date: Mon, 25 Jan 2010 15:43:41 +0000 (-0600) Subject: Really fix the basics recipe 4 code to remove unneeded code, and update the narrative... X-Git-Tag: 0.95~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=922a97e9307800a5cfd9cbae2018db87d60819f3;p=gitmo%2FMoose.git Really fix the basics recipe 4 code to remove unneeded code, and update the narrative text. --- diff --git a/lib/Moose/Cookbook/Basics/Recipe4.pod b/lib/Moose/Cookbook/Basics/Recipe4.pod index 190f62b..f5fd597 100644 --- a/lib/Moose/Cookbook/Basics/Recipe4.pod +++ b/lib/Moose/Cookbook/Basics/Recipe4.pod @@ -56,19 +56,15 @@ Moose::Cookbook::Basics::Recipe4 - Subtypes, and modeling a simple B cl sub BUILD { my ( $self, $params ) = @_; - if ( $self->employees ) { - foreach my $employee ( @{ $self->employees } ) { - $employee->employer($self); - } + foreach my $employee ( @{ $self->employees || [] } ) { + $employee->employer($self); } } after 'employees' => sub { my ( $self, $employees ) = @_; - if ($employees) { - foreach my $employee ( @{$employees} ) { - $employee->employer($self); - } + foreach my $employee ( @{ $employees || [] } ) { + $employee->employer($self); } }; @@ -213,17 +209,15 @@ C attribute: sub BUILD { my ( $self, $params ) = @_; - if ( $self->employees ) { - foreach my $employee ( @{ $self->employees } ) { - $employee->employer($self); - } + foreach my $employee ( @{ $self->employees || [] } ) { + $employee->employer($self); } } -The C method is executed after type constraints are checked, so -it is safe to assume that C<< $self->employees >> will return an array -reference, and that the elements of that array will be C -objects. +The C method is executed after type constraints are checked, so it is +safe to assume that if C<< $self->employees >> has a value, it will be an +array reference, and that the elements of that array reference will be +C objects. We also want to make sure that whenever the C attribute for a C is changed, we also update the C for each @@ -233,16 +227,14 @@ To do this we can use an C modifier: after 'employees' => sub { my ( $self, $employees ) = @_; - if ($employees) { - foreach my $employee ( @{$employees} ) { - $employee->employer($self); - } + foreach my $employee ( @{ $employees || [] } ) { + $employee->employer($self); } }; -Again, as with the C method, we know that the type constraint -check has already happened, so we can just check for definedness on the -C<$employees> argument. +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.. The B class does not really demonstrate anything new. It has several C attributes. It also has a C method, which we