1Moose::Manual::Classes(U3s)er Contributed Perl DocumentatMiooonse::Manual::Classes(3)
2
3
4
6 Moose::Manual::Classes - Making your classes use Moose (and
7 subclassing)
8
10 version 2.2203
11
13 Using Moose is very simple, you just "use Moose":
14
15 package Person;
16
17 use Moose;
18
19 That's it, you've made a class with Moose!
20
21 There's actually a lot going on here under the hood, so let's step
22 through it.
23
24 When you load Moose, a bunch of sugar functions are exported into your
25 class, such as "extends", "has", "with", and more. These functions are
26 what you use to define your class. For example, you might define an
27 attribute ...
28
29 package Person;
30
31 use Moose;
32
33 has 'ssn' => ( is => 'rw' );
34
35 Attributes are described in the Moose::Manual::Attributes
36 documentation.
37
38 Loading Moose also enables the "strict" and "warnings" pragmas in your
39 class.
40
41 When you load Moose, your class will become a subclass of
42 Moose::Object. The Moose::Object class provides a default constructor
43 and destructor, as well as object construction helper methods. You can
44 read more about this in the Moose::Manual::Construction document.
45
46 As a convenience, Moose creates a new class type for your class. See
47 the Moose::Manual::Types document to learn more about types.
48
49 It also creates a Moose::Meta::Class object for your class. This
50 metaclass object is now available by calling a "meta" method on your
51 class, for example "Person->meta".
52
53 The metaclass object provides an introspection API for your class. It
54 is also used by Moose itself under the hood to add attributes, define
55 parent classes, and so on. In fact, all of Moose's sugar does the real
56 work by calling methods on this metaclass object (and other meta API
57 objects).
58
60 Moose provides a simple sugar function for declaring your parent
61 classes, "extends":
62
63 package User;
64
65 use Moose;
66
67 extends 'Person';
68
69 has 'username' => ( is => 'rw' );
70
71 Note that each call to "extends" will reset your parents. For multiple
72 inheritance you must provide all the parents at once, "extends 'Foo',
73 'Bar'".
74
75 When you call "extends" Moose will try to load any classes you pass.
76
77 You can use Moose to extend a non-Moose parent. However, when you do
78 this, you will inherit the parent class's constructor (assuming it is
79 also called "new"). In that case, you will have to take care of
80 initializing attributes manually, either in the parent's constructor,
81 or in your subclass, and you will lose a lot of Moose magic.
82
83 See the MooseX::NonMoose module on CPAN if you're interested in
84 extending non-Moose parent classes with Moose child classes.
85
87 Moose exports a number of functions into your class. It's a good idea
88 to remove these sugar functions from your class's namespace, so that
89 "Person->can('has')" will no longer return true.
90
91 There are several ways to do this. We recommend using
92 namespace::autoclean, a CPAN module. Not only will it remove Moose
93 exports, it will also remove any other exports.
94
95 package Person;
96
97 use namespace::autoclean;
98
99 use Moose;
100
101 If you absolutely can't use a CPAN module (but can use Moose?), you can
102 write "no Moose" at the end of your class. This will remove any Moose
103 exports in your class.
104
105 package Person;
106
107 use Moose;
108
109 has 'ssn' => ( is => 'rw' );
110
111 no Moose;
112
114 Moose has a feature called "immutabilization" that you can use to
115 greatly speed up your classes at runtime. However, using it incurs a
116 cost when your class is first being loaded. When you make your class
117 immutable you tell Moose that you will not be changing it in the
118 future. You will not be adding any more attributes, methods, roles,
119 etc.
120
121 This allows Moose to generate code specific to your class. In
122 particular, it creates an "inline" constructor, making object
123 construction much faster.
124
125 To make your class immutable you simply call "make_immutable" on your
126 class's metaclass object.
127
128 __PACKAGE__->meta->make_immutable;
129
130 Immutabilization and new()
131 If you override new() in your class, then the immutabilization code
132 will not be able to provide an optimized constructor for your class.
133 Instead, you should use a BUILD() method, which will be called from the
134 inlined constructor.
135
136 Alternately, if you really need to provide a different new(), you can
137 also provide your own immutabilization method. Doing so requires
138 extending the Moose metaclasses, and is well beyond the scope of this
139 manual.
140
142 When you're ready to use Moose classes in an application, reference
143 them in your code in the regular Perl OO way by including a "use"
144 directive at the top of the file where the objects should be created.
145
146 use Person;
147
148 my $person = Person->new(
149 # attribute values at instantiation
150 # go here
151 ssn => '123456789',
152 );
153
155 • Stevan Little <stevan@cpan.org>
156
157 • Dave Rolsky <autarch@urth.org>
158
159 • Jesse Luehrs <doy@cpan.org>
160
161 • Shawn M Moore <sartak@cpan.org>
162
163 • יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
164
165 • Karen Etheridge <ether@cpan.org>
166
167 • Florian Ragwitz <rafl@debian.org>
168
169 • Hans Dieter Pearcey <hdp@cpan.org>
170
171 • Chris Prather <chris@prather.org>
172
173 • Matt S Trout <mstrout@cpan.org>
174
176 This software is copyright (c) 2006 by Infinity Interactive, Inc.
177
178 This is free software; you can redistribute it and/or modify it under
179 the same terms as the Perl 5 programming language system itself.
180
181
182
183perl v5.36.0 2023-02-06 Moose::Manual::Classes(3)