X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FCookbook%2FRecipe2.pod;h=bbd4788fdc78b99895157dd31e4919f96286880e;hb=43b50af34c88c00f35c97282cfcb8f5cd5bd81c4;hp=04c5613ccae9492f29e4a8b022b253253890b121;hpb=703d9522a59b696a3ade220de01eed471d7df5af;p=gitmo%2FMoose.git diff --git a/lib/Moose/Cookbook/Recipe2.pod b/lib/Moose/Cookbook/Recipe2.pod index 04c5613..bbd4788 100644 --- a/lib/Moose/Cookbook/Recipe2.pod +++ b/lib/Moose/Cookbook/Recipe2.pod @@ -3,13 +3,11 @@ =head1 NAME -Moose::Cookbook::Recipe2 - A simple Bank Account example +Moose::Cookbook::Recipe2 - A simple B example =head1 SYNOPSIS package BankAccount; - use strict; - use warnings; use Moose; has 'balance' => (isa => 'Int', is => 'rw', default => 0); @@ -28,8 +26,6 @@ Moose::Cookbook::Recipe2 - A simple Bank Account example } package CheckingAccount; - use strict; - use warnings; use Moose; extends 'BankAccount'; @@ -39,7 +35,7 @@ Moose::Cookbook::Recipe2 - A simple Bank Account example before 'withdraw' => sub { my ($self, $amount) = @_; my $overdraft_amount = $amount - $self->balance(); - if (self->overdraft_account && $overdraft_amount > 0) { + if ($self->overdraft_account && $overdraft_amount > 0) { $self->overdraft_account->withdraw($overdraft_amount); $self->deposit($overdraft_amount); } @@ -68,10 +64,10 @@ new attribute feature, that of a default value. has 'balance' => (isa => 'Int', is => 'rw', default => 0); -This tells is that a B has a C attribute, -which is has the C type constraint, a read/write accessor, +This tells us that a B has a C attribute, +which has the C type constraint, a read/write accessor, and a default value of C<0>. This means that every instance of -B that is created will have it's C slot +B that is created will have its C slot initialized to C<0>. Very simple really :) Next come the methods. The C and C methods @@ -95,7 +91,7 @@ you define, a corresponding type constraint will be created for that class. This means that in the first recipe, a C and C type constraint were created, and in this recipe, both a C and a C type constraint were -created. Moose does this as a convience for you so that your +created. Moose does this as a convenience for you so that your class model and the type constraint model can both be kept in sync with one another. In short, Moose makes sure that it will just DWIM (1). @@ -107,7 +103,7 @@ modifier. before 'withdraw' => sub { my ($self, $amount) = @_; my $overdraft_amount = $amount - $self->balance(); - if (self->overdraft_account && $overdraft_amount > 0) { + if ($self->overdraft_account && $overdraft_amount > 0) { $self->overdraft_account->withdraw($overdraft_amount); $self->deposit($overdraft_amount); } @@ -130,7 +126,7 @@ pseudo-package. So the above method is equivalent to the one here. sub withdraw { my ($self, $amount) = @_; my $overdraft_amount = $amount - $self->balance(); - if ($overdraft_amount > 0 && $self->overdraft_account) { + if ($self->overdraft_account && $overdraft_amount > 0) { $self->overdraft_account->withdraw($overdraft_amount); $self->deposit($overdraft_amount); } @@ -142,13 +138,13 @@ author of the B subclass does not need to remember to call C and to pass it the C<$amount> argument. Instead the method modifier assures that all arguments make it to the superclass method correctly. But this is actually more -than just a convience for forgetful programmers, it also helps +than just a convenience for forgetful programmers, it also helps isolate subclasses from changes in the superclasses. For instance, if B were to add an additional argument of some kind, the version of B which uses C would not pass that extra argument -correctly. Whereas the method modifier version of would pass -all arguments along correctly automatically. +correctly. Whereas the method modifier version would automatically pass +along all arguments correctly. Just as with the first recipe, object instantiation is a fairly normal process, here is an example: @@ -160,7 +156,7 @@ normal process, here is an example: ); And as with the first recipe, a more in-depth example of using -these classes can be found in the F test file. +these classes can be found in the F test file. =head1 CONCLUSION @@ -207,11 +203,11 @@ Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE -Copyright 2006 by Infinity Interactive, Inc. +Copyright 2006, 2007 by Infinity Interactive, Inc. L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. -=cut \ No newline at end of file +=cut