x
Loading
 Loading
Hello, Guest | Login | Register

The Moose is Flying, Part One

Build better classes faster with the new Perl package named Moose.

Perl’s object system is very” flexible,” meaning that you get to build it from the ground up. You can build traditional hash-based objects, or more exotic forms, such as array-based or inside-out objects. However, in the two latter cases, you also have the onus to create the accessors, define access policies, and write a lot of repetitive code.

Luckily, Perl is introspective enough that you can get it to do most of the hard, boring work for you. Indeed, a number of simplifying” class frameworks” can now be found in the CPAN. The Moose framework appeared about a year ago, and I initially dismissed it as” yet another class framework”, much as I feel about yet another template system and yet another object-relational mapper (ORM).

However, I recently took a look at what Moose has become and was pleasantly surprised. As I started playing with it, I exclaimed frequently that Moose would have saved me quite a bit of time on some past projects, such as the text I wrote for the Intermediate Perl course and book, parts of which have been included as the perlboot man page in the Perl distribution.

Let’s recreate the” animal” classes from that text, using Moose, to see how this emerging framework simplifies things.

First, create a horse class in Horse.pm that has a name and a color:

 package Horse; use Moose; has 'name' => (is => 'rw'); has 'color' => (is => 'rw'); 1; 

Importing Moose defines has, which…

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Helpful Tools for Software Developers
  2. The Github Hall of Fame
  3. Book'em, Github.
  4. This Week on Github: Stupid Ruby Tricks
  5. A Veritable Scatter Shot!
Follow Linux Magazine
Rackspace