Chapter 8. Moose

Support for object orientation in Perl was added later on and one has to build these features into their packages from startup. However, fortunately, there are many packages that extend the object orientation of Perl, just like there are many templating engines. Moose is the most widely accepted and promising extension of Perl's object orientation. Catalyst itself as of version 5.8 is rewritten to use Moose. Moose is based on Class-MOP, which implements the meta object protocol (MOP) in Perl. This allows MooseX extensions to be used in combination in a single class without them bothering each other. It also makes classes inspectable, which allows for things such as form generation by reflection of class attributes. In this chapter, we will explore Moose and how Catalyst applications can benefit from it. This chapter assumes that you are already familiar with the basics of object-oriented programming in Perl.

Moose introduction

Moose introduces declarative syntax for objects in Perl similar to Ruby. Moose is just another package technically and it can be used like any other package by including it with use Moose.

OO in Perl

Following are some fundamentals of OO in Perl:

  • Every package is a class: package packagename means that the code following in the rest of the scope is defining a class called packagename.
  • Every subroutine is a method: Every subroutine in a class becomes an object method. Can be avoided by using namespace::autoclean.
  • An object is only a blessed reference: Any reference that gets blessed is an object that can use the methods from the blessing class.

We are familiar with the following in the context of a class:

  • Properties
  • Methods that can manipulate these properties
  • Ways of instantiating itself as objects

Let us explore how Perl addresses this. Properties are achieved by creating a reference and blessing it. Please note that a blessed reference is an object. There's nothing more to it. However, we will have to write our own constructor that will create this blessed reference. There's no 'this' keyword in Perl. The class name or blessed reference is passed as first argument.

Following is a code sample of what such a class would look like:

package Whatever;
use strict;
use warnings;
sub new {
my $self = {
# List of properties are defined here
}
bless $self, 'Whatever';
return $self;
}

All this is just to get a mere working implementation of a class. Now, what does it take to ensure our class uses some of the best practices established by OO? For example, if we had to implement encapsulation and want only setters for certain properties. More so, what if we would like to validate the data for each of these properties before setting them? You can imagine, we will have to write a function for setting and getting the value and implement the validation logic, if any. Yes, there are CPAN modules that do this automatically. Yet, creating well-implemented classes are not that friendly. Having the liberty to implement the properties of our own class leaves room for so much flexibility. Following is an explanation of how Moose addresses the challenges above without necessarily destroying the flexibility.

OO in Moose

We will discover in this chapter how Moose simplifies the process.

Properties

When using Moose, a property can be implemented by just saying:

has 'propertyname' => (
is => '[rw|ro|bare]',
isa => '[datatype']
)

Note: Normal parenthesis.

As shown, the syntax is very declarative and one has to only say that the class has a certain property and is readable or writable or both and that it is a string or integer type or whatever predefined or custom data type you want it to be!

Class method

Defining methods in Perl has always been only declaring the subroutine. So Moose does not have much to offer here. However, Moose offers what are known as method modifiers. Method modifiers are subroutines that need to be executed before, after, or around the execution of the method itself. This feature is especially important for inheritance and other hierarchy design principles. For example:

sub runfast
{
}

is a method. Please note that the () would be an empty prototype, which doesn't make sense on a method.

Following could be before and after modifiers applied to the method:

before 'runfast' => sub {
print "Warm up First";
};
after 'runfast' => sub {
print "Cool Down";
};

Instantiating objects

Moose provides a new() method for every package, which can be used as a constructor. The flexibility that comes with writing your own constructors in Perl is still available by defining a BUILD method. The BUILD method is called for any manipulation immediately after Moose creates the new object. You do not need to use method modifiers or other means of calling a parent class' BUILD method. Moose will automatically call all BUILD methods in the hierarchy in the correct order.

Inheritance

Moose makes inheritance simple, which is illustrated in the following code:

Package Creation;
Use Moose;
#Some Code Here
Package Human
Use Moose;
extends 'Creation';

# Now this class can make use of properties and methods from Creation.

All that has to be said is class child extends parents.

An interesting concept that Moose introduces is called roles. Roles are encapsulated behavior. Roles as we will explore later will make the creation of plugins in Catalyst simpler. Roles are not classes, so they cannot be instantiated. However, they (the properties and methods the package defines) can be used by other classes or roles. You can think of roles as abstract classes or interfaces. Abstract classes or interfaces are classes that can't be used directly but define methods that classes inheriting them should implement. This is illustrated in the following code example:

Package Creation;
Use Moose::Role;
Requires 'sensoryinput';
Package Human;
Use Moose;
with 'Creation';
# Now this class has to define sensoryinput as it uses the role creation
sub sensoryinput {
#some code
}

The last code example is a summary of most commonly used OO concepts and its syntax in Moose. This by no means is a primer on Moose; however, this will help you to quickly relate to some of the text covered later. However, I recommend learning to use Moose and its fundamentals. There is a Moose cookbook on cpan. Also, I find the following links interesting and a quick refresher on Moose, especially if you have read the other Perl OO tutorials such as perlboot at:

http://www.stonehenge.com/merlyn/LinuxMag/col94.html

http://www.stonehenge.com/merlyn/LinuxMag/col95.html

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.43.26