Explain attributes and subclassing
[gitmo/Moose.git] / lib / Moose / Cookbook / Recipe6.pod
1
2 =pod
3
4 =head1 NAME
5
6 Moose::Cookbook::Recipe6 - The Moose::Role example
7
8 =head1 SYNOPSIS
9
10   package Eq;
11   use Moose::Role;
12   
13   requires 'equal_to';
14   
15   sub not_equal_to { 
16       my ($self, $other) = @_;
17       not $self->equal_to($other);
18   }
19   
20   package Comparable;
21   use Moose::Role;
22   
23   with 'Eq';
24   
25   requires 'compare';
26   
27   sub equal_to {
28       my ($self, $other) = @_;
29       $self->compare($other) == 0;
30   }    
31   
32   sub greater_than {
33       my ($self, $other) = @_;
34       $self->compare($other) == 1;
35   }    
36   
37   sub less_than {
38       my ($self, $other) = @_;
39       $self->compare($other) == -1;
40   }
41   
42   sub greater_than_or_equal_to {
43       my ($self, $other) = @_;
44       $self->greater_than($other) || $self->equal_to($other);
45   }        
46   
47   sub less_than_or_equal_to {
48       my ($self, $other) = @_;
49       $self->less_than($other) || $self->equal_to($other);
50   }  
51   
52   package Printable;
53   use Moose::Role;
54   
55   requires 'to_string';    
56   
57   package US::Currency;
58   use Moose;
59   
60   with 'Comparable', 'Printable';
61   
62   has 'amount' => (is => 'rw', isa => 'Num', default => 0);
63   
64   sub compare {
65       my ($self, $other) = @_;
66       $self->amount <=> $other->amount;
67   }
68   
69   sub to_string {
70       my $self = shift;
71       sprintf '$%0.2f USD' => $self->amount
72   }
73   
74 =head1 DESCRIPTION
75
76 Coming Soon. 
77
78 =head1 AUTHOR
79
80 Stevan Little E<lt>stevan@iinteractive.comE<gt>
81
82 =head1 COPYRIGHT AND LICENSE
83
84 Copyright 2006, 2007 by Infinity Interactive, Inc.
85
86 L<http://www.iinteractive.com>
87
88 This library is free software; you can redistribute it and/or modify
89 it under the same terms as Perl itself.
90
91 =cut       
92