Moose::Manual::MooseX - Recommended Moose extensions



NAME

Moose::Manual::MooseX - Recommended Moose extensions


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.

Writing your own extensions does require a good understanding of the meta-model. You can start learning about this with the the Moose::Manual::MOP manpage docs. There are also several extension recipes in the the Moose::Cookbook manpage.

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.

This document covers a few of the ones we like best.


the MooseX::AttributeHelpers manpage

If you only look at one extension, it should be this one. It provides the equivalent of delegation for all of Perl's native data types, such as array reference, hash references, numbers, strings, etc.

This lets you create much cleaner and fluent APIs.

  package User;
  use Moose;
  use MooseX::AttributeHelpers;
  has '_orders' => (
      metaclass => 'Collection::Array',
      is        => 'ro',
      isa       => 'ArrayRef',
      default   => sub { [] },
      provides  => {
          push     => 'add_order',
          shift    => 'next_order',
          elements => 'orders',
      },
  );

Instead of directly exposing an array reference, we have three well-named, easy to use methods.


the MooseX::StrictConstructor manpage

By default, Moose lets you pass any old junk into a class's constructor. If you load the MooseX::StrictConstructor manpage, your class will throw an error if it sees something it doesn't recognize;

  package User;
  use Moose;
  use MooseX::StrictConstructor;
  has 'name';
  has 'email';
  User->new( name => 'Bob', emali => 'bob@example.com' );

With the MooseX::StrictConstructor manpage, that typo ("emali") will cause a runtime error. With plain old Moose, the "emali" attribute would be silently ignored.


the MooseX::Params::Validate manpage

We have high hopes for the future of the MooseX::Method::Signatures manpage and the MooseX::Declare manpage. However, for now we recommend the decidedly more clunky (but also faster and simpler) the MooseX::Params::Validate manpage. This module lets you apply Moose types and coercions to any method arguments.

  package User;
  use Moose;
  use MooseX::Params::Validate;
  sub login {
      my $self = shift;
      my ($password)
          = validated_list( \@_, password => { isa => 'Str', required => 1 } );
      ...
  }


the MooseX::Getopt manpage

This is a role which adds a new_with_options method to your class. This is a constructor that takes the command line options and uses them to populate attributes.

This makes writing a command-line application as a module trivially simple:

  package App::Foo;
  use Moose;
  with 'MooseX::Getopt';
  has 'input' => (
      is       => 'ro',
      isa      => 'Str',
      required => 1
  );
  has 'output' => (
      is       => 'ro',
      isa      => 'Str',
      required => 1
  );
  sub run { ... }

Then in the script that gets run we have:

  use App::Foo;
  App::Foo->new_with_options->run;

From the command line, someone can execute the script:

  foo@example> foo --input /path/to/input --output /path/to/output


the MooseX::Singleton manpage

To be honest, using a singleton is often a hack, but it sure is a handy hack. the MooseX::Singleton manpage lets you have a Moose class that's a singleton:

  package Config;
  use MooseX::Singleton; # instead of Moose
  has 'cache_dir' => ( ... );

It's that simple.


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.

the MooseX::Declare manpage

Extends Perl with Moose-based keywords using Devel::Declare. Very cool, but still new and experimental.

  class User {
      has 'name'  => ( ... );
      has 'email' => ( ... );
      method login (Str $password) { ... }
        }

the MooseX::Types manpage

This extension helps you build a type library for your application. It also lets you predeclare type names and use them as barewords.

  use MooseX::Types -declare => ['PositiveInt'];
  use MooseX::Types::Moose 'Int';
  subtype PositiveInt
      => as Int,
      => where { $_ > 0 }
      => message {"Int is not larger than 0"};

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.

the MooseX::Types::Structured manpage

This extension builds on top of the MooseX::Types manpage to let you declare complex data structure types.

  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] ];

Of course, you could always use objects to represent these sorts of things too.

the MooseX::ClassAttribute manpage

This extension provides class attributes for Moose classes. The declared class attributes are introspectable just like regular Moose attributes.

  package User;
  use Moose;
  use MooseX::ClassAttribute;
  has 'name' => ( ... );
  class_has 'Cache' => ( ... );

the MooseX::Daemonize manpage

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.

the MooseX::Role::Parameterized manpage

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.

the MooseX::POE manpage

This is a small wrapper that ties together a Moose class with POE::Session, and gives you an event sugar function to declare event handlers.

the MooseX::FollowPBP manpage

Automatically names all accessors Perl Best Practices-style, "get_size" and "set_size".

the MooseX::SemiAffordanceAccessor manpage

Automatically names all accessors with an explicit set and implicit get, "size" and "set_size".


AUTHOR

Dave Rolsky <autarch@urth.org>


COPYRIGHT AND LICENSE

Copyright 2009 by Infinity Interactive, Inc.

http://www.iinteractive.com

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

 Moose::Manual::MooseX - Recommended Moose extensions