2 use lib '/Users/stevan/Projects/CPAN/Class-MOP/Class-MOP/lib';
11 use Scalar::Util 'blessed', 'reftype';
13 use Sub::Name 'subname';
15 use UNIVERSAL::require;
19 use Moose::Meta::Class;
20 use Moose::Meta::Attribute;
23 use Moose::Util::TypeConstraints ':no_export';
29 # we should never export to main
30 return if $pkg eq 'main';
32 Moose::Util::TypeConstraints->import($pkg);
34 # make a subtype for each Moose class
35 Moose::Util::TypeConstraints::subtype($pkg
36 => Moose::Util::TypeConstraints::as Object
37 => Moose::Util::TypeConstraints::where { $_->isa($pkg) }
41 if ($pkg->can('meta')) {
43 (blessed($meta) && $meta->isa('Class::MOP::Class'))
44 || confess "Whoops, not møøsey enough";
47 $meta = Moose::Meta::Class->initialize($pkg => (
48 ':attribute_metaclass' => 'Moose::Meta::Attribute'
50 $meta->add_method('meta' => sub {
51 # re-initialize so it inherits properly
52 Moose::Meta::Class->initialize($pkg => (
53 ':attribute_metaclass' => 'Moose::Meta::Attribute'
59 # &alias_method will install the method, but it
60 # will not name it with
63 $meta->alias_method('extends' => subname 'Moose::extends' => sub {
65 $meta->superclasses(@_)
69 $meta->alias_method('has' => subname 'Moose::has' => sub {
70 my ($name, %options) = @_;
71 if (exists $options{is}) {
72 if ($options{is} eq 'ro') {
73 $options{reader} = $name;
75 elsif ($options{is} eq 'rw') {
76 $options{accessor} = $name;
79 if (exists $options{isa}) {
80 # allow for anon-subtypes here ...
81 if (reftype($options{isa}) && reftype($options{isa}) eq 'CODE') {
82 $options{type_constraint} = $options{isa};
85 # otherwise assume it is a constraint
86 my $constraint = Moose::Util::TypeConstraints::find_type_constraint($options{isa});
87 # if the constraing it not found ....
88 unless (defined $constraint) {
89 # assume it is a foreign class, and make
90 # an anon constraint for it
91 $constraint = Moose::Util::TypeConstraints::subtype(
92 Object => Moose::Util::TypeConstraints::where { $_->isa($constraint) }
95 $options{type_constraint} = $constraint;
98 if (exists $options{coerce} && $options{coerce} && $options{isa}) {
99 my $coercion = Moose::Util::TypeConstraints::find_type_coercion($options{isa});
101 || confess "Cannot find coercion for type " . $options{isa};
102 $options{coerce} = $coercion;
104 $meta->add_attribute($name, %options)
107 # handle method modifers
108 $meta->alias_method('before' => subname 'Moose::before' => sub {
110 $meta->add_before_method_modifier($_, $code) for @_;
112 $meta->alias_method('after' => subname 'Moose::after' => sub {
114 $meta->add_after_method_modifier($_, $code) for @_;
116 $meta->alias_method('around' => subname 'Moose::around' => sub {
118 $meta->add_around_method_modifier($_, $code) for @_;
121 # make sure they inherit from Moose::Object
122 $meta->superclasses('Moose::Object')
123 unless $meta->superclasses();
125 # we recommend using these things
126 # so export them for them
127 $meta->alias_method('confess' => \&Carp::confess);
128 $meta->alias_method('blessed' => \&Scalar::Util::blessed);
139 Moose - Moose, it's the new Camel
146 has 'x' => (isa => 'Int', is => 'rw');
147 has 'y' => (isa => 'Int', is => 'rw');
160 has 'z' => (isa => 'Int');
162 after 'clear' => sub {
169 This is a B<very> early release of this module, it still needs
170 some fine tuning and B<lots> more documentation. I am adopting
171 the I<release early and release often> approach with this module,
172 so keep an eye on your favorite CPAN mirror!
176 Moose is an extension of the Perl 5 object system.
178 =head2 Another object system!?!?
180 Yes, I know there has been an explosion recently of new ways to
181 build object's in Perl 5, most of them based on inside-out objects,
182 and other such things. Moose is different because it is not a new
183 object system for Perl 5, but instead an extension of the existing
186 Moose is built on top of L<Class::MOP>, which is a metaclass system
187 for Perl 5. This means that Moose not only makes building normal
188 Perl 5 objects better, but it also provides the power of metaclass
191 =head2 What does Moose stand for??
193 Moose doesn't stand for one thing in particular, however, if you
194 want, here are a few of my favorites, feel free to contribute
199 =item Make Other Object Systems Envious
201 =item Makes Object Orientation So Easy
203 =item Makes Object Orientation Spiffy- Er (sorry ingy)
205 =item Most Other Object Systems Emasculate
207 =item My Overcraft Overfilled (with) Some Eels
209 =item Moose Often Ovulate Sorta Early
211 =item Many Overloaded Object Systems Exists
213 =item Moose Offers Often Super Extensions
217 =head1 ACKNOWLEDGEMENTS
221 =item I blame Sam Vilain for giving me my first hit of meta-model crack.
223 =item I blame Audrey Tang for encouraging that meta-crack habit in #perl6.
225 =item Without the love and encouragement of Yuval "nothingmuch" Kogman,
226 this module would not be possible (and it wouldn't have a name).
228 =item The basis of the TypeContraints module was Rob Kinyon's idea
229 originally, I just ran with it.
231 =item Much love to mst & chansen and the whole #moose poose for all the
232 ideas/feature-requests/encouragement
240 =item L<http://forum2.org/moose/>
246 All complex software has bugs lurking in it, and this module is no
247 exception. If you find a bug please either email me, or add the bug
252 Stevan Little E<lt>stevan@iinteractive.comE<gt>
254 =head1 COPYRIGHT AND LICENSE
256 Copyright 2006 by Infinity Interactive, Inc.
258 L<http://www.iinteractive.com>
260 This library is free software; you can redistribute it and/or modify
261 it under the same terms as Perl itself.