Moose::Manual - What is Moose, and how do I use it? |
Moose::Manual - What is Moose, and how do I use it?
version 2.1605
Moose is a complete object system for Perl 5. Consider any modern object-oriented language (which Perl 5 definitely isn't). It provides keywords for attribute declaration, object construction, inheritance, and maybe more. These keywords are part of the language, and you don't care how they are implemented.
Moose aims to do the same thing for Perl 5 OO. We can't actually create new keywords, but we do offer ``sugar'' that looks a lot like them. More importantly, with Moose, you define your class declaratively, without needing to know about blessed hashrefs, accessor methods, and so on.
With Moose, you can concentrate on the logical structure of your classes, focusing on ``what'' rather than ``how''. A class definition with Moose reads like a list of very concise English sentences.
Moose is built on top of Class::MOP
, a meta-object protocol (aka
MOP). Using the MOP, Moose provides complete introspection for all
Moose-using classes. This means you can ask classes about their
attributes, parents, children, methods, etc., all using a well-defined
API. The MOP abstracts away the symbol table, looking at @ISA
vars,
and all the other crufty Perl tricks we know and love(?).
Moose is based in large part on the Perl 6 object system, as well as drawing on the best ideas from CLOS, Smalltalk, and many other languages.
Moose makes Perl 5 OO both simpler and more powerful. It encapsulates Perl 5 power tools in high-level declarative APIs which are easy to use. Best of all, you don't need to be a wizard to use it.
But if you want to dig about in the guts, Moose lets you do that too, by using and extending its powerful introspection API.
package Person;
use Moose;
has 'first_name' => ( is => 'rw', isa => 'Str', );
has 'last_name' => ( is => 'rw', isa => 'Str', );
no Moose; __PACKAGE__->meta->make_immutable;
This is a complete and usable class definition!
package User;
use DateTime; use Moose;
extends 'Person';
has 'password' => ( is => 'rw', isa => 'Str', );
has 'last_login' => ( is => 'rw', isa => 'DateTime', handles => { 'date_of_last_login' => 'date' }, );
sub login { my $self = shift; my $pw = shift;
return 0 if $pw ne $self->password;
$self->last_login( DateTime->now() );
return 1; }
no Moose; __PACKAGE__->meta->make_immutable;
When ready to instantiate your class in an application, use it in the ``traditional'' Perl manner:
use User;
my $user = User->new( first_name => 'Example', last_name => 'User', password => 'letmein', );
$user->login('letmein');
say $user->date_of_last_login;
We'll leave the line-by-line explanation of this code to other documentation, but you can see how Moose reduces common OO idioms to simple declarative constructs.
This manual consists of a number of documents.
BUILD
and BUILDARGS
methods. Also covers object destruction
with DEMOLISH
.
If you're still asking yourself ``Why do I need this?'', then this section is for you.
Moose is built on top of the Class::MOP manpage, 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.
Moose has been used successfully in production environments by many people and companies. There are Moose applications which have been in production with little or no issue now for years. We consider it highly stable and we are committed to keeping it stable.
Of course, in the end, you need to make this call yourself. If you have any questions or concerns, please feel free to email Stevan or the moose@perl.org list, or just stop by irc.perl.org#moose and ask away.
Nuff Said.
This software is copyright (c) 2006 by Infinity Interactive, Inc.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Moose::Manual - What is Moose, and how do I use it? |