typo fixes
[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
9e25a72a 97A more generic way to unimport not only L<Moose>'s exports but also
98those from type libraries and other modules is to use
99L<namespace::clean> or L<namespace::autoclean>.
100
6c40a460 101=head1 MAKING IT FASTER
102
103Moose has a feature called "immutabilization" that you can use to
104greatly speed up your classes at runtime. However, using it does incur
105a cost when your class is first being loaded. When you make your class
3ac9038a 106immutable you tell Moose that you will not be changing it in the
9e25a72a 107future. You will not be adding any more attributes, methods, roles, etc.
6c40a460 108
3ac9038a 109This allows Moose to generate code specific to your class. In
110particular, it creates an "inline" constructor, making object
111construction much faster.
6c40a460 112
113To make your class immutable you simply call C<make_immutable> on your
114class's metaclass object.
115
3ac9038a 116 __PACKAGE__->meta->make_immutable;
117
1a5d5ecd 118=head2 Immutabilization and C<new()>
119
120If you override C<new()> in your class, then the immutabilization code
121will not be able to provide an optimized constructor for your
dab94063 122class. Instead, you should use a C<BUILD()> method, which will be
123called from the inlined constructor.
1a5d5ecd 124
125Alternately, if you really need to provide a different C<new()>, you
126can also provide your own immutabilization method. Doing so requires
127extending the Moose metaclasses, and is well beyond the scope of this
128manual.
129
6c40a460 130=head1 AUTHOR
131
132Dave Rolsky E<lt>autarch@urth.orgE<gt>
133
134=head1 COPYRIGHT AND LICENSE
135
3ac9038a 136Copyright 2008-2009 by Infinity Interactive, Inc.
6c40a460 137
138L<http://www.iinteractive.com>
139
140This library is free software; you can redistribute it and/or modify
141it under the same terms as Perl itself.
142
143=cut