NAME
Moose - Moose, it's the new Camel
SYNOPSIS
package Point;
use Moose;
has 'x' => (isa => 'Int', is => 'rw');
has 'y' => (isa => 'Int', is => 'rw');
sub clear {
my $self = shift;
$self->x(0);
$self->y(0);
}
package Point3D;
use Moose;
extends 'Point';
has 'z' => (isa => 'Int');
after 'clear' => sub {
my $self = shift;
$self->{z} = 0;
};
CAVEAT
This is a very early release of this module, it still needs some fine tuning and lots more documentation. I am adopting the release early and release often approach with this module, so keep an eye on your favorite CPAN mirror!
DESCRIPTION
Moose is an extension of the Perl 5 object system.
Another object system!?!?
Yes, I know there has been an explosion recently of new ways to build object's in Perl 5, most of them based on inside-out objects, and other such things. Moose is different because it is not a new object system for Perl 5, but instead an extension of the existing object system.
Moose is built on top of Class::MOP, which is a metaclass system for Perl 5. This means that Moose not only makes building normal Perl 5 objects better, but it also provides the power of metaclass programming.
What does Moose stand for??
Moose doesn't stand for one thing in particular, however, if you want, here are a few of my favorites, feel free to contribute more :)
- Make Other Object Systems Envious
- Makes Object Orientation So Easy
- Makes Object Orientation Spiffy- Er (sorry ingy)
- Most Other Object Systems Emasculate
- My Overcraft Overfilled (with) Some Eels
- Moose Often Ovulate Sorta Early
- Many Overloaded Object Systems Exists
- Moose Offers Often Super Extensions
BUILDING CLASSES WITH MOOSE
Moose makes every attempt to provide as much convience during class construction/definition, but still stay out of your way if you want it to. Here are some of the features Moose provides:
Unless specified with extends
, any class which uses Moose will inherit from Moose::Object.
Moose will also manage all attributes (including inherited ones) that are defined with has
. And assuming that you call new
which is inherited from Moose::Object, then this includes properly initializing all instance slots, setting defaults where approprtiate and performing any type constraint checking or coercion.
EXPORTED FUNCTIONS
Moose will export a number of functions into the class's namespace, which can then be used to set up the class. These functions all work directly on the current class.
- meta
-
This is a method which provides access to the current class's metaclass.
- extends (@superclasses)
-
This function will set the superclass(es) for the current class.
This approach is recommended instead of
use base
, becauseuse base
actuallypush
es onto the class's@ISA
, whereasextends
will replace it. This is important to ensure that classes which do not have superclasses properly inherit from Moose::Object. - has ($name, %options)
-
This will install an attribute of a given
$name
into the current class. The list of%options
are the same as those provided by both Class::MOP::Attribute and Moose::Meta::Attribute, in addition to a few convience ones provided by Moose which are listed below:- is => 'rw'|'ro'
-
The is option accepts either rw (for read/write) or ro (for read only). These will create either a read/write accessor or a read-only accessor respectively, using the same name as the
$name
of the attribute.If you need more control over how your accessors are named, you can use the reader, writer and accessor options inherited from Moose::Meta::Attribute.
- isa => $type_name
-
The isa option uses Moose's type constraint facilities to set up runtime type checking for this attribute. Moose will perform the checks during class construction, and within any accessors. The
$type_name
argument must be a string. The string can be either a class name, or a type defined using Moose's type defintion features.
- before $name|@names => sub { ... }
- after $name|@names => sub { ... }
- around $name|@names => sub { ... }
-
This three items are syntactic sugar for the before, after and around method modifier features that Class::MOP provides. More information on these can be found in the Class::MOP documentation for now.
- confess
-
This is the
Carp::confess
function, and exported here beause I use it all the time. This feature may change in the future, so you have been warned. - blessed
-
This is the
Scalar::Uti::blessed
function, it is exported here beause I use it all the time. It is highly recommended that this is used instead ofref
anywhere you need to test for an object's class name.
ACKNOWLEDGEMENTS
- I blame Sam Vilain for introducing me to the insanity that is meta-models.
- I blame Audrey Tang for then encouraging my meta-model habit in #perl6.
- Without Yuval "nothingmuch" Kogman this module would not be possible, and it certainly wouldn't have this name ;P
- The basis of the TypeContraints module was Rob Kinyon's idea originally, I just ran with it.
- Thanks to mst & chansen and the whole #moose poose for all the ideas/feature-requests/encouragement
SEE ALSO
- Class::MOP documentation
- The #moose channel on irc.perl.org
- http://forum2.org/moose/
BUGS
All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.
AUTHOR
Stevan Little <stevan@iinteractive.com>
COPYRIGHT AND LICENSE
Copyright 2006 by Infinity Interactive, Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.