Moose A postmodern metaclass-based object system for Perl 5
Objects in Perl 5 (a short intro) A blessed hashref
Manual  new()  method
No real attributes bless {}, __PACKAGE__; sub new { my ( $class, @args ) =  @_; my $self = { @args }; # params bless $self, $class; return $self; } sub name { my ( $self, $name ) = @_; $name and $self->{'name'} = $name; return $self->{'name'}; }
Why would you want to use Moose? Moose package Person; use strict; use warnings; use Carp qw( confess ); use DateTime; use DateTime::Format::Natural; sub new { my $class = shift; my %p = ref $_[0] ? %{ $_[0] } : @_; exists $p{name} or confess 'name is a required attribute'; $class->_validate_name( $p{name} ); exists $p{birth_date} or confess 'birth_date is a required attribute'; $p{birth_date} = $class->_coerce_birth_date( $p{birth_date} ); $class->_validate_birth_date( $p{birth_date} ); $p{shirt_size} = 'l' unless exists $p{shirt_size}: $class->_validate_shirt_size( $p{shirt_size} ); return bless \%p, $class; } sub _validate_name { shift; my $name = shift; local $Carp::CarpLevel = $Carp::CarpLevel + 1; defined $name or confess 'name must be a string'; } Plain old Perl 5 package User; use Email::Valid; use Moose; use Moose::Util::TypeConstraints; extends 'Person'; subtype 'Email' => as 'Str' => where { Email::Valid->address($_) } => message { "$_ is not a valid email address" }; has email_address => ( is  => 'rw', isa  => 'Email', required => 1, );
Get it?
Defining an object in Moose use strict
use warnings
An object!
new()  method
A pon-.. err.. a moose! package User; use Moose; 1; You get:
Full-Affordance accessors ' ro ' is also available for read-only attributes!
You can set the getter or setter manually via  reader / writer
I'll show more attribute options later on has name => ( is => 'rw', );
An attribute with type constraint Lots of types:  Str ,  Int ,  ArrayRef ,  HashRef ,  CodeRef ,  Regexp
You can combine:  ArrayRef[Str] ,  HashRef[ArrayRef[Int]]
They have inheritance:  Int  is a  Num
Roll your own using  subtype package User; use Moose; has name => ( is  => 'rw', isa => 'Str', ); 1;
Methods are the same as before sub method { my $self = shift; ... $self->more(); }
Inheritance is as easy as... Multiple inheritance is also possible,  extends  accepts an array package Punk; use Moose; extends 'Person'; 1; package Child; use Moose; extends qw/ Father Mother /; 1;

Moose (Perl 5)

  • 1.
    Moose A postmodernmetaclass-based object system for Perl 5
  • 2.
    Objects in Perl5 (a short intro) A blessed hashref
  • 3.
    Manual new() method
  • 4.
    No real attributesbless {}, __PACKAGE__; sub new { my ( $class, @args ) = @_; my $self = { @args }; # params bless $self, $class; return $self; } sub name { my ( $self, $name ) = @_; $name and $self->{'name'} = $name; return $self->{'name'}; }
  • 5.
    Why would youwant to use Moose? Moose package Person; use strict; use warnings; use Carp qw( confess ); use DateTime; use DateTime::Format::Natural; sub new { my $class = shift; my %p = ref $_[0] ? %{ $_[0] } : @_; exists $p{name} or confess 'name is a required attribute'; $class->_validate_name( $p{name} ); exists $p{birth_date} or confess 'birth_date is a required attribute'; $p{birth_date} = $class->_coerce_birth_date( $p{birth_date} ); $class->_validate_birth_date( $p{birth_date} ); $p{shirt_size} = 'l' unless exists $p{shirt_size}: $class->_validate_shirt_size( $p{shirt_size} ); return bless \%p, $class; } sub _validate_name { shift; my $name = shift; local $Carp::CarpLevel = $Carp::CarpLevel + 1; defined $name or confess 'name must be a string'; } Plain old Perl 5 package User; use Email::Valid; use Moose; use Moose::Util::TypeConstraints; extends 'Person'; subtype 'Email' => as 'Str' => where { Email::Valid->address($_) } => message { "$_ is not a valid email address" }; has email_address => ( is => 'rw', isa => 'Email', required => 1, );
  • 6.
  • 7.
    Defining an objectin Moose use strict
  • 8.
  • 9.
  • 10.
  • 11.
    A pon-.. err..a moose! package User; use Moose; 1; You get:
  • 12.
    Full-Affordance accessors 'ro ' is also available for read-only attributes!
  • 13.
    You can setthe getter or setter manually via reader / writer
  • 14.
    I'll show moreattribute options later on has name => ( is => 'rw', );
  • 15.
    An attribute withtype constraint Lots of types: Str , Int , ArrayRef , HashRef , CodeRef , Regexp
  • 16.
    You can combine: ArrayRef[Str] , HashRef[ArrayRef[Int]]
  • 17.
  • 18.
    Roll your ownusing subtype package User; use Moose; has name => ( is => 'rw', isa => 'Str', ); 1;
  • 19.
    Methods are thesame as before sub method { my $self = shift; ... $self->more(); }
  • 20.
    Inheritance is aseasy as... Multiple inheritance is also possible, extends accepts an array package Punk; use Moose; extends 'Person'; 1; package Child; use Moose; extends qw/ Father Mother /; 1;