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