package Person; use Moose;
That's it, you've made a class with Moose!
There's actually a lot going on here under the hood, so let's step through it.
When you load Moose, a bunch of sugar functions are exported into your class, such as "extends", "has", "with", and more. These functions are what you use to define your class. For example, you might define an attribute ...
package Person; use Moose; has 'ssn' => ( is => 'rw' );
Attributes are described in the Moose::Manual::Attributes documentation.
Loading Moose also enables the "strict" and "warnings" pragmas in your class.
When you load Moose, your class will become a subclass of Moose::Object. The Moose::Object class provides a default constructor and destructor, as well as object construction helper methods. You can read more about this in the Moose::Manual::Construction document.
As a convenience, Moose creates a new class type for your class. See the Moose::Manual::Types document to learn more about types.
It also creates a Moose::Meta::Class object for your class. This metaclass object is now available by calling a "meta" method on your class, for example "Person->meta".
The metaclass object provides an introspection API for your class. It is also used by Moose itself under the hood to add attributes, define parent classes, and so on. In fact, all of Moose's sugar does the real work by calling methods on this metaclass object (and other meta API objects).
package User; use Moose; extends 'Person'; has 'username' => ( is => 'rw' );
Note that each call to "extends" will reset your parents. For multiple inheritance you must provide all the parents at once, "extends 'Foo', 'Bar'".
When you call "extends" Moose will try to load any classes you pass.
You can use Moose to extend a non-Moose parent. However, when you do this, you will inherit the parent class's constructor (assuming it is also called "new"). In that case, you will have to take care of initializing attributes manually, either in the parent's constructor, or in your subclass, and you will lose a lot of Moose magic.
There are several ways to do this. We recommend using namespace::autoclean, a CPAN module. Not only will it remove Moose exports, it will also remove any other exports.
package Person; use namespace::autoclean; use Moose;
If you absolutely can't use a CPAN module (but can use Moose?), you can write "no Moose" at the end of your class. This will remove any Moose exports in your class.
package Person; use Moose; has 'ssn' => ( is => 'rw' ); no Moose;
This allows Moose to generate code specific to your class. In particular, it creates an ``inline'' constructor, making object construction much faster.
To make your class immutable you simply call "make_immutable" on your class's metaclass object.
Alternately, if you really need to provide a different "new()", you can also provide your own immutabilization method. Doing so requires extending the Moose metaclasses, and is well beyond the scope of this manual.
use Person; my $person = Person->new( # attribute values at instantiation # go here ssn => '123456789', );
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.