.\" -*- 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::MooseX 3pm" .TH Moose::Manual::MooseX 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::MooseX \- Recommended Moose extensions .SH VERSION .IX Header "VERSION" version 2.2207 .SH MooseX? .IX Header "MooseX?" It's easy to extend and change Moose, and this is part of what makes Moose so powerful. You can use the MOP API to do things your own way, add new features, and generally customize your Moose. .PP Writing your own extensions does require a good understanding of the meta-model. You can start learning about this with the Moose::Manual::MOP docs. There are also several extension recipes in the Moose::Cookbook. .PP Explaining how to write extensions is beyond the scope of this manual. Fortunately, lots of people have already written extensions and put them on CPAN for you. .PP This document covers a few of the ones we like best. .SH MooseX::AttributeHelpers .IX Header "MooseX::AttributeHelpers" The functionality of this MooseX module has been moved into Moose core. See Moose::Meta::Attribute::Native. .SH Moose::Autobox .IX Header "Moose::Autobox" MooseX::AttributeHelpers, but turned inside out, Moose::Autobox provides methods on both arrays/hashes/etc. but also references to them, using Moose roles, allowing you do to things like: .PP .Vb 1 \& use Moose::Autobox; \& \& $somebody_elses_object\->orders\->push($order); .Ve .PP Lexically scoped and not to everybody's taste, but very handy for sugaring up other people's APIs and your own code. .SH MooseX::StrictConstructor .IX Header "MooseX::StrictConstructor" By default, Moose lets you pass any old junk into a class's constructor. If you load MooseX::StrictConstructor, your class will throw an error if it sees something it doesn't recognize; .PP .Vb 1 \& package User; \& \& use Moose; \& use MooseX::StrictConstructor; \& \& has \*(Aqname\*(Aq; \& has \*(Aqemail\*(Aq; \& \& User\->new( name => \*(AqBob\*(Aq, emali => \*(Aqbob@example.com\*(Aq ); .Ve .PP With MooseX::StrictConstructor, that typo ("emali") will cause a runtime error. With plain old Moose, the "emali" attribute would be silently ignored. .SH MooseX::Params::Validate .IX Header "MooseX::Params::Validate" We have high hopes for the future of MooseX::Method::Signatures and Moops. However, these modules, while used regularly in production by some of the more insane members of the community, are still marked alpha just in case backwards incompatible changes need to be made. .PP If you don't want to risk that, for now we recommend the decidedly more clunky (but also faster and simpler) MooseX::Params::Validate. This module lets you apply Moose types and coercions to any method arguments. .PP .Vb 1 \& package User; \& \& use Moose; \& use MooseX::Params::Validate; \& \& sub login { \& my $self = shift; \& my ($password) \& = validated_list( \e@_, password => { isa => \*(AqStr\*(Aq, required => 1 } ); \& \& ... \& } .Ve .SH MooseX::Getopt .IX Header "MooseX::Getopt" This is a role which adds a \f(CW\*(C`new_with_options\*(C'\fR method to your class. This is a constructor that takes the command line options and uses them to populate attributes. .PP This makes writing a command-line application as a module trivially simple: .PP .Vb 1 \& package App::Foo; \& \& use Moose; \& with \*(AqMooseX::Getopt\*(Aq; \& \& has \*(Aqinput\*(Aq => ( \& is => \*(Aqro\*(Aq, \& isa => \*(AqStr\*(Aq, \& required => 1 \& ); \& \& has \*(Aqoutput\*(Aq => ( \& is => \*(Aqro\*(Aq, \& isa => \*(AqStr\*(Aq, \& required => 1 \& ); \& \& sub run { ... } .Ve .PP Then in the script that gets run we have: .PP .Vb 1 \& use App::Foo; \& \& App::Foo\->new_with_options\->run; .Ve .PP From the command line, someone can execute the script: .PP .Vb 1 \& foo@example> foo \-\-input /path/to/input \-\-output /path/to/output .Ve .SH MooseX::Singleton .IX Header "MooseX::Singleton" To be honest, using a singleton is just a way to have a magic global variable in languages that don't actually have global variables. .PP In perl, you can just as easily use a global. However, if your colleagues are Java-infected, they might prefer a singleton. Also, if you have an existing class that \fIisn't\fR a singleton but should be, using MooseX::Singleton is the easiest way to convert it. .PP .Vb 1 \& package Config; \& \& use MooseX::Singleton; # instead of Moose \& \& has \*(Aqcache_dir\*(Aq => ( ... ); .Ve .PP It's that simple. .SH "EXTENSIONS TO CONSIDER" .IX Header "EXTENSIONS TO CONSIDER" There are literally dozens of other extensions on CPAN. This is a list of extensions that you might find useful, but we're not quite ready to endorse just yet. .SS MooseX::Declare .IX Subsection "MooseX::Declare" MooseX::Declare is based on Devel::Declare, a giant bag of crack originally implemented by mst with the goal of upsetting the perl core developers so much by its very existence that they implemented proper keyword handling in the core. .PP As of perl5 version 14, this goal has been achieved, and modules such as Devel::CallParser, Function::Parameters, and Keyword::Simple provide mechanisms to mangle perl syntax that don't require hallucinogenic drugs to interpret the error messages they produce. .PP If you want to use declarative syntax in new code, please for the love of kittens get yourself a recent perl and look at Moops instead. .SS MooseX::Types .IX Subsection "MooseX::Types" This extension helps you build a type library for your application. It also lets you predeclare type names and use them as barewords. .PP .Vb 2 \& use MooseX::Types \-declare => [\*(AqPositiveInt\*(Aq]; \& use MooseX::Types::Moose \*(AqInt\*(Aq; \& \& subtype PositiveInt, \& as Int, \& where { $_ > 0 }, \& message { "Int is not larger than 0" }; .Ve .PP One nice feature is that those bareword names are actually namespaced in Moose's type registry, so multiple applications can use the same bareword names, even if the type definitions differ. .SS MooseX::Types::Structured .IX Subsection "MooseX::Types::Structured" This extension builds on top of MooseX::Types to let you declare complex data structure types. .PP .Vb 3 \& use MooseX::Types \-declare => [ qw( Name Color ) ]; \& use MooseX::Types::Moose qw(Str Int); \& use MooseX::Types::Structured qw(Dict Tuple Optional); \& \& subtype Name \& => as Dict[ first => Str, middle => Optional[Str], last => Str ]; \& \& subtype Color \& => as Tuple[ Int, Int, Int, Optional[Int] ]; .Ve .PP Of course, you could always use objects to represent these sorts of things too. .SS MooseX::ClassAttribute .IX Subsection "MooseX::ClassAttribute" This extension provides class attributes for Moose classes. The declared class attributes are introspectable just like regular Moose attributes. .PP .Vb 1 \& package User; \& \& use Moose; \& use MooseX::ClassAttribute; \& \& has \*(Aqname\*(Aq => ( ... ); \& \& class_has \*(AqCache\*(Aq => ( ... ); .Ve .PP Note however that this class attribute does \fInot\fR inherit like a Class::Data::Inheritable or similar attribute \- calling .PP .Vb 1 \& $subclass\->Cache($cache); .Ve .PP will set it for the superclass as well. Additionally, class data is usually The Wrong Thing To Do in a strongly OO program since it makes testing a lot harder \- consider carefully whether you'd be better off with an object that's passed around instead. .SS MooseX::Daemonize .IX Subsection "MooseX::Daemonize" This is a role that provides a number of methods useful for creating a daemon, including methods for starting and stopping, managing a PID file, and signal handling. .SS MooseX::Role::Parameterized .IX Subsection "MooseX::Role::Parameterized" If you find yourself wanting a role that customizes itself for each consumer, this is the tool for you. With this module, you can create a role that accepts parameters and generates attributes, methods, etc. on a customized basis for each consumer. .SS MooseX::POE .IX Subsection "MooseX::POE" This is a small wrapper that ties together a Moose class with \&\f(CW\*(C`POE::Session\*(C'\fR, and gives you an \f(CW\*(C`event\*(C'\fR sugar function to declare event handlers. .SS MooseX::FollowPBP .IX Subsection "MooseX::FollowPBP" Automatically names all accessors \fIPerl Best Practices\fR\-style, "get_size" and "set_size". .SS MooseX::SemiAffordanceAccessor .IX Subsection "MooseX::SemiAffordanceAccessor" Automatically names all accessors with an explicit set and implicit get, "size" and "set_size". .SS MooseX::NonMoose .IX Subsection "MooseX::NonMoose" MooseX::NonMoose allows for easily subclassing non-Moose classes with Moose, taking care of the annoying details connected with doing this, such as setting up proper inheritance from Moose::Object and installing (and inlining, at make_immutable time) a constructor that makes sure things like BUILD methods are called. .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.