SlideShare une entreprise Scribd logo
1  sur  15
MooseBest Practices by Aran Deltac (CPAN: bluefeet)
make_immutable() package MyClass; use Moose; … __PACKAGE__->meta->make_immutable; 1;
default => $value has ‘is_clean’ => (    is => ‘ro’, isa => ‘Bool’,    default => 1, );
Many Possible States has ‘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 States has ‘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 => 1 has ‘ssn’ => (     is => ‘ro’, isa => ‘Str’,    required => 1, );
Many Possible States subtype ‘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_name has ‘dbh’ => (     is => ‘ro’, isa => ‘dbh’, builder => ‘_build_dbh’, ); sub _build_dbh {     return DBI->connect(…); }
lazy => $bool has ‘dbh’ => (     is => ‘ro’, isa => ‘dbh’,     builder => ‘_build_dbh’, lazy    => 1, ); sub _build_dbh {     return DBI->connect(…); }
lazy_build => 1 has ‘dbh’ => (     is => ‘ro’, isa => ‘DBI::db’, lazy_build => 1, ); sub _build_dbh {    return DBI->connect( … ); }
is => ‘ro’ has ‘name’ => (     is => ‘ro’, isa => ‘Str’, );
Changing State has ‘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 State has ‘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_name has ‘color’ => (    is => ‘ro’, isa => ‘Str’, write => ‘_color’, );
init_arg => undef has ‘result’ => (     is => ‘ro’, isa => ‘HashRef’,    writer => ‘_set_result’, init_arg => undef, );

Contenu connexe

Tendances

Bash Learning By Examples
Bash Learning By ExamplesBash Learning By Examples
Bash Learning By Examples
Arun Bagul
 

Tendances (18)

Moose
MooseMoose
Moose
 
Moose workshop
Moose workshopMoose workshop
Moose workshop
 
WordPress Cuztom Helper
WordPress Cuztom HelperWordPress Cuztom Helper
WordPress Cuztom Helper
 
DBIx::Class beginners
DBIx::Class beginnersDBIx::Class beginners
DBIx::Class beginners
 
DBIx::Class introduction - 2010
DBIx::Class introduction - 2010DBIx::Class introduction - 2010
DBIx::Class introduction - 2010
 
(Parameterized) Roles
(Parameterized) Roles(Parameterized) Roles
(Parameterized) Roles
 
Doctrine 2
Doctrine 2Doctrine 2
Doctrine 2
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
 
Introduction to Perl Best Practices
Introduction to Perl Best PracticesIntroduction to Perl Best Practices
Introduction to Perl Best Practices
 
Hack in the Box Keynote 2006
Hack in the Box Keynote 2006Hack in the Box Keynote 2006
Hack in the Box Keynote 2006
 
Introduction to Domain-Driven Design
Introduction to Domain-Driven DesignIntroduction to Domain-Driven Design
Introduction to Domain-Driven Design
 
Rapid web development, the right way.
Rapid web development, the right way.Rapid web development, the right way.
Rapid web development, the right way.
 
CakeFest 2013 keynote
CakeFest 2013 keynoteCakeFest 2013 keynote
CakeFest 2013 keynote
 
Chapter 2: What's your type?
Chapter 2: What's your type?Chapter 2: What's your type?
Chapter 2: What's your type?
 
Bioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeBioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekinge
 
Bioinformatica p6-bioperl
Bioinformatica p6-bioperlBioinformatica p6-bioperl
Bioinformatica p6-bioperl
 
Bash Learning By Examples
Bash Learning By ExamplesBash Learning By Examples
Bash Learning By Examples
 
Ruby Programming Language
Ruby Programming LanguageRuby Programming Language
Ruby Programming Language
 

En vedette

Tools of the CPAN Ninja
Tools of the CPAN NinjaTools of the CPAN Ninja
Tools of the CPAN Ninja
Aran Deltac
 
Introduction to Apache Cassandra
Introduction to Apache CassandraIntroduction to Apache Cassandra
Introduction to Apache Cassandra
Aran Deltac
 

En vedette (14)

DBIC 2 - Resultsets
DBIC 2 - ResultsetsDBIC 2 - Resultsets
DBIC 2 - Resultsets
 
Tools of the CPAN Ninja
Tools of the CPAN NinjaTools of the CPAN Ninja
Tools of the CPAN Ninja
 
How to Contribute to Apache Usergrid
How to Contribute to Apache UsergridHow to Contribute to Apache Usergrid
How to Contribute to Apache Usergrid
 
Introduction to Apache Cassandra
Introduction to Apache CassandraIntroduction to Apache Cassandra
Introduction to Apache Cassandra
 
Building Mobile Apps with Apache UserGrid, the Open Source Baas
Building Mobile Apps with Apache UserGrid, the Open Source BaasBuilding Mobile Apps with Apache UserGrid, the Open Source Baas
Building Mobile Apps with Apache UserGrid, the Open Source Baas
 
Usergrid Overview
Usergrid OverviewUsergrid Overview
Usergrid Overview
 
Open Source Mobile Backend on Cassandra
Open Source Mobile Backend on CassandraOpen Source Mobile Backend on Cassandra
Open Source Mobile Backend on Cassandra
 
Introduction to Cassandra Basics
Introduction to Cassandra BasicsIntroduction to Cassandra Basics
Introduction to Cassandra Basics
 
Indexing in Cassandra
Indexing in CassandraIndexing in Cassandra
Indexing in Cassandra
 
Introduction to Apache Cassandra
Introduction to Apache CassandraIntroduction to Apache Cassandra
Introduction to Apache Cassandra
 
Cassandra By Example: Data Modelling with CQL3
Cassandra By Example: Data Modelling with CQL3Cassandra By Example: Data Modelling with CQL3
Cassandra By Example: Data Modelling with CQL3
 
Advanced data modeling with apache cassandra
Advanced data modeling with apache cassandraAdvanced data modeling with apache cassandra
Advanced data modeling with apache cassandra
 
An Overview of Apache Cassandra
An Overview of Apache CassandraAn Overview of Apache Cassandra
An Overview of Apache Cassandra
 
Cassandra Data Modeling - Practical Considerations @ Netflix
Cassandra Data Modeling - Practical Considerations @ NetflixCassandra Data Modeling - Practical Considerations @ Netflix
Cassandra Data Modeling - Practical Considerations @ Netflix
 

Similaire à Moose Best Practices

Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
jhchabran
 
Advance Techniques In Php
Advance Techniques In PhpAdvance Techniques In Php
Advance Techniques In Php
Kumar S
 
Graph Databases
Graph DatabasesGraph Databases
Graph Databases
Josh Adell
 
R57shell
R57shellR57shell
R57shell
ady36
 
Reformas e auto-reformas da educação no Brasil
Reformas e auto-reformas da educação no BrasilReformas e auto-reformas da educação no Brasil
Reformas e auto-reformas da educação no Brasil
Iasmin Marinho
 

Similaire à Moose Best Practices (20)

Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
 
How else can you write the code in PHP?
How else can you write the code in PHP?How else can you write the code in PHP?
How else can you write the code in PHP?
 
Google Visualization API
Google  Visualization  APIGoogle  Visualization  API
Google Visualization API
 
Modern Perl
Modern PerlModern Perl
Modern Perl
 
Advance Techniques In Php
Advance Techniques In PhpAdvance Techniques In Php
Advance Techniques In Php
 
Drupal Lightning FAPI Jumpstart
Drupal Lightning FAPI JumpstartDrupal Lightning FAPI Jumpstart
Drupal Lightning FAPI Jumpstart
 
Drupal Development (Part 2)
Drupal Development (Part 2)Drupal Development (Part 2)
Drupal Development (Part 2)
 
Graph Databases
Graph DatabasesGraph Databases
Graph Databases
 
Jquery
JqueryJquery
Jquery
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)
 
Views notwithstanding
Views notwithstandingViews notwithstanding
Views notwithstanding
 
R57shell
R57shellR57shell
R57shell
 
Php functions
Php functionsPhp functions
Php functions
 
Rails 2010 Workshop
Rails 2010 WorkshopRails 2010 Workshop
Rails 2010 Workshop
 
There's more than one way to empty it
There's more than one way to empty itThere's more than one way to empty it
There's more than one way to empty it
 
Exploiting Php With Php
Exploiting Php With PhpExploiting Php With Php
Exploiting Php With Php
 
Reformas e auto-reformas da educação no Brasil
Reformas e auto-reformas da educação no BrasilReformas e auto-reformas da educação no Brasil
Reformas e auto-reformas da educação no Brasil
 
Daily notes
Daily notesDaily notes
Daily notes
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 
DBI
DBIDBI
DBI
 

Dernier

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Dernier (20)

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Moose Best Practices

  • 1. MooseBest Practices by Aran Deltac (CPAN: bluefeet)
  • 2. make_immutable() package MyClass; use Moose; … __PACKAGE__->meta->make_immutable; 1;
  • 3. default => $value has ‘is_clean’ => ( is => ‘ro’, isa => ‘Bool’, default => 1, );
  • 4. Many Possible States has ‘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 States has ‘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 => 1 has ‘ssn’ => ( is => ‘ro’, isa => ‘Str’, required => 1, );
  • 7. Many Possible States subtype ‘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_name has ‘dbh’ => ( is => ‘ro’, isa => ‘dbh’, builder => ‘_build_dbh’, ); sub _build_dbh { return DBI->connect(…); }
  • 9. lazy => $bool has ‘dbh’ => ( is => ‘ro’, isa => ‘dbh’, builder => ‘_build_dbh’, lazy => 1, ); sub _build_dbh { return DBI->connect(…); }
  • 10. lazy_build => 1 has ‘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 State has ‘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 State has ‘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_name has ‘color’ => ( is => ‘ro’, isa => ‘Str’, write => ‘_color’, );
  • 15. init_arg => undef has ‘result’ => ( is => ‘ro’, isa => ‘HashRef’, writer => ‘_set_result’, init_arg => undef, );