Commit | Line | Data |
471c4f09 |
1 | |
2 | =pod |
3 | |
4 | =head1 NAME |
5 | |
6 | Moose::Cookbook::Recipe2 |
7 | |
8 | =head1 SYNOPSIS |
9 | |
10 | package BankAccount; |
11 | use strict; |
12 | use warnings; |
13 | use Moose; |
14 | |
15 | has 'balance' => (isa => 'Int', is => 'rw', default => 0); |
16 | |
17 | sub deposit { |
18 | my ($self, $amount) = @_; |
19 | $self->balance($self->balance + $amount); |
20 | } |
21 | |
22 | sub withdraw { |
23 | my ($self, $amount) = @_; |
24 | my $current_balance = $self->balance(); |
25 | ($current_balance >= $amount) |
26 | || confess "Account overdrawn"; |
27 | $self->balance($current_balance - $amount); |
28 | } |
29 | |
30 | package CheckingAccount; |
31 | use strict; |
32 | use warnings; |
33 | use Moose; |
34 | |
35 | extends 'BankAccount'; |
36 | |
37 | has 'overdraft_account' => (isa => 'BankAccount', is => 'rw'); |
38 | |
39 | before 'withdraw' => sub { |
40 | my ($self, $amount) = @_; |
41 | my $overdraft_amount = $amount - $self->balance(); |
42 | if ($overdraft_amount > 0) { |
43 | $self->overdraft_account->withdraw($overdraft_amount); |
44 | $self->deposit($overdraft_amount); |
45 | } |
46 | }; |
47 | |
48 | =head1 DESCRIPTION |
49 | |
50 | =head1 AUTHOR |
51 | |
52 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
53 | |
54 | =head1 COPYRIGHT AND LICENSE |
55 | |
56 | Copyright 2006 by Infinity Interactive, Inc. |
57 | |
58 | L<http://www.iinteractive.com> |
59 | |
60 | This library is free software; you can redistribute it and/or modify |
61 | it under the same terms as Perl itself. |
62 | |
63 | =cut |