1Moose::Manual::MooseX(3U)ser Contributed Perl DocumentatiMoonose::Manual::MooseX(3)
2
3
4
6 Moose::Manual::MooseX - Recommended Moose extensions
7
9 version 2.2013
10
12 It's easy to extend and change Moose, and this is part of what makes
13 Moose so powerful. You can use the MOP API to do things your own way,
14 add new features, and generally customize your Moose.
15
16 Writing your own extensions does require a good understanding of the
17 meta-model. You can start learning about this with the
18 Moose::Manual::MOP docs. There are also several extension recipes in
19 the Moose::Cookbook.
20
21 Explaining how to write extensions is beyond the scope of this manual.
22 Fortunately, lots of people have already written extensions and put
23 them on CPAN for you.
24
25 This document covers a few of the ones we like best.
26
28 The functionality of this MooseX module has been moved into Moose core.
29 See Moose::Meta::Attribute::Native.
30
32 MooseX::AttributeHelpers, but turned inside out, Moose::Autobox
33 provides methods on both arrays/hashes/etc. but also references to
34 them, using Moose roles, allowing you do to things like:
35
36 use Moose::Autobox;
37
38 $somebody_elses_object->orders->push($order);
39
40 Lexically scoped and not to everybody's taste, but very handy for
41 sugaring up other people's APIs and your own code.
42
44 By default, Moose lets you pass any old junk into a class's
45 constructor. If you load MooseX::StrictConstructor, your class will
46 throw an error if it sees something it doesn't recognize;
47
48 package User;
49
50 use Moose;
51 use MooseX::StrictConstructor;
52
53 has 'name';
54 has 'email';
55
56 User->new( name => 'Bob', emali => 'bob@example.com' );
57
58 With MooseX::StrictConstructor, that typo ("emali") will cause a
59 runtime error. With plain old Moose, the "emali" attribute would be
60 silently ignored.
61
63 We have high hopes for the future of MooseX::Method::Signatures and
64 Moops. However, these modules, while used regularly in production by
65 some of the more insane members of the community, are still marked
66 alpha just in case backwards incompatible changes need to be made.
67
68 If you don't want to risk that, for now we recommend the decidedly more
69 clunky (but also faster and simpler) MooseX::Params::Validate. This
70 module lets you apply Moose types and coercions to any method
71 arguments.
72
73 package User;
74
75 use Moose;
76 use MooseX::Params::Validate;
77
78 sub login {
79 my $self = shift;
80 my ($password)
81 = validated_list( \@_, password => { isa => 'Str', required => 1 } );
82
83 ...
84 }
85
87 This is a role which adds a "new_with_options" method to your class.
88 This is a constructor that takes the command line options and uses them
89 to populate attributes.
90
91 This makes writing a command-line application as a module trivially
92 simple:
93
94 package App::Foo;
95
96 use Moose;
97 with 'MooseX::Getopt';
98
99 has 'input' => (
100 is => 'ro',
101 isa => 'Str',
102 required => 1
103 );
104
105 has 'output' => (
106 is => 'ro',
107 isa => 'Str',
108 required => 1
109 );
110
111 sub run { ... }
112
113 Then in the script that gets run we have:
114
115 use App::Foo;
116
117 App::Foo->new_with_options->run;
118
119 From the command line, someone can execute the script:
120
121 foo@example> foo --input /path/to/input --output /path/to/output
122
124 To be honest, using a singleton is just a way to have a magic global
125 variable in languages that don't actually have global variables.
126
127 In perl, you can just as easily use a global. However, if your
128 colleagues are Java-infected, they might prefer a singleton. Also, if
129 you have an existing class that isn't a singleton but should be, using
130 MooseX::Singleton is the easiest way to convert it.
131
132 package Config;
133
134 use MooseX::Singleton; # instead of Moose
135
136 has 'cache_dir' => ( ... );
137
138 It's that simple.
139
141 There are literally dozens of other extensions on CPAN. This is a list
142 of extensions that you might find useful, but we're not quite ready to
143 endorse just yet.
144
145 MooseX::Declare
146 MooseX::Declare is based on Devel::Declare, a giant bag of crack
147 originally implemented by mst with the goal of upsetting the perl core
148 developers so much by its very existence that they implemented proper
149 keyword handling in the core.
150
151 As of perl5 version 14, this goal has been achieved, and modules such
152 as Devel::CallParser, Function::Parameters, and Keyword::Simple provide
153 mechanisms to mangle perl syntax that don't require hallucinogenic
154 drugs to interpret the error messages they produce.
155
156 If you want to use declarative syntax in new code, please for the love
157 of kittens get yourself a recent perl and look at Moops instead.
158
159 MooseX::Types
160 This extension helps you build a type library for your application. It
161 also lets you predeclare type names and use them as barewords.
162
163 use MooseX::Types -declare => ['PositiveInt'];
164 use MooseX::Types::Moose 'Int';
165
166 subtype PositiveInt,
167 as Int,
168 where { $_ > 0 },
169 message { "Int is not larger than 0" };
170
171 One nice feature is that those bareword names are actually namespaced
172 in Moose's type registry, so multiple applications can use the same
173 bareword names, even if the type definitions differ.
174
175 MooseX::Types::Structured
176 This extension builds on top of MooseX::Types to let you declare
177 complex data structure types.
178
179 use MooseX::Types -declare => [ qw( Name Color ) ];
180 use MooseX::Types::Moose qw(Str Int);
181 use MooseX::Types::Structured qw(Dict Tuple Optional);
182
183 subtype Name
184 => as Dict[ first => Str, middle => Optional[Str], last => Str ];
185
186 subtype Color
187 => as Tuple[ Int, Int, Int, Optional[Int] ];
188
189 Of course, you could always use objects to represent these sorts of
190 things too.
191
192 MooseX::ClassAttribute
193 This extension provides class attributes for Moose classes. The
194 declared class attributes are introspectable just like regular Moose
195 attributes.
196
197 package User;
198
199 use Moose;
200 use MooseX::ClassAttribute;
201
202 has 'name' => ( ... );
203
204 class_has 'Cache' => ( ... );
205
206 Note however that this class attribute does not inherit like a
207 Class::Data::Inheritable or similar attribute - calling
208
209 $subclass->Cache($cache);
210
211 will set it for the superclass as well. Additionally, class data is
212 usually The Wrong Thing To Do in a strongly OO program since it makes
213 testing a lot harder - consider carefully whether you'd be better off
214 with an object that's passed around instead.
215
216 MooseX::Daemonize
217 This is a role that provides a number of methods useful for creating a
218 daemon, including methods for starting and stopping, managing a PID
219 file, and signal handling.
220
221 MooseX::Role::Parameterized
222 If you find yourself wanting a role that customizes itself for each
223 consumer, this is the tool for you. With this module, you can create a
224 role that accepts parameters and generates attributes, methods, etc. on
225 a customized basis for each consumer.
226
227 MooseX::POE
228 This is a small wrapper that ties together a Moose class with
229 "POE::Session", and gives you an "event" sugar function to declare
230 event handlers.
231
232 MooseX::FollowPBP
233 Automatically names all accessors Perl Best Practices-style, "get_size"
234 and "set_size".
235
236 MooseX::SemiAffordanceAccessor
237 Automatically names all accessors with an explicit set and implicit
238 get, "size" and "set_size".
239
240 MooseX::NonMoose
241 MooseX::NonMoose allows for easily subclassing non-Moose classes with
242 Moose, taking care of the annoying details connected with doing this,
243 such as setting up proper inheritance from Moose::Object and installing
244 (and inlining, at make_immutable time) a constructor that makes sure
245 things like BUILD methods are called.
246
248 · Stevan Little <stevan.little@iinteractive.com>
249
250 · Dave Rolsky <autarch@urth.org>
251
252 · Jesse Luehrs <doy@tozt.net>
253
254 · Shawn M Moore <code@sartak.org>
255
256 · יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
257
258 · Karen Etheridge <ether@cpan.org>
259
260 · Florian Ragwitz <rafl@debian.org>
261
262 · Hans Dieter Pearcey <hdp@weftsoar.net>
263
264 · Chris Prather <chris@prather.org>
265
266 · Matt S Trout <mst@shadowcat.co.uk>
267
269 This software is copyright (c) 2006 by Infinity Interactive, Inc.
270
271 This is free software; you can redistribute it and/or modify it under
272 the same terms as the Perl 5 programming language system itself.
273
274
275
276perl v5.32.0 2020-07-28 Moose::Manual::MooseX(3)