More cleanup in native documentation
[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
3ac9038a 15That's it, you've made a class with Moose!
6c40a460 16
17There's actually a lot going on here under the hood, so let's step
3ac9038a 18through it.
6c40a460 19
3ac9038a 20When you load L<Moose>, a bunch of sugar functions are exported into
6c40a460 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
dab94063 34Loading Moose also enables C<strict> and C<warnings> pragmas in your
35class.
6c40a460 36
37When you load Moose, your class will become a subclass of
38L<Moose::Object>. The L<Moose::Object> class provides a default
dab94063 39constructor and destructor, as well as object construction helper
6c40a460 40methods. You can read more about this in the
41L<Moose::Manual::Construction> document.
42
3ac9038a 43As a convenience, Moose creates a new class type for your class. See
44the L<Moose::Manual::Types> document to learn more about types.
6c40a460 45
056348be 46It also creates a L<Moose::Meta::Class> object for your class. This
47metaclass object is now available by calling a C<meta> method on your
48class, for example C<< Person->meta >>.
49
3ac9038a 50The metaclass object provides an introspection API for your class. It
51is also used by Moose itself under the hood to add attributes, define
52parent classes, and so on. In fact, all of Moose's sugar does the real
53work by calling methods on this metaclass object (and other meta API
54objects).
55
6c40a460 56=head1 SUBCLASSING
57
58Moose provides a simple sugar function for declaring your parent
59classes, C<extends>:
60
61 package User;
62
63 use Moose;
64
65 extends 'Person';
66
67 has 'username' => ( is => 'rw' );
68
0f62a437 69Note that each call to C<extends> will I<reset> your parents. For
3ac9038a 70multiple inheritance you must provide all the parents at once,
71C<extends 'Foo', 'Bar'>.
6c40a460 72
73You can use Moose to extend a non-Moose parent. However, when you do
74this, you will inherit the parent class's constructor (assuming it is
75also called C<new>). In that case, you will have to take care of
76initializing attributes manually, either in the parent's constructor,
3ac9038a 77or in your subclass, and you will lose a lot of Moose magic.
6c40a460 78
79=head1 NO MOOSE
80
81Moose also allows you to remove its sugar functions from your class's
82namespace. We recommend that you take advantage of this feature, since
83it just makes your classes "cleaner". You can do this by simply adding
84C<no Moose> at the end of your module file.
85
adf8494e 86 package Person;
87
88 use Moose;
89
90 has 'ssn' => ( is => 'rw' );
91
92 no Moose;
93
3ac9038a 94This deletes Moose's sugar functions from your class's namespace, so
6c40a460 95that C<< Person->can('has') >> will no longer return true.
96
97=head1 MAKING IT FASTER
98
99Moose has a feature called "immutabilization" that you can use to
100greatly speed up your classes at runtime. However, using it does incur
101a cost when your class is first being loaded. When you make your class
3ac9038a 102immutable you tell Moose that you will not be changing it in the
103future. You will not adding any more attributes, methods, roles, etc.
6c40a460 104
3ac9038a 105This allows Moose to generate code specific to your class. In
106particular, it creates an "inline" constructor, making object
107construction much faster.
6c40a460 108
109To make your class immutable you simply call C<make_immutable> on your
110class's metaclass object.
111
3ac9038a 112 __PACKAGE__->meta->make_immutable;
113
1a5d5ecd 114=head2 Immutabilization and C<new()>
115
116If you override C<new()> in your class, then the immutabilization code
117will not be able to provide an optimized constructor for your
dab94063 118class. Instead, you should use a C<BUILD()> method, which will be
119called from the inlined constructor.
1a5d5ecd 120
121Alternately, if you really need to provide a different C<new()>, you
122can also provide your own immutabilization method. Doing so requires
123extending the Moose metaclasses, and is well beyond the scope of this
124manual.
125
6c40a460 126=head1 AUTHOR
127
128Dave Rolsky E<lt>autarch@urth.orgE<gt>
129
130=head1 COPYRIGHT AND LICENSE
131
3ac9038a 132Copyright 2008-2009 by Infinity Interactive, Inc.
6c40a460 133
134L<http://www.iinteractive.com>
135
136This library is free software; you can redistribute it and/or modify
137it under the same terms as Perl itself.
138
139=cut