SlideShare a Scribd company logo
MooseBest Practicesby Aran Deltac (CPAN: bluefeet)
make_immutable()package MyClass;use Moose;…__PACKAGE__->meta->make_immutable;1;
default => $valuehas ‘is_clean’ => (   is => ‘ro’,isa => ‘Bool’,   default => 1,);
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) { … }
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) { … }
required => 1has ‘ssn’ => (    is => ‘ro’,isa => ‘Str’,   required => 1,);
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[] [ … ]…
builder => $sub_namehas ‘dbh’ => (    is => ‘ro’,isa => ‘dbh’,builder => ‘_build_dbh’,);sub _build_dbh {    return DBI->connect(…);}
lazy => $boolhas ‘dbh’ => (    is => ‘ro’,isa => ‘dbh’,    builder => ‘_build_dbh’,lazy    => 1,);sub _build_dbh {    return DBI->connect(…);}
lazy_build => 1has ‘dbh’ => (    is => ‘ro’,isa => ‘DBI::db’,lazy_build => 1,);sub _build_dbh {   return DBI->connect( … );}
is => ‘ro’has ‘name’ => (    is => ‘ro’,isa => ‘Str’,);
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!
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!
writer => $sub_namehas ‘color’ => (   is => ‘ro’,isa => ‘Str’,write => ‘_color’,);
init_arg => undefhas ‘result’ => (    is => ‘ro’,isa => ‘HashRef’,   writer => ‘_set_result’,init_arg => undef,);

More Related Content

ODP
Moose talk at FOSDEM 2011 (Perl devroom)
KEY
Intro To Moose
ODP
Moose (Perl 5)
PDF
OO Perl with Moose
KEY
Introduction To Moose
ODP
Evolving Software with Moose
PDF
Introduction to Moose
ODP
Moose - YAPC::NA 2012
Moose talk at FOSDEM 2011 (Perl devroom)
Intro To Moose
Moose (Perl 5)
OO Perl with Moose
Introduction To Moose
Evolving Software with Moose
Introduction to Moose
Moose - YAPC::NA 2012

What's hot (18)

PDF
Moose workshop
PDF
WordPress Cuztom Helper
PDF
DBIx::Class beginners
PDF
DBIx::Class introduction - 2010
KEY
(Parameterized) Roles
PDF
Doctrine 2
PDF
A Few of My Favorite (Python) Things
KEY
Introduction to Perl Best Practices
PPT
Hack in the Box Keynote 2006
ODP
Introduction to Domain-Driven Design
PDF
Rapid web development, the right way.
PDF
CakeFest 2013 keynote
PDF
Chapter 2: What's your type?
PPTX
Bioinformatics p5-bioperl v2013-wim_vancriekinge
PPTX
Bioinformatica p6-bioperl
PDF
Bash Learning By Examples
PDF
Ruby Programming Language
Moose workshop
WordPress Cuztom Helper
DBIx::Class beginners
DBIx::Class introduction - 2010
(Parameterized) Roles
Doctrine 2
A Few of My Favorite (Python) Things
Introduction to Perl Best Practices
Hack in the Box Keynote 2006
Introduction to Domain-Driven Design
Rapid web development, the right way.
CakeFest 2013 keynote
Chapter 2: What's your type?
Bioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatica p6-bioperl
Bash Learning By Examples
Ruby Programming Language
Ad

Viewers also liked (14)

PPT
DBIC 2 - Resultsets
PPTX
Tools of the CPAN Ninja
PDF
How to Contribute to Apache Usergrid
PPTX
Introduction to Apache Cassandra
PDF
Building Mobile Apps with Apache UserGrid, the Open Source Baas
PDF
Usergrid Overview
PDF
Open Source Mobile Backend on Cassandra
PDF
Introduction to Cassandra Basics
PDF
Indexing in Cassandra
PDF
Introduction to Apache Cassandra
PDF
Cassandra By Example: Data Modelling with CQL3
PDF
Advanced data modeling with apache cassandra
PPTX
An Overview of Apache Cassandra
PPTX
Cassandra Data Modeling - Practical Considerations @ Netflix
DBIC 2 - Resultsets
Tools of the CPAN Ninja
How to Contribute to Apache Usergrid
Introduction to Apache Cassandra
Building Mobile Apps with Apache UserGrid, the Open Source Baas
Usergrid Overview
Open Source Mobile Backend on Cassandra
Introduction to Cassandra Basics
Indexing in Cassandra
Introduction to Apache Cassandra
Cassandra By Example: Data Modelling with CQL3
Advanced data modeling with apache cassandra
An Overview of Apache Cassandra
Cassandra Data Modeling - Practical Considerations @ Netflix
Ad

Similar to Moose Best Practices (11)

PPT
Functional Pe(a)rls version 2
ODP
Functional Pearls 4 (YAPC::EU::2009 remix)
ODP
Modern Perl
ODP
Perl 6 Datastructures
PDF
First step of Performance Tuning
PPTX
Marcs (bio)perl course
PDF
Good Evils In Perl
ODP
Perl Teach-In (part 2)
PPT
Oo Perl
PDF
Method::Signatures
PDF
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Functional Pe(a)rls version 2
Functional Pearls 4 (YAPC::EU::2009 remix)
Modern Perl
Perl 6 Datastructures
First step of Performance Tuning
Marcs (bio)perl course
Good Evils In Perl
Perl Teach-In (part 2)
Oo Perl
Method::Signatures
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Electronic commerce courselecture one. Pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Encapsulation theory and applications.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Cloud computing and distributed systems.
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Electronic commerce courselecture one. Pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Review of recent advances in non-invasive hemoglobin estimation
MIND Revenue Release Quarter 2 2025 Press Release
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Building Integrated photovoltaic BIPV_UPV.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Encapsulation theory and applications.pdf
Network Security Unit 5.pdf for BCA BBA.
Cloud computing and distributed systems.
Understanding_Digital_Forensics_Presentation.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Chapter 3 Spatial Domain Image Processing.pdf
Programs and apps: productivity, graphics, security and other tools
Reach Out and Touch Someone: Haptics and Empathic Computing

Moose Best Practices

  • 1. MooseBest Practicesby Aran Deltac (CPAN: bluefeet)
  • 3. default => $valuehas ‘is_clean’ => ( is => ‘ro’,isa => ‘Bool’, default => 1,);
  • 4. 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) { … }
  • 5. 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) { … }
  • 6. required => 1has ‘ssn’ => ( is => ‘ro’,isa => ‘Str’, required => 1,);
  • 7. 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[] [ … ]…
  • 8. builder => $sub_namehas ‘dbh’ => ( is => ‘ro’,isa => ‘dbh’,builder => ‘_build_dbh’,);sub _build_dbh { return DBI->connect(…);}
  • 9. lazy => $boolhas ‘dbh’ => ( is => ‘ro’,isa => ‘dbh’, builder => ‘_build_dbh’,lazy => 1,);sub _build_dbh { return DBI->connect(…);}
  • 10. lazy_build => 1has ‘dbh’ => ( is => ‘ro’,isa => ‘DBI::db’,lazy_build => 1,);sub _build_dbh { return DBI->connect( … );}
  • 11. is => ‘ro’has ‘name’ => ( is => ‘ro’,isa => ‘Str’,);
  • 12. 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!
  • 13. 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!
  • 14. writer => $sub_namehas ‘color’ => ( is => ‘ro’,isa => ‘Str’,write => ‘_color’,);
  • 15. init_arg => undefhas ‘result’ => ( is => ‘ro’,isa => ‘HashRef’, writer => ‘_set_result’,init_arg => undef,);