Add first draft of classes manual document
[gitmo/Moose.git] / lib / Moose / Manual / Classes.pod
CommitLineData
6c40a460 1=pod
2
3=head1 NAME
4
5Moose::Manual::Classes - Making your classes use Moose (and subclassing)
6
7=head1 USING MOOSE
8
9Using Moose is very simple, you just C<use Moose>:
10
11 package Person;
12
13 use Moose;
14
15That's it, you've now made a Moose-based class!
16
17There's actually a lot going on here under the hood, so let's step
18through it. The L<Moose> package does several things when you load it.
19
20When you load Moose, you get a bunch of sugar functions exported into
21your class. These include things like C<extends>, C<has>, C<with>, and
22more. These functions are what you use to define your class. For
23example, you might define an attribute ...
24
25 package Person;
26
27 use Moose;
28
29 has 'ssn' => ( is => 'rw' );
30
31Attributes are described in the L<Moose::Manual::Attributes>
32documentation.
33
34Loading Moose in your class also turns on the C<strict> and
35C<warnings> pragmas in your class for you.
36
37It also creates a L<Moose::Meta::Class> object for your class. This
38metaclass object is now available by calling a C<meta> method on your
39class, for example C<< Person->meta >>.
40
41The metaclass object an introspection API for your class. It is also
42used by Moose itself under the hood to add attributes, define parent
43classes, and so on. In fact, all of Moose's sugar does the real work
44by calling methods on this metaclass object (and other meta level
45objects).
46
47When you load Moose, your class will become a subclass of
48L<Moose::Object>. The L<Moose::Object> class provides a default
49constructor, destructor, as well as object construction helper
50methods. You can read more about this in the
51L<Moose::Manual::Construction> document.
52
53As a convenience, Moose creates a new class type with the name of your
54class, by calling the C<class_type> function in
55L<Moose::Util::Constraints>. See the L<Moose::Manual::Types> document
56for more about types.
57
58=head1 SUBCLASSING
59
60Moose provides a simple sugar function for declaring your parent
61classes, C<extends>:
62
63 package User;
64
65 use Moose;
66
67 extends 'Person';
68
69 has 'username' => ( is => 'rw' );
70
71When you call extends, Moose takes the class(es) you provide and makes
72those the parent of the current class. Note, that each call to
73C<extends> will I<reset> your parents, so for multiple inheritance you
74should provide all you parents at once, C<extends 'Foo', 'Bar'>.
75
76You can use Moose to extend a non-Moose parent. However, when you do
77this, you will inherit the parent class's constructor (assuming it is
78also called C<new>). In that case, you will have to take care of
79initializing attributes manually, either in the parent's constructor,
80or in your subclass, and you will generally lose a lot of Moose magic.
81
82=head1 NO MOOSE
83
84Moose also allows you to remove its sugar functions from your class's
85namespace. We recommend that you take advantage of this feature, since
86it just makes your classes "cleaner". You can do this by simply adding
87C<no Moose> at the end of your module file.
88
89What this does is delete the functions from your class's namespace, so
90that C<< Person->can('has') >> will no longer return true.
91
92=head1 MAKING IT FASTER
93
94Moose has a feature called "immutabilization" that you can use to
95greatly speed up your classes at runtime. However, using it does incur
96a cost when your class is first being loaded. When you make your class
97immutable you tell Moose that you will not be changing it,
98specifically not adding any attributes, methods, roles, etc.
99
100This allows Moose to generate code specific to your class for its
101constructor and other methods, making object construction much
102faster. It also makes some of the introspection methods faster as
103well.
104
105To make your class immutable you simply call C<make_immutable> on your
106class's metaclass object.
107
108=head1 RECOMMENDATIONS
109
110We recommend that you end your Moose class definitions by removing the
111Moose sugar and making your class immutable.
112
113 package Person;
114
115 use Moose;
116
117 # extends, roles, attributes, etc.
118
119 # methods
120
121 no Moose;
122
123 __PACKAGE__->meta->make_immutable;
124
125 1;
126
127=head1 AUTHOR
128
129Dave Rolsky E<lt>autarch@urth.orgE<gt>
130
131=head1 COPYRIGHT AND LICENSE
132
133Copyright 2008 by Infinity Interactive, Inc.
134
135L<http://www.iinteractive.com>
136
137This library is free software; you can redistribute it and/or modify
138it under the same terms as Perl itself.
139
140=cut
141