1Moose::Manual::Classes(U3s)er Contributed Perl DocumentatMiooonse::Manual::Classes(3)
2
3
4

NAME

6       Moose::Manual::Classes - Making your classes use Moose (and
7       subclassing)
8

USING MOOSE

10       Using Moose is very simple, you just "use Moose":
11
12         package Person;
13
14         use Moose;
15
16       That's it, you've made a class with Moose!
17
18       There's actually a lot going on here under the hood, so let's step
19       through it.
20
21       When you load Moose, a bunch of sugar functions are exported into your
22       class. These include things like "extends", "has", "with", and more.
23       These functions are what you use to define your class. For example, you
24       might define an attribute ...
25
26         package Person;
27
28         use Moose;
29
30         has 'ssn' => ( is => 'rw' );
31
32       Attributes are described in the Moose::Manual::Attributes
33       documentation.
34
35       Loading Moose also enables "strict" and "warnings" pragmas in your
36       class.
37
38       When you load Moose, your class will become a subclass of
39       Moose::Object. The Moose::Object class provides a default constructor
40       and destructor, as well as object construction helper methods. You can
41       read more about this in the Moose::Manual::Construction document.
42
43       As a convenience, Moose creates a new class type for your class. See
44       the Moose::Manual::Types document to learn more about types.
45
46       It also creates a Moose::Meta::Class object for your class. This
47       metaclass object is now available by calling a "meta" method on your
48       class, for example "Person->meta".
49
50       The metaclass object provides an introspection API for your class. It
51       is also used by Moose itself under the hood to add attributes, define
52       parent classes, and so on. In fact, all of Moose's sugar does the real
53       work by calling methods on this metaclass object (and other meta API
54       objects).
55

SUBCLASSING

57       Moose provides a simple sugar function for declaring your parent
58       classes, "extends":
59
60         package User;
61
62         use Moose;
63
64         extends 'Person';
65
66         has 'username' => ( is => 'rw' );
67
68       Note that each call to "extends" will reset your parents. For multiple
69       inheritance you must provide all the parents at once, "extends 'Foo',
70       'Bar'".
71
72       You can use Moose to extend a non-Moose parent. However, when you do
73       this, you will inherit the parent class's constructor (assuming it is
74       also called "new"). In that case, you will have to take care of
75       initializing attributes manually, either in the parent's constructor,
76       or in your subclass, and you will lose a lot of Moose magic.
77

NO MOOSE

79       Moose also allows you to remove its sugar functions from your class's
80       namespace. We recommend that you take advantage of this feature, since
81       it just makes your classes "cleaner". You can do this by simply adding
82       "no Moose" at the end of your module file.
83
84         package Person;
85
86         use Moose;
87
88         has 'ssn' => ( is => 'rw' );
89
90         no Moose;
91
92       This deletes Moose's sugar functions from your class's namespace, so
93       that "Person->can('has')" will no longer return true.
94
95       A more generic way to unimport not only Moose's exports but also those
96       from type libraries and other modules is to use namespace::clean or
97       namespace::autoclean.
98

MAKING IT FASTER

100       Moose has a feature called "immutabilization" that you can use to
101       greatly speed up your classes at runtime. However, using it does incur
102       a cost when your class is first being loaded. When you make your class
103       immutable you tell Moose that you will not be changing it in the
104       future. You will not be adding any more attributes, methods, roles,
105       etc.
106
107       This allows Moose to generate code specific to your class. In
108       particular, it creates an "inline" constructor, making object
109       construction much faster.
110
111       To make your class immutable you simply call "make_immutable" on your
112       class's metaclass object.
113
114         __PACKAGE__->meta->make_immutable;
115
116   Immutabilization and "new()"
117       If you override "new()" in your class, then the immutabilization code
118       will not be able to provide an optimized constructor for your class.
119       Instead, you should use a "BUILD()" method, which will be called from
120       the inlined constructor.
121
122       Alternately, if you really need to provide a different "new()", you can
123       also provide your own immutabilization method. Doing so requires
124       extending the Moose metaclasses, and is well beyond the scope of this
125       manual.
126

AUTHOR

128       Dave Rolsky <autarch@urth.org>
129
131       Copyright 2008-2009 by Infinity Interactive, Inc.
132
133       <http://www.iinteractive.com>
134
135       This library is free software; you can redistribute it and/or modify it
136       under the same terms as Perl itself.
137
138
139
140perl v5.12.2                      2010-08-20         Moose::Manual::Classes(3)
Impressum