1Moose::Manual::MooseX(3U)ser Contributed Perl DocumentatiMoonose::Manual::MooseX(3)
2
3
4

NAME

6       Moose::Manual::MooseX - Recommended Moose extensions
7

MooseX?

9       It's easy to extend and change Moose, and this is part of what makes
10       Moose so powerful. You can use the MOP API to do things your own way,
11       add new features, and generally customize your Moose.
12
13       Writing your own extensions does require a good understanding of the
14       meta-model. You can start learning about this with the
15       Moose::Manual::MOP docs. There are also several extension recipes in
16       the Moose::Cookbook.
17
18       Explaining how to write extensions is beyond the scope of this manual.
19       Fortunately, lots of people have already written extensions and put
20       them on CPAN for you.
21
22       This document covers a few of the ones we like best.
23

MooseX::AttributeHelpers

25       The functionality of this MooseX module has been moved into Moose core.
26       See Moose::Meta::Attribute::Native.
27

Moose::Autobox

29       MooseX::AttributeHelpers, but turned inside out, Moose::Autobox
30       provides methods on both arrays/hashes/etc. but also references to
31       them, using Moose roles, allowing you do to things like:
32
33         use Moose::Autobox;
34
35         $somebody_elses_object->orders->push($order);
36
37       Lexically scoped and not to everybody's taste, but very handy for
38       sugaring up other people's APIs and your own code.
39

MooseX::StrictConstructor

41       By default, Moose lets you pass any old junk into a class's
42       constructor. If you load MooseX::StrictConstructor, your class will
43       throw an error if it sees something it doesn't recognize;
44
45         package User;
46
47         use Moose;
48         use MooseX::StrictConstructor;
49
50         has 'name';
51         has 'email';
52
53         User->new( name => 'Bob', emali => 'bob@example.com' );
54
55       With MooseX::StrictConstructor, that typo ("emali") will cause a
56       runtime error. With plain old Moose, the "emali" attribute would be
57       silently ignored.
58

MooseX::Params::Validate

60       We have high hopes for the future of MooseX::Method::Signatures and
61       MooseX::Declare. However, these modules, while used regularly in
62       production by some of the more insane members of the community, are
63       still marked alpha just in case backwards incompatible changes need to
64       be made.
65
66       If you don't want to risk that, for now we recommend the decidedly more
67       clunky (but also faster and simpler) MooseX::Params::Validate. This
68       module lets you apply Moose types and coercions to any method
69       arguments.
70
71         package User;
72
73         use Moose;
74         use MooseX::Params::Validate;
75
76         sub login {
77             my $self = shift;
78             my ($password)
79                 = validated_list( \@_, password => { isa => 'Str', required => 1 } );
80
81             ...
82         }
83

MooseX::Getopt

85       This is a role which adds a "new_with_options" method to your class.
86       This is a constructor that takes the command line options and uses them
87       to populate attributes.
88
89       This makes writing a command-line application as a module trivially
90       simple:
91
92         package App::Foo;
93
94         use Moose;
95         with 'MooseX::Getopt';
96
97         has 'input' => (
98             is       => 'ro',
99             isa      => 'Str',
100             required => 1
101         );
102
103         has 'output' => (
104             is       => 'ro',
105             isa      => 'Str',
106             required => 1
107         );
108
109         sub run { ... }
110
111       Then in the script that gets run we have:
112
113         use App::Foo;
114
115         App::Foo->new_with_options->run;
116
117       From the command line, someone can execute the script:
118
119         foo@example> foo --input /path/to/input --output /path/to/output
120

MooseX::Singleton

122       To be honest, using a singleton is just a way to have a magic global
123       variable in languages that don't actually have global variables.
124
125       In perl, you can just as easily use a global. However, if your
126       colleagues are Java-infected, they might prefer a singleton. Also, if
127       you have an existing class that isn't a singleton but should be, using
128       MooseX::Singleton is the easiest way to convert it.
129
130         package Config;
131
132         use MooseX::Singleton; # instead of Moose
133
134         has 'cache_dir' => ( ... );
135
136       It's that simple.
137

EXTENSIONS TO CONSIDER

139       There are literally dozens of other extensions on CPAN. This is a list
140       of extensions that you might find useful, but we're not quite ready to
141       endorse just yet.
142
143   MooseX::Declare
144       Extends Perl with Moose-based keywords using "Devel::Declare". Very
145       cool, but still new and experimental.
146
147         class User {
148
149             has 'name'  => ( ... );
150             has 'email' => ( ... );
151
152             method login (Str $password) { ... }
153         }
154
155   MooseX::Types
156       This extension helps you build a type library for your application. It
157       also lets you predeclare type names and use them as barewords.
158
159         use MooseX::Types -declare => ['PositiveInt'];
160         use MooseX::Types::Moose 'Int';
161
162         subtype PositiveInt,
163             as Int,
164             where { $_ > 0 },
165             message { "Int is not larger than 0" };
166
167       One nice feature is that those bareword names are actually namespaced
168       in Moose's type registry, so multiple applications can use the same
169       bareword names, even if the type definitions differ.
170
171   MooseX::Types::Structured
172       This extension builds on top of MooseX::Types to let you declare
173       complex data structure types.
174
175         use MooseX::Types -declare => [ qw( Name Color ) ];
176         use MooseX::Types::Moose qw(Str Int);
177         use MooseX::Types::Structured qw(Dict Tuple Optional);
178
179         subtype Name
180             => as Dict[ first => Str, middle => Optional[Str], last => Str ];
181
182         subtype Color
183             => as Tuple[ Int, Int, Int, Optional[Int] ];
184
185       Of course, you could always use objects to represent these sorts of
186       things too.
187
188   MooseX::ClassAttribute
189       This extension provides class attributes for Moose classes. The
190       declared class attributes are introspectable just like regular Moose
191       attributes.
192
193         package User;
194
195         use Moose;
196         use MooseX::ClassAttribute;
197
198         has 'name' => ( ... );
199
200         class_has 'Cache' => ( ... );
201
202       Note however that this class attribute does not inherit like a
203       Class::Data::Inheritable or similar attribute - calling
204
205         $subclass->Cache($cache);
206
207       will set it for the superclass as well. Additionally, class data is
208       usually The Wrong Thing To Do in a strongly OO program since it makes
209       testing a lot harder - consider carefully whether you'd be better off
210       with an object that's passed around instead.
211
212   MooseX::Daemonize
213       This is a role that provides a number of methods useful for creating a
214       daemon, including methods for starting and stopping, managing a PID
215       file, and signal handling.
216
217   MooseX::Role::Parameterized
218       If you find yourself wanting a role that customizes itself for each
219       consumer, this is the tool for you. With this module, you can create a
220       role that accepts parameters and generates attributes, methods, etc. on
221       a customized basis for each consumer.
222
223   MooseX::POE
224       This is a small wrapper that ties together a Moose class with
225       "POE::Session", and gives you an "event" sugar function to declare
226       event handlers.
227
228   MooseX::FollowPBP
229       Automatically names all accessors Perl Best Practices-style, "get_size"
230       and "set_size".
231
232   MooseX::SemiAffordanceAccessor
233       Automatically names all accessors with an explicit set and implicit
234       get, "size" and "set_size".
235
236   MooseX::NonMoose
237       MooseX::NonMoose allows for easily subclassing non-Moose classes with
238       Moose, taking care of the annoying details connected with doing this,
239       such as setting up proper inheritance from Moose::Object and installing
240       (and inlining, at make_immutable time) a constructor that makes sure
241       things like BUILD methods are called.
242

AUTHOR

244       Dave Rolsky <autarch@urth.org>
245
247       Copyright 2009 by Infinity Interactive, Inc.
248
249       <http://www.iinteractive.com>
250
251       This library is free software; you can redistribute it and/or modify it
252       under the same terms as Perl itself.
253
254
255
256perl v5.12.2                      2010-08-20          Moose::Manual::MooseX(3)
Impressum