Moose::Cookbook(3)
๐ Moose::Cookbook ๐
okay bestie, welcome to the Moose::Cookbook page โ it's part of the User Contributed Perl Documentation, and it's literally your recipe box for cooking a Moose ๐ซโจ
version 2.4000 โ yes, that's the VERSION, and yes, it slaps.
โจ DESCRIPTION โจ
So the Moose cookbook is a whole series of recipes showing off the various features Moose has to offer. Most recipes hand you some code that demonstrates a feature, and then break down the details of that code for you. Very demure, very mindful.
real talk though: you should probably read the Moose::Manual first. The manual explains Moose concepts without being too code-heavy โ think of it as the vibe check before the main event. ๐
๐ณ RECIPES ๐ณ
๐ฑ Basic Moose
These recipes give you a genuinely good overview of what Moose can do โ starting with simple attribute declaration and then levelling up to the powerful stuff like laziness, types, type coercion, method modifiers, and more. Character development arc, honestly. ๐
Moose::Cookbook::Basics::Point_AttributesAndSubclassingโ a simple Moose-based class. Demonstrates basic Moose attributes and subclassing. Starter pack energy.Moose::Cookbook::Basics::BankAccount_MethodModifiersAndSubclassingโ a slightly more complex Moose class. Demonstrates using a method modifier in a subclass. ๐ธMoose::Cookbook::Basics::BinaryTree_AttributeFeaturesโ demonstrates several attribute features, including types, weak references, predicates ("does this object have a foo?"), defaults, laziness, and triggers. That's a lot of features, she's doing numbers. ๐ณMoose::Cookbook::Basics::Company_Subtypesโ introduces the creation and use of custom types, aBUILDmethod, and the use ofoverridein a subclass. This recipe also shows how to model a set of classes that could be used to model companies, people, employees, etc. Corporate slay ๐ขMoose::Cookbook::Basics::HTTP_SubtypesAndCoercionโ this recipe covers more subtype creation, including the use of type coercions.Moose::Cookbook::Basics::Immutableโ making a class immutable greatly increases the speed of accessors and object construction. speed. ๐๏ธโจMoose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild- Builder methods and lazy_build โ the builder feature provides an inheritable and role-composable way to provide a default attribute value. iykyk.Moose::Cookbook::Basics::Genome_OverloadingSubtypesAndCoercionโ demonstrates using operator overloading, coercion, and subtypes to model how eye color is determined during reproduction. ๐งฌ๐๏ธ genetics but make it OOP.Moose::Cookbook::Basics::Person_BUILDARGSAndBUILDโ this recipe demonstrates the use ofBUILDARGSandBUILDto hook into object construction. constructor girlies rise ๐ซถMoose::Cookbook::Basics::DateTime_ExtendingNonMooseParentโ in this recipe, we make a Moose-based subclass ofDateTime, a module which does not use Moose itself. crossing the friend groups, iconic behavior โฐMoose::Cookbook::Basics::Document_AugmentAndInnerโ demonstrates the use ofaugmentmethod modifiers, a way of turning the usual method overriding style "inside-out". ๐
๐ญ Moose Roles
These recipes show you how to use Moose roles. Roles are the code-reuse besties.
Moose::Cookbook::Roles::Comparable_CodeReuseโ demonstrates roles, which are also sometimes known as traits or mix-ins. Roles provide a method of code re-use which is orthogonal to subclassing. different axis, same excellence ๐Moose::Cookbook::Roles::Restartable_AdvancedCompositionโ sometimes you just want to include part of a role in your class. Sometimes you want the whole role but one of its methods conflicts with one in your class. With method exclusion and aliasing, you can work around these problems. conflict resolution queen ๐Moose::Cookbook::Roles::ApplicationToInstanceโ in this recipe, we apply a role to an existing object instance. not the class, the instance. she's specific.
๐ฎ Meta Moose
These recipes show you how to write your own meta classes, which lets you extend the object system provided by Moose. very meta of you fr fr
Moose::Cookbook::Meta::WhyMetaโ if you're wondering what all this "meta" stuff is, and why you should care about it, read this "recipe". ๐ซMoose::Cookbook::Meta::Labeled_AttributeTraitโ extending Moose's attribute metaclass is a great way to add functionality. However, attributes can only have one metaclass. Applying roles to the attribute metaclass lets you provide composable attribute functionality. one metaclass only, so we compose instead โ problem solved โจMoose::Cookbook::Meta::Table_MetaclassTraitโ this recipe takes the class metaclass we saw in the previous recipe and reimplements it as a metaclass trait.Moose::Cookbook::Meta::PrivateOrPublic_MethodMetaclassโ this recipe shows a custom method metaclass that implements making a method private. ๐คซMoose::Cookbook::Meta::GlobRef_InstanceMetaclassโ this recipe shows an example of how you create your own meta-instance class. The meta-instance determines the internal structure of object instances and provide access to attribute slots.- In this particular instance, we use a blessed glob reference as the instance instead of a blessed hash reference. plot twist!! ๐
- Hooking into immutabilization (TODO) โ Moose has a feature known as "immutabilization". By calling
__PACKAGE__->meta()->make_immutable()after defining your class (attributes, roles, etc), you tell Moose to optimize things like object creation, attribute access, and so on.- If you are creating your own metaclasses, you may need to hook into the immutabilization system. This cuts across a number of spots, including the metaclass class, meta method classes, and possibly the meta-instance class as well.
- This recipe shows you how to write extensions which immutabilize properly.
๐จ note the (TODO) up there โ that recipe is a work in progress, bestie. don't come for me.
๐งฐ Extending Moose
These recipes cover some more ways to extend Moose, and will be useful if you plan to write your own MooseX module. yes, you. you could do this. ๐ซถ
Moose::Cookbook::Extending::ExtensionOverviewโ there are quite a few ways to extend Moose. This recipe provides an overview of each method, and provides recommendations for when each is appropriate. the map before the quest ๐บ๏ธMoose::Cookbook::Extending::Debugging_BaseClassRoleโ many base object class extensions can be implemented as roles. This example shows how to provide a base object class debugging role that is applied to any class that uses a notionalMooseX::Debuggingmodule. ๐Moose::Cookbook::Extending::Mooseish_MooseSugarโ this recipe shows how to provide a replacement forMoose.pm. You may want to do this as part of the API for aMooseXmodule, especially if you want to default to a new metaclass class or base object class. sugar, but make it Moose ๐ฌ
๐ SNACKS ๐
little bite-sized ones, perfect between meals:
Moose::Cookbook::Snack::KeywordsMoose::Cookbook::Snack::Types
๐ต Legacy Recipes
These cover topics that are no longer considered best practice. We've kept them in case in you encounter these usages in the wild. archive vibes, museum vibes, "we don't do this anymore but someone out there still does" vibes ๐๏ธ
Moose::Cookbook::Legacy::Labeled_AttributeMetaclassMoose::Cookbook::Legacy::Table_ClassMetaclassMoose::Cookbook::Legacy::Debugging_BaseClassReplacement
๐ SEE ALSO ๐
๐ AUTHORS ๐
the legends, the icons, the ones who cooked:
- 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>
๐ COPYRIGHT AND LICENSE ๐
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. free as in freedom, bestie ๐๏ธ
okay that's the whole page, no cap ๐ this was the Moose::Cookbook(3) manpage, cooked and served by perl v5.42.0, dated 2025-07-25. go forth and write some Moose, you absolute main character. ๐๐ฆ๐
๐ฆ where this page comes from
- fedora 43: perl-Moose 2.4000-4.fc43 ยท GPL-1.0-or-later OR Artistic-1.0-Perl
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms