.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Moose::Manual::Classes 3pm" .TH Moose::Manual::Classes 3pm 2024-01-21 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Moose::Manual::Classes \- Making your classes use Moose (and subclassing) .SH VERSION .IX Header "VERSION" version 2.2207 .SH "USING MOOSE" .IX Header "USING MOOSE" Using Moose is very simple, you just \f(CW\*(C`use Moose\*(C'\fR: .PP .Vb 1 \& package Person; \& \& use Moose; .Ve .PP That's it, you've made a class with Moose! .PP There's actually a lot going on here under the hood, so let's step through it. .PP When you load Moose, a bunch of sugar functions are exported into your class, such as \f(CW\*(C`extends\*(C'\fR, \f(CW\*(C`has\*(C'\fR, \f(CW\*(C`with\*(C'\fR, and more. These functions are what you use to define your class. For example, you might define an attribute ... .PP .Vb 1 \& package Person; \& \& use Moose; \& \& has \*(Aqssn\*(Aq => ( is => \*(Aqrw\*(Aq ); .Ve .PP Attributes are described in the Moose::Manual::Attributes documentation. .PP Loading Moose also enables the \f(CW\*(C`strict\*(C'\fR and \f(CW\*(C`warnings\*(C'\fR pragmas in your class. .PP 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. .PP As a convenience, Moose creates a new class type for your class. See the Moose::Manual::Types document to learn more about types. .PP It also creates a Moose::Meta::Class object for your class. This metaclass object is now available by calling a \f(CW\*(C`meta\*(C'\fR method on your class, for example \f(CW\*(C`Person\->meta\*(C'\fR. .PP 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). .SH SUBCLASSING .IX Header "SUBCLASSING" Moose provides a simple sugar function for declaring your parent classes, \f(CW\*(C`extends\*(C'\fR: .PP .Vb 1 \& package User; \& \& use Moose; \& \& extends \*(AqPerson\*(Aq; \& \& has \*(Aqusername\*(Aq => ( is => \*(Aqrw\*(Aq ); .Ve .PP Note that each call to \f(CW\*(C`extends\*(C'\fR will \fIreset\fR your parents. For multiple inheritance you must provide all the parents at once, \&\f(CW\*(C`extends \*(AqFoo\*(Aq, \*(AqBar\*(Aq\*(C'\fR. .PP When you call \f(CW\*(C`extends\*(C'\fR Moose will try to load any classes you pass. .PP 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 \f(CW\*(C`new\*(C'\fR). 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. .PP See the MooseX::NonMoose module on CPAN if you're interested in extending non-Moose parent classes with Moose child classes. .SH "CLEANING UP MOOSE DROPPINGS" .IX Header "CLEANING UP MOOSE DROPPINGS" Moose exports a number of functions into your class. It's a good idea to remove these sugar functions from your class's namespace, so that \f(CW\*(C`Person\->can(\*(Aqhas\*(Aq)\*(C'\fR will no longer return true. .PP 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. .PP .Vb 1 \& package Person; \& \& use namespace::autoclean; \& \& use Moose; .Ve .PP If you absolutely can't use a CPAN module (but can use Moose?), you can write \&\f(CW\*(C`no Moose\*(C'\fR at the end of your class. This will remove any Moose exports in your class. .PP .Vb 1 \& package Person; \& \& use Moose; \& \& has \*(Aqssn\*(Aq => ( is => \*(Aqrw\*(Aq ); \& \& no Moose; .Ve .SH "MAKING IT FASTER" .IX Header "MAKING IT FASTER" Moose has a feature called "immutabilization" that you can use to greatly speed up your classes at runtime. However, using it incurs a cost when your class is first being loaded. When you make your class immutable you tell Moose that you will not be changing it in the future. You will not be adding any more attributes, methods, roles, etc. .PP This allows Moose to generate code specific to your class. In particular, it creates an "inline" constructor, making object construction much faster. .PP To make your class immutable you simply call \f(CW\*(C`make_immutable\*(C'\fR on your class's metaclass object. .PP .Vb 1 \& _\|_PACKAGE_\|_\->meta\->make_immutable; .Ve .ie n .SS "Immutabilization and new()" .el .SS "Immutabilization and \f(CWnew()\fP" .IX Subsection "Immutabilization and new()" If you override \f(CWnew()\fR in your class, then the immutabilization code will not be able to provide an optimized constructor for your class. Instead, you should use a \f(CWBUILD()\fR method, which will be called from the inlined constructor. .PP Alternately, if you really need to provide a different \f(CWnew()\fR, you can also provide your own immutabilization method. Doing so requires extending the Moose metaclasses, and is well beyond the scope of this manual. .SH "INSTANTIATING CLASSES" .IX Header "INSTANTIATING CLASSES" When you're ready to use Moose classes in an application, reference them in your code in the regular Perl OO way by including a \f(CW\*(C`use\*(C'\fR directive at the top of the file where the objects should be created. .PP .Vb 1 \& use Person; \& \& my $person = Person\->new( \& # attribute values at instantiation \& # go here \& ssn => \*(Aq123456789\*(Aq, \& ); .Ve .SH AUTHORS .IX Header "AUTHORS" .IP \(bu 4 Stevan Little .IP \(bu 4 Dave Rolsky .IP \(bu 4 Jesse Luehrs .IP \(bu 4 Shawn M Moore .IP \(bu 4 יובל קוג'מן (Yuval Kogman) .IP \(bu 4 Karen Etheridge .IP \(bu 4 Florian Ragwitz .IP \(bu 4 Hans Dieter Pearcey .IP \(bu 4 Chris Prather .IP \(bu 4 Matt S Trout .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2006 by Infinity Interactive, Inc. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.