9 use Scalar::Util 'blessed', 'reftype';
11 use Sub::Name 'subname';
13 use UNIVERSAL::require;
17 use Moose::Meta::Class;
18 use Moose::Meta::Attribute;
19 use Moose::Meta::TypeConstraint;
22 use Moose::Util::TypeConstraints;
28 # we should never export to main
29 return if $pkg eq 'main';
31 Moose::Util::TypeConstraints->import($pkg);
33 # make a subtype for each Moose class
36 => where { $_->isa($pkg) };
39 if ($pkg->can('meta')) {
41 (blessed($meta) && $meta->isa('Class::MOP::Class'))
42 || confess "Whoops, not møøsey enough";
45 $meta = Moose::Meta::Class->initialize($pkg => (
46 ':attribute_metaclass' => 'Moose::Meta::Attribute'
48 $meta->add_method('meta' => sub {
49 # re-initialize so it inherits properly
50 Moose::Meta::Class->initialize($pkg => (
51 ':attribute_metaclass' => 'Moose::Meta::Attribute'
57 # &alias_method will install the method, but it
58 # will not name it with
61 $meta->alias_method('extends' => subname 'Moose::extends' => sub {
63 $meta->superclasses(@_)
67 $meta->alias_method('has' => subname 'Moose::has' => sub {
68 my ($name, %options) = @_;
69 if (exists $options{is}) {
70 if ($options{is} eq 'ro') {
71 $options{reader} = $name;
73 elsif ($options{is} eq 'rw') {
74 $options{accessor} = $name;
77 if (exists $options{isa}) {
78 # allow for anon-subtypes here ...
79 if (reftype($options{isa}) && reftype($options{isa}) eq 'CODE') {
80 $options{type_constraint} = Moose::Meta::TypeConstraint->new(
82 constraint_code => $options{isa}
86 # otherwise assume it is a constraint
87 my $constraint = Moose::Util::TypeConstraints::find_type_constraint($options{isa});
88 # if the constraing it not found ....
89 unless (defined $constraint) {
90 # assume it is a foreign class, and make
91 # an anon constraint for it
92 $constraint = Moose::Meta::TypeConstraint->new(
94 constraint_code => subtype Object => where { $_->isa($constraint) }
97 $options{type_constraint} = $constraint;
100 $meta->add_attribute($name, %options)
103 # handle method modifers
104 $meta->alias_method('before' => subname 'Moose::before' => sub {
106 $meta->add_before_method_modifier($_, $code) for @_;
108 $meta->alias_method('after' => subname 'Moose::after' => sub {
110 $meta->add_after_method_modifier($_, $code) for @_;
112 $meta->alias_method('around' => subname 'Moose::around' => sub {
114 $meta->add_around_method_modifier($_, $code) for @_;
117 # make sure they inherit from Moose::Object
118 $meta->superclasses('Moose::Object')
119 unless $meta->superclasses();
121 # we recommend using these things
122 # so export them for them
123 $meta->alias_method('confess' => \&Carp::confess);
124 $meta->alias_method('blessed' => \&Scalar::Util::blessed);
135 Moose - Moose, it's the new Camel
142 has 'x' => (isa => 'Int', is => 'rw');
143 has 'y' => (isa => 'Int', is => 'rw');
156 has 'z' => (isa => 'Int');
158 after 'clear' => sub {
165 This is a B<very> early release of this module, it still needs
166 some fine tuning and B<lots> more documentation. I am adopting
167 the I<release early and release often> approach with this module,
168 so keep an eye on your favorite CPAN mirror!
172 Moose is an extension of the Perl 5 object system.
174 =head2 Another object system!?!?
176 Yes, I know there has been an explosion recently of new ways to
177 build object's in Perl 5, most of them based on inside-out objects,
178 and other such things. Moose is different because it is not a new
179 object system for Perl 5, but instead an extension of the existing
182 Moose is built on top of L<Class::MOP>, which is a metaclass system
183 for Perl 5. This means that Moose not only makes building normal
184 Perl 5 objects better, but it also provides the power of metaclass
187 =head2 What does Moose stand for??
189 Moose doesn't stand for one thing in particular, however, if you
190 want, here are a few of my favorites, feel free to contribute
195 =item Make Other Object Systems Envious
197 =item Makes Object Orientation So Easy
199 =item Makes Object Orientation Spiffy- Er (sorry ingy)
201 =item Most Other Object Systems Emasculate
203 =item My Overcraft Overfilled (with) Some Eels
205 =item Moose Often Ovulate Sorta Early
207 =item Many Overloaded Object Systems Exists
209 =item Moose Offers Often Super Extensions
213 =head1 ACKNOWLEDGEMENTS
217 =item I blame Sam Vilain for giving me my first hit of meta-model crack.
219 =item I blame Audrey Tang for encouraging that meta-crack habit in #perl6.
221 =item Without the love and encouragement of Yuval "nothingmuch" Kogman,
222 this module would not be possible (and it wouldn't have a name).
224 =item The basis of the TypeContraints module was Rob Kinyon's idea
225 originally, I just ran with it.
227 =item Much love to mst & chansen and the whole #moose poose for all the
228 ideas/feature-requests/encouragement
236 =item L<http://forum2.org/moose/>
242 All complex software has bugs lurking in it, and this module is no
243 exception. If you find a bug please either email me, or add the bug
248 Stevan Little E<lt>stevan@iinteractive.comE<gt>
250 =head1 COPYRIGHT AND LICENSE
252 Copyright 2006 by Infinity Interactive, Inc.
254 L<http://www.iinteractive.com>
256 This library is free software; you can redistribute it and/or modify
257 it under the same terms as Perl itself.