Moose Best Practices

15
Moose Best Practices by Aran Deltac (CPAN: bluefeet)

description

This presentation was created for, and presented at, the August 11th Thousand Oaks Perl Mongers meeting.

Transcript of Moose Best Practices

Page 1: Moose Best Practices

MooseBest Practices

by Aran Deltac (CPAN: bluefeet)

Page 2: Moose Best Practices

make_immutable()

package MyClass;use Moose;

__PACKAGE__->meta->make_immutable;1;

Page 3: Moose Best Practices

default => $value

has ‘is_clean’ => ( is => ‘ro’, isa => ‘Bool’, default => 1,);

Page 4: Moose Best Practices

Many Possible Stateshas ‘ages’ => ( is => ‘rw’, isa => ‘ArrayRef’,);

sub avg_age { my ($self) = @_; my $ages = $self->ages(); return (sum @$ages ) / scalar @$ages;}

Possible States:• undef• []• [ … ]

if (defined($ages) and @$ages) { … }

Page 5: Moose Best Practices

Many Possible Stateshas ‘ages’ => ( is => ‘rw’, isa => ‘ArrayRef’, default => sub{ [] },);

sub avg_age { my ($self) = @_; my $ages = $self->ages(); return (sum @$ages ) / scalar @$ages;}

Possible Values:• undef• []• [ … ]

if (@$ages) { … }

Page 6: Moose Best Practices

required => 1

has ‘ssn’ => ( is => ‘ro’, isa => ‘Str’, required => 1,);

Page 7: Moose Best Practices

Many Possible Statessubtype ‘NonEmptyArrayRef' => as ‘ArrayRef’ => where { @$_ > 0 };

has ‘ages’ => ( is => ‘rw’, isa => ‘NonEmptyArrayRef’, required => 1,);

sub avg_age { my ($self) = @_; my $ages = $self->ages(); return (sum @$ages ) / scalar @$ages;}

Possible Values:• undef• []• [ … ]

Page 8: Moose Best Practices

builder => $sub_name

has ‘dbh’ => ( is => ‘ro’, isa => ‘dbh’, builder => ‘_build_dbh’,);sub _build_dbh { return DBI->connect(…);}

Page 9: Moose Best Practices

lazy => $bool

has ‘dbh’ => ( is => ‘ro’, isa => ‘dbh’, builder => ‘_build_dbh’, lazy => 1,);sub _build_dbh { return DBI->connect(…);}

Page 10: Moose Best Practices

lazy_build => 1

has ‘dbh’ => ( is => ‘ro’, isa => ‘DBI::db’, lazy_build => 1,);

sub _build_dbh { return DBI->connect( … );}

Page 11: Moose Best Practices

is => ‘ro’

has ‘name’ => ( is => ‘ro’, isa => ‘Str’,);

Page 12: Moose Best Practices

Changing Statehas ‘ages’ => ( is => ‘rw’, isa => ‘ArrayRef’, default => sub{ [] },);

has ‘avg_age’ => ( is => ‘ro’, isa => ‘Int’, lazy_build => 1,);sub _build_avg_age { my ($self) = @_; my $ages = $self->ages(); return (sum @$ages ) / scalar @$ages;}

my $obj = Class->new( ages => [4, 7],);

print $obj->avg_age(); # 6

$obj->ages([4, 11]);

print $obj->avg_age(); # 6!

Page 13: Moose Best Practices

Changing Statehas ‘ages’ => ( is => ‘ro’, isa => ‘ArrayRef’, default => sub{ [] },);

has ‘avg_age’ => ( is => ‘ro’, isa => ‘Int’, lazy_build => 1,);sub _build_avg_age { my ($self) = @_; my $ages = $self->ages(); return (sum @$ages ) / scalar @$ages;}

my $obj = Class->new( ages => [4, 7],);

print $obj->avg_age(); # 6

$obj->ages([4, 11]);

print $obj->avg_age(); # 6!

Page 14: Moose Best Practices

writer => $sub_name

has ‘color’ => ( is => ‘ro’, isa => ‘Str’, write => ‘_color’,);

Page 15: Moose Best Practices

init_arg => undef

has ‘result’ => ( is => ‘ro’, isa => ‘HashRef’, writer => ‘_set_result’, init_arg => undef,);