Object Oriented Perl

www.perl.org
 

Moose - Object Oriented Perl

Why code what you can declare? Moose gives you clear object syntax with lots of power when you need it.

Perl has had Object Oriented (OO) capabilities for fifteen years now, and today it has Moose. Moose helps developers focus on the underlying logic and requirements behind any project; encourages code reuse, simplification (making testing easier) and promotes best practices.

With Moose, gone are the days of writing constructors, destructors, and accessors. Moose provides a simple, declarative syntax for specifying classes. Most attribute validation code simply disappears. Moose handles it automatically as soon as the data type is specified.

Roles allow for easy reuse of behavior independent of class data. Moose is built to be as flexible as Perl, it can be change to suit your projects needs—see the growing MooseX namespace on CPAN. One example of a MooseX module is MooseX::NonMoose, designed to make migrating existing codebases easier. And best of all, the user interface to Moose is Just Perl™, so developers don't need to learn a new syntax.

Attributes in Moose are simple and straightforward. A programmer declares them with a simple function call, using named parameters like many other Perl libraries. This declaration can specify whether the attribute is writable, what kind of values it accepts, a default for the attribute, custom builder / clearer methods, and more. Given type information for an attribute, Moose will optionally try to convert values to that type or throw an exception if it cannot do so. This checking allows the programmer to focus on writing their logic, without getting bogged down in the details of value checking.

Just as attributes change how Moose class data is handled, roles change how class behavior is written. Roles are reusable collections of methods that can be added to a class or an object at runtime. With roles, one can write behavior for a collection of classes without needing to know or care about the data those classes manage. Roles can require methods to exist in the classes that use them, and in this way can function like interfaces in more traditional object-oriented languages like Java and C#. As with attributes, exceptions will be thrown upon the failure to satisfy these criteria, providing a structured mechanism for adding behavior to classes.

Well-steeped in the open development model of other Perl projects, Moose has a growing number of committers with a growing number of tests, over four thousand as of this writing.

Quotes

  • "I write less code thanks to Moose, I write better code thanks to Moose. Basically I am a happier person all around because Moose takes much of the drudge work out of my day. Thanks to the Moose team." -- Ben Hengst
  • "What? I can only give it five stars?" -- ctbrown

Resources:

Article Author

Chris Nehren