1Mouse(3) User Contributed Perl Documentation Mouse(3)
2
3
4
6 Mouse - Moose minus the antlers
7
9 This document describes Mouse version 0.58
10
12 package Point;
13 use Mouse; # automatically turns on strict and warnings
14
15 has 'x' => (is => 'rw', isa => 'Int');
16 has 'y' => (is => 'rw', isa => 'Int');
17
18 sub clear {
19 my $self = shift;
20 $self->x(0);
21 $self->y(0);
22 }
23
24
25 __PACKAGE__->meta->make_immutable();
26
27 package Point3D;
28 use Mouse;
29
30 extends 'Point';
31
32 has 'z' => (is => 'rw', isa => 'Int');
33
34 after 'clear' => sub {
35 my $self = shift;
36 $self->z(0);
37 };
38
39 __PACKAGE__->meta->make_immutable();
40
42 Moose is wonderful. Use Moose instead of Mouse.
43
44 Unfortunately, Moose has a compile-time penalty. Though significant
45 progress has been made over the years, the compile time penalty is a
46 non-starter for some very specific applications. If you are writing a
47 command-line application or CGI script where startup time is essential,
48 you may not be able to use Moose. We recommend that you instead use
49 HTTP::Engine and FastCGI for the latter, if possible.
50
51 Mouse aims to alleviate this by providing a subset of Moose's
52 functionality, faster.
53
54 We're also going as light on dependencies as possible. Mouse currently
55 has no dependencies except for testing modules.
56
57 MOOSE COMPATIBILITY
58 Compatibility with Moose has been the utmost concern. The sugary
59 interface is highly compatible with Moose. Even the error messages are
60 taken from Moose. The Mouse code just runs the test suite 4x faster.
61
62 The idea is that, if you need the extra power, you should be able to
63 run "s/Mouse/Moose/g" on your codebase and have nothing break. To that
64 end, we have written Any::Moose which will act as Mouse unless Moose is
65 loaded, in which case it will act as Moose. Since Mouse is a little
66 sloppier than Moose, if you run into weird errors, it would be worth
67 running:
68
69 ANY_MOOSE=Moose perl your-script.pl
70
71 to see if the bug is caused by Mouse. Moose's diagnostics and
72 validation are also better.
73
74 See also Mouse::Spec for compatibility and incompatibility with Moose.
75
76 MouseX
77 Please don't copy MooseX code to MouseX. If you need extensions, you
78 really should upgrade to Moose. We don't need two parallel sets of
79 extensions!
80
81 If you really must write a Mouse extension, please contact the Moose
82 mailing list or #moose on IRC beforehand.
83
85 "$object->meta -> Mouse::Meta::Class"
86 Returns this class' metaclass instance.
87
88 "extends superclasses"
89 Sets this class' superclasses.
90
91 "before (method|methods|regexp) => CodeRef"
92 Installs a "before" method modifier. See "before" in Moose.
93
94 "after (method|methods|regexp) => CodeRef"
95 Installs an "after" method modifier. See "after" in Moose.
96
97 "around (method|methods|regexp) => CodeRef"
98 Installs an "around" method modifier. See "around" in Moose.
99
100 "has (name|names) => parameters"
101 Adds an attribute (or if passed an arrayref of names, multiple
102 attributes) to this class. Options:
103
104 "is => ro|rw|bare"
105 The is option accepts either rw (for read/write), ro (for read
106 only) or bare (for nothing). These will create either a read/write
107 accessor or a read-only accessor respectively, using the same name
108 as the $name of the attribute.
109
110 If you need more control over how your accessors are named, you can
111 use the "reader", "writer" and "accessor" options, however if you
112 use those, you won't need the is option.
113
114 "isa => TypeName | ClassName"
115 Provides type checking in the constructor and accessor. The
116 following types are supported. Any unknown type is taken to be a
117 class check (e.g. "isa => 'DateTime'" would accept only DateTime
118 objects).
119
120 Any Item Bool Undef Defined Value Num Int Str ClassName
121 Ref ScalarRef ArrayRef HashRef CodeRef RegexpRef GlobRef
122 FileHandle Object
123
124 For more documentation on type constraints, see
125 Mouse::Util::TypeConstraints.
126
127 "does => RoleName"
128 This will accept the name of a role which the value stored in this
129 attribute is expected to have consumed.
130
131 "coerce => Bool"
132 This will attempt to use coercion with the supplied type constraint
133 to change the value passed into any accessors or constructors. You
134 must have supplied a type constraint in order for this to work. See
135 Moose::Cookbook::Basics::Recipe5 for an example.
136
137 "required => Bool"
138 Whether this attribute is required to have a value. If the
139 attribute is lazy or has a builder, then providing a value for the
140 attribute in the constructor is optional.
141
142 "init_arg => Str | Undef"
143 Allows you to use a different key name in the constructor. If
144 undef, the attribute can't be passed to the constructor.
145
146 "default => Value | CodeRef"
147 Sets the default value of the attribute. If the default is a
148 coderef, it will be invoked to get the default value. Due to quirks
149 of Perl, any bare reference is forbidden, you must wrap the
150 reference in a coderef. Otherwise, all instances will share the
151 same reference.
152
153 "lazy => Bool"
154 If specified, the default is calculated on demand instead of in the
155 constructor.
156
157 "predicate => Str"
158 Lets you specify a method name for installing a predicate method,
159 which checks that the attribute has a value. It will not invoke a
160 lazy default or builder method.
161
162 "clearer => Str"
163 Lets you specify a method name for installing a clearer method,
164 which clears the attribute's value from the instance. On the next
165 read, lazy or builder will be invoked.
166
167 "handles => HashRef|ArrayRef|Regexp"
168 Lets you specify methods to delegate to the attribute. ArrayRef
169 forwards the given method names to method calls on the attribute.
170 HashRef maps local method names to remote method names called on
171 the attribute. Other forms of "handles", such as RoleName and
172 CodeRef, are not yet supported.
173
174 "weak_ref => Bool"
175 Lets you automatically weaken any reference stored in the
176 attribute.
177
178 Use of this feature requires Scalar::Util!
179
180 "trigger => CodeRef"
181 Any time the attribute's value is set (either through the accessor
182 or the constructor), the trigger is called on it. The trigger
183 receives as arguments the instance, the new value, and the
184 attribute instance.
185
186 "builder => Str"
187 Defines a method name to be called to provide the default value of
188 the attribute. "builder => 'build_foo'" is mostly equivalent to
189 "default => sub { $_[0]->build_foo }".
190
191 "auto_deref => Bool"
192 Allows you to automatically dereference ArrayRef and HashRef
193 attributes in list context. In scalar context, the reference is
194 returned (NOT the list length or bucket status). You must specify
195 an appropriate type constraint to use auto_deref.
196
197 "lazy_build => Bool"
198 Automatically define the following options:
199
200 has $attr => (
201 # ...
202 lazy => 1
203 builder => "_build_$attr",
204 clearer => "clear_$attr",
205 predicate => "has_$attr",
206 );
207
208 "confess(message) -> BOOM"
209 "confess" in Carp for your convenience.
210
211 "blessed(value) -> ClassName | undef"
212 "blessed" in Scalar::Util for your convenience.
213
215 import
216 Importing Mouse will default your class' superclass list to
217 Mouse::Object. You may use "extends" to replace the superclass list.
218
219 unimport
220 Please unimport Mouse ("no Mouse") so that if someone calls one of the
221 keywords (such as "extends") it will break loudly instead breaking
222 subtly.
223
225 We have a public git repository:
226
227 git clone git://git.moose.perl.org/Mouse.git
228
230 Perl 5.6.2 or later.
231
233 Mouse::Spec
234
235 Moose
236
237 Moose::Manual
238
239 Moose::Cookbook
240
241 Class::MOP
242
244 Shawn M Moore <sartak at gmail.com>
245
246 Yuval Kogman <nothingmuch at woobling.org>
247
248 tokuhirom
249
250 Yappo
251
252 wu-lee
253
254 Goro Fuji (gfx) <gfuji at cpan.org>
255
256 with plenty of code borrowed from Class::MOP and Moose
257
259 All complex software has bugs lurking in it, and this module is no
260 exception. Please report any bugs to "bug-mouse at rt.cpan.org", or
261 through the web interface at
262 <http://rt.cpan.org/Public/Dist/Display.html?Name=Mouse>
263
265 Copyright (c) 2008-2010 Infinity Interactive, Inc.
266
267 http://www.iinteractive.com/
268
269 This program is free software; you can redistribute it and/or modify it
270 under the same terms as Perl itself.
271
272
273
274perl v5.12.0 2010-05-08 Mouse(3)