Mention the two accessor naming MX modules
[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
6c40a460 37The metaclass object an introspection API for your class. It is also
38used by Moose itself under the hood to add attributes, define parent
39classes, and so on. In fact, all of Moose's sugar does the real work
40by calling methods on this metaclass object (and other meta level
41objects).
42
43When you load Moose, your class will become a subclass of
44L<Moose::Object>. The L<Moose::Object> class provides a default
45constructor, destructor, as well as object construction helper
46methods. You can read more about this in the
47L<Moose::Manual::Construction> document.
48
49As a convenience, Moose creates a new class type with the name of your
50class, by calling the C<class_type> function in
51L<Moose::Util::Constraints>. See the L<Moose::Manual::Types> document
52for more about types.
53
056348be 54It also creates a L<Moose::Meta::Class> object for your class. This
55metaclass object is now available by calling a C<meta> method on your
56class, for example C<< Person->meta >>.
57
6c40a460 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
6c40a460 108=head1 AUTHOR
109
110Dave Rolsky E<lt>autarch@urth.orgE<gt>
111
112=head1 COPYRIGHT AND LICENSE
113
114Copyright 2008 by Infinity Interactive, Inc.
115
116L<http://www.iinteractive.com>
117
118This library is free software; you can redistribute it and/or modify
119it under the same terms as Perl itself.
120
121=cut