Beginning Moose - Houston Perl Mongers

Beginning Moose
Houston Perl Mongers, April 10th 2014
What is Moose?
• Moose is a complete Object Oriented system for Perl
• Moose allows the developer to define your class
• It allows for objects to have attributes, roles,
methods, types and more.
Why Moose?
• Moose is easy to use and allows the developer to
focus more on what the object should be doing and
not how to implement it.
• Moose does not require the knowledge blessing
hashrefs and accessor methods.
• It also provides introspection for classes in order to
learn about attributes, methods etc.
Creating an Object Attribute
package Human;
use Moose;
has 'name' => (
is => 'rw',
isa => 'Str',
required => 1,
predicate => ‘has_name’
• Attributes are declared with the has keyword
No More of this! Yay!
package Thing;
sub new {
my $class = shift;
return bless {}, $class;
About Moose Attribute Properties
• An attribute is a property of a class that defines it. It
should have a name and can have many other
properties. Potential Moose attributes can include:
• Is
• Isa
• Required
• Predicate
Other Attribute Properties
• Clearer
• Builder
• Lazy
• Sub type
• Delegation
• Coercion
Example of Attribute clearer and builder
has 'weight' => (
is => 'rw',
isa => 'Int',
builder => '_build_weight',
clearer => 'clear_weight',
sub _build_weight { return 155; }
• Within our Human module from previous
Example of Sub Type
use Moose;
use Moose::Util::TypeConstraints;
subtype 'PositiveInt',
as 'Int’;
• Don’t forget to use
Moose::Util::TypeConstriants !
Using Our Moose Object
#! /usr/bin/perl
use Human;
my $human = Human->new(name => 'Dan The Man Culver');
• Because we made the name property
required in our Human module, it is required
to define the name when we call new
print "The human's name is ".$human->name."\n";
print $human->has_name."\n";
print $human->weight."\n";
print $human->weight."weight is now cleared\n";
• Our predicate property ‘has_name’ for our
module will return a true or false value
Extending our Moose Object
package Programmer;
use Moose;
• Extend an object with extends ‘<objectname>’
use DateTime;
extends 'Human';
has 'coffee' => (
is => 'rw',
isa => 'Str',
lazy => 1,
has 'money' => (
is => 'rw',
isa => PostiveInt,
# Continued next slide
Extending our Moose Object (Cont.)
has 'celebration' => (
is => 'rw',
isa => 'DateTime',
handles => { 'last_made_it_rain' => 'date' }
sub celebrate {
my $self = shift;
my $dollars = shift;
if ( $self->money ne $dollars ) { return 0; }
$self->celebration( DateTime->now() );
return 1;
Here in celebration attribute, handles is
an example of delegation in Moose
Using our new Object Extension
#! /usr/bin/perl
use Programmer;
my $programmer = Programmer->new(
name => 'Dan The Man Culver',
coffee => 'black',
money => 1000,
print $programmer->name ." made it rain on "
. $programmer->last_made_it_rain . " with "
. $programmer->money ." dollars while drinking "
. $programmer->coffee ." coffee.";
Using Moose Roles
#! /usr/bin/perl
package Coding;
use Moose::Role;
has 'is_coding' => (
is => 'rw',
isa => 'Bool',
sub code {
my $self = shift;
print "I'm trying to code..\n";
• It helps to think of roles as something that can be
done, or ask yourself it an ‘ing’ or ‘able’ can be
added to the end of the desired role. In this
example, a programmer can be coding
Using Moose Roles ( Cont. )
package Programmer;
use Moose;
use DateTime;
extends 'Human';
with 'Coding';
• To use a role, simply add with ‘<rolename>’
in the module that you intend on using the
role with.
Using our Moose Role
#! /usr/bin/perl
use Programmer;
my $programmer = Programmer->new(
name => 'Dan The Man Culver',
coffee => 'black',
money => 1000,
I’m trying to code..
( Output )
Recommended Resources
• Moose Newsletter/Mailing List
Any Questions?

similar documents