package Person;
use strict;
use warnings;
sub new {
my ($class) = @_;
return bless {
name => '',
age => undef,
}, $class;
}
sub name {
my ($self, $name) = @_;
$self->{'name'} = $name if $name;
return $self->{'name'};
}
sub age {
my ($self, $age) = @_;
$self->{'age'} = $age if $age;
return $self->{'age'};
}
1;
package Person;
use Moose;
has name => (is => 'rw');
has age => (is => 'rw');
1;
use Moose;
has, extends, with, before, after, around, super, override, inner, augmentuse strict; and use warnings;Carp::confess and Scalar::Util::blessed@ISA to Moose::Object (unless it’s already set)
package Person;
use Moose;
has name => (is => 'rw');
has age => (is => 'rw');
1;
has declares attributes
is => 'rw' create read/write accessoris => 'ro' create read only accessorMoose::Object provides one for us
package Person;
use Moose;
has name => (
is => 'rw',
isa => 'Str'
default => 'Bob'
);
has age => (
is => 'rw',
isa => 'Int',
default => '0'
);
1;
has name => (
is => 'rw',
isa => 'Str'
default => 'Bob'
);
isa sets up Type checking
Moose::Util::TypeConstraints.Any, Item, Bool, Undef, Defined, Value, Num, Int, Str, Ref, ScalarRef, ArrayRef, HashRef, CodeRef, RegexpRef, GlobRef, FileHandle, Object and Role
has 'date' => (isa => 'DateTime'); # This will DWIM
has name => (
is => 'rw',
isa => 'Str'
default => 'Bob'
);
has staff => (
is => 'ro',
isa => 'ArrayRef',
default => sub { [qw(Bob Alice Tim)] },
);
default values are either …
default sub is passed $self
package Person;
use Moose;
has name => (is => 'rw');
has age => (is => 'rw');
package Employee;
use Moose;
extends qw(Person);
has manager => (
is => 'ro',
isa => 'Manager',
required => 1, # Everybody has to have a manager
);
package Manager;
use Moose;
extends qw(Employee);
has staff => (
is => 'rw',
isa => 'ArrayRef',
default => sub { [] },
);
package Employee;
use Moose;
extends qw(Person);
has manager => (
is => 'ro',
isa => 'Manager',
required => 1, # Everybody has to have a manager
);
extends declares the parent classesextends is preferred over use base because it fully replaces the @ISA rather than extending it
Moose::Object
package Employee;
use Moose;
use Moose::Util::TypeConstraints;
extends qw(Person);
subtype 'Manager'
=> as 'Object'
=> where { $_[0]->isa('Manager') };
coerce 'Manager'
=> from 'Str'
=> via { Manager->new( name => $_) };
has manager => (
is => 'ro',
isa => 'Manager',
required => 1,
coerce => 1,
);
use Moose::Util::TypeConstraints;
subtype 'Manager'
=> as 'Object'
=> where { $_[0]->isa('Manager') };
coerce 'Manager'
=> from 'Str'
=> via { Manager->new( name => $_) };
has manager => (
...
coerce => 1,
);
package TeamMember;
use Moose;
extends qw(Employee);
with qw(ProjectTeamRole);
has 'weekly_hours' => (
is => 'ro',
isa => 'Int',
default => 40,
);
1;
with adds a Role (ProjectTeamRole) to the Class (TeamMember)Moose::Object provides a does() method to check objects for Roles
$emp->does('ProjectTeamRole'); # true for TeamMembers
package ProjectTeamRole;
use Moose::Role;
requires qw('weekly_hours');
has project => (
is => 'ro',
isa => 'Str',
required => 1,
)
has project_hours => (
is => 'rw',
isa => 'Int',
default => 0,
)
sub log_hours {
my ($self, $hours) = @_;
my $total = $self->project_hours + $hours;
$self->project_hours($total);
}
1;
package ProjectTeamRole;
use Moose::Role;
requires qw('weekly_hours');
use Moose::Role
requires and excludesrequires says that the composing class must have a weekly_hours methodextends throws an exception
has project => (
is => 'ro',
isa => 'Str',
required => 1,
)
has project_hours => (
is => 'rw',
isa => 'Int',
default => 0,
)
sub log_hours {
my ($self, $hours) = @_;
my $total = $self->project_hours + $hours;
$self->project_hours($total);
}
project and project_hourslog_hours basically increments the project_hours by a given amountThere are several pre-baked Roles on the CPAN that provide various features
MooseX::Storage - Serialization to JSON or YAMLMooseX::Daemonize - Daemonization and handling of pid filesMooseX::Getopt- Command Line ParsingMooseX::IOC - Support for Inversion of Control paradigmMooseX::Param - Provide a param() method similar to CGI.pm’sAnd Several more in the Moose Repository waiting to be released
MooseX::Workers - forked process management for concurrently running tasksMooseX::Service - Turns your class into a lazy service locatorMooseX::LogDispatch - Provide a standard Logging objectAnd you for coming