Moose::Cookbook - How to cook a Moose
| Use Case | Command | Description |
|---|---|---|
| Define an attribute | has 'name' => (is => 'rw', isa => 'Str') | Declare a read-write string attribute |
| Create a custom subtype | subtype 'PositiveInt' => as 'Int' => where { $_ > 0 } | Define a constrained integer type |
| Apply a role to a class | with 'Comparable'; | Compose a role into the current class |
| Make a class immutable | __PACKAGE__->meta->make_immutable(); | Optimize accessors and construction |
| Use a method modifier (before) | before 'method' => sub { ... }; | Run code before a method call |
| Use a method modifier (after) | after 'method' => sub { ... }; | Run code after a method call |
| Use a method modifier (around) | around 'method' => sub { my $orig = shift; $orig->(@_); }; | Wrap a method call |
| Define a type coercion | coerce 'ArrayRef' => from 'Str' => via { [split /,/] }; | Automatically convert a string to an array |
| Use lazy_build | has 'data' => (is => 'ro', lazy_build => 1); | Deferred attribute initialization with builder |
| Extend a non-Moose class | extends 'DateTime'; | Subclass a class that does not use Moose |
version 2.2200
The Moose cookbook is a series of recipes showing various Moose features. Most recipes present some code demonstrating some feature, and then explain the details of the code.
You should probably read the Moose::Manual first. The manual explains Moose concepts without being too code-heavy.
These recipes will give you a good overview of Moose's capabilities, starting with simple attribute declaration, and moving on to more powerful features like laziness, types, type coercion, method modifiers, and more.
These recipes will show you how to use Moose roles.
These recipes show you how to write your own meta classes, which lets you extend the object system provided by Moose.
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.
These recipes cover some more ways to extend Moose, and will be useful if you plan to write your own "MooseX" module.
These cover topics that are no longer considered best practice. We've kept them in case in you encounter these usages in the wild.
<http://www.gsph.com/index.php?Lang=En&ID=291>
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.
Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-24 17:51 @216.73.217.23
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)