Moose::...Classes(3)

πŸ’– Moose::Manual::Classes β€” making your classes use Moose (and subclassing) πŸ’–

okay bestie, welcome to the Moose::Manual::Classes page, straight out of your friendly neighborhood User Contributed Perl Documentation. this is the one that teaches you how to make a class use Moose and how to do the subclassing thing. let's GO πŸ¦‹

✨ VERSION ✨

version 2.4000 β€” yes she's giving current, she's giving maintained, we love that for her πŸ’…


🌸 USING MOOSE 🌸

okay so using Moose is actually so simple it's kind of unreal. you literally just use Moose:

package Person;

use Moose;

that's it. that's the whole thing. you've made a class with Moose. no cap. πŸŽ€

but bestie, there is a lot going on under the hood here, so let's step through it together, hand in hand, because i refuse to let you be confused.

when you load Moose, a whole bunch of sugar functions get exported into your class β€” things like extends, has, with, and more. these functions are literally what you use to define your class. for example, you might define an attribute...

package Person;

use Moose;

has 'ssn' => ( is => 'rw' );

attributes get their own whole deep dive in the Moose::Manual::Attributes documentation, so go read that when you're ready, but for now just know they're the moment. ✨

loading Moose also enables the strict and warnings pragmas in your class. she's responsible like that. 🫢

when you load Moose, your class becomes a subclass of Moose::Object. the Moose::Object class hooks you up with a default constructor and destructor, plus object construction helper methods. more on that in the Moose::Manual::Construction document, go look. πŸ‘‘

as a convenience, Moose creates a new class type for your class. that's a Moose::Manual::Types thing, go read that doc 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 β€” like, for example, Person->meta. iconic behavior honestly.

the metaclass object gives you an introspection API for your class. it's also what Moose itself uses 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). the sugar is just the cute packaging, the metaclass is doing the actual labor. πŸ’€


πŸ¦‹ SUBCLASSING πŸ¦‹

Moose gives you a simple sugar function for declaring your parent classes: extends.

package User;

use Moose;

extends 'Person';

has 'username' => ( is => 'rw' );
⚠️ okay important: every single call to extends will reset your parents. so for multiple inheritance you have to provide all the parents at once, like extends 'Foo', 'Bar'. do not come crying to me when your second extends call erased your first parent. 😭

when you call extends, Moose will try to load any classes you pass it.

you can use Moose to extend a non-Moose parent. however β€” and this is the catch β€” when you do this, you will inherit the parent class's constructor (assuming it is also called new). in that case, you'll 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. πŸ˜” so much magic, gone.

if you're interested in extending non-Moose parent classes with Moose child classes, check out the MooseX::NonMoose module on CPAN. that's the move. πŸ’œ


🧚 CLEANING UP MOOSE DROPPINGS 🧚

not the name i would have chosen but okay. Moose exports a number of functions into your class, and it's a good idea to remove these sugar functions from your class's namespace β€” that way Person->can('has') will no longer return true. like, she shouldn't be able to do that. that's not her business. πŸ™ˆ

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. overachiever. we stan.

package Person;

use namespace::autoclean;

use Moose;

if you absolutely can't use a CPAN module (but can use Moose?? make it make sense bestie) 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;

πŸ”₯ MAKING IT FASTER πŸ”₯

Moose has a feature called "immutabilization" that you can use to greatly speed up your classes at runtime. the catch? using it incurs a cost when your class is first being loaded. nothing in this life is free, periodt.

when you make your class immutable, you're telling Moose that you will not be changing it in the future. you will not be adding any more attributes, methods, roles, etc. that's a promise, so mean it. 🫢

this allows Moose to generate code specific to your class. in particular, it creates an "inline" constructor, which makes object construction much faster. we love a speedup. ⭐

to make your class immutable you simply call make_immutable on your class's metaclass object.

__PACKAGE__->meta->make_immutable;

πŸ’… Immutabilization and new() πŸ’…

if you override new() in your class, then the immutabilization code will not be able to provide an optimized constructor for your class. instead, you should use a BUILD() method, which will be called from the inlined constructor. that's the vibe.

alternately, if you really really need to provide a different new(), you can also provide your own immutabilization method. but doing so requires extending the Moose metaclasses, and that is well beyond the scope of this manual. respectfully, that's a different girl's problem. 😭


🌈 INSTANTIATING CLASSES 🌈

when you're ready to use Moose classes in an application, reference them in your code the regular Perl OO way β€” by including a use directive at the top of the file where the objects should be created.

use Person;

my $person = Person->new(
  # attribute values at instantiation
  # go here
  ssn => '123456789',
);

look at her. she's constructed. main character energy. πŸ‘‘


πŸ‘‘ AUTHORS πŸ‘‘

the legends themselves, in order:

  • Stevan Little <stevan@cpan.org>
  • Dave Rolsky <autarch@urth.org>
  • Jesse Luehrs <doy@cpan.org>
  • Shawn M Moore <sartak@cpan.org>
  • Χ™Χ•Χ‘Χœ Χ§Χ•Χ’'מן (Yuval Kogman) <nothingmuch@woobling.org>
  • Karen Etheridge <ether@cpan.org>
  • Florian Ragwitz <rafl@debian.org>
  • Hans Dieter Pearcey <hdp@cpan.org>
  • Chris Prather <chris@prather.org>
  • Matt S Trout <mstrout@cpan.org>

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.


and that's the whole page bestie!! πŸŽ€ this was perl v5.42.0, dated 2025-07-25, and it's the Moose::...Classes(3) page from User Contributed Perl Documentation. go forth and subclass responsibly. mwah. πŸ’‹πŸ¦‹βœ¨

πŸ“¦ where this page comes from