fix some typos reported by acme
[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
3ac9038a 34Loading Moose also turns enables C<strict> and C<warnings> pragmas in
35your class.
6c40a460 36
37When you load Moose, your class will become a subclass of
38L<Moose::Object>. The L<Moose::Object> class provides a default
39constructor, destructor, as well as object construction helper
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
3ac9038a 86This deletes Moose's sugar functions from your class's namespace, so
6c40a460 87that C<< Person->can('has') >> will no longer return true.
88
89=head1 MAKING IT FASTER
90
91Moose has a feature called "immutabilization" that you can use to
92greatly speed up your classes at runtime. However, using it does incur
93a cost when your class is first being loaded. When you make your class
3ac9038a 94immutable you tell Moose that you will not be changing it in the
95future. You will not adding any more attributes, methods, roles, etc.
6c40a460 96
3ac9038a 97This allows Moose to generate code specific to your class. In
98particular, it creates an "inline" constructor, making object
99construction much faster.
6c40a460 100
101To make your class immutable you simply call C<make_immutable> on your
102class's metaclass object.
103
3ac9038a 104 __PACKAGE__->meta->make_immutable;
105
6c40a460 106=head1 AUTHOR
107
108Dave Rolsky E<lt>autarch@urth.orgE<gt>
109
110=head1 COPYRIGHT AND LICENSE
111
3ac9038a 112Copyright 2008-2009 by Infinity Interactive, Inc.
6c40a460 113
114L<http://www.iinteractive.com>
115
116This library is free software; you can redistribute it and/or modify
117it under the same terms as Perl itself.
118
119=cut