SlideShare uma empresa Scribd logo
1 de 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, );

Mais conteúdo relacionado

Mais procurados

Moose workshop
Moose workshopMoose workshop
Moose workshopYnon Perek
 
WordPress Cuztom Helper
WordPress Cuztom HelperWordPress Cuztom Helper
WordPress Cuztom Helperslicejack
 
DBIx::Class beginners
DBIx::Class beginnersDBIx::Class beginners
DBIx::Class beginnersleo lapworth
 
DBIx::Class introduction - 2010
DBIx::Class introduction - 2010DBIx::Class introduction - 2010
DBIx::Class introduction - 2010leo lapworth
 
(Parameterized) Roles
(Parameterized) Roles(Parameterized) Roles
(Parameterized) Rolessartak
 
Doctrine 2
Doctrine 2Doctrine 2
Doctrine 2zfconfua
 
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) ThingsMichael Pirnat
 
Introduction to Perl Best Practices
Introduction to Perl Best PracticesIntroduction to Perl Best Practices
Introduction to Perl Best PracticesJosé Castro
 
Hack in the Box Keynote 2006
Hack in the Box Keynote 2006Hack in the Box Keynote 2006
Hack in the Box Keynote 2006Mark Curphey
 
Rapid web development, the right way.
Rapid web development, the right way.Rapid web development, the right way.
Rapid web development, the right way.nubela
 
Chapter 2: What's your type?
Chapter 2: What's your type?Chapter 2: What's your type?
Chapter 2: What's your type?Seth McLaughlin
 
Bioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeBioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeProf. Wim Van Criekinge
 
Bash Learning By Examples
Bash Learning By ExamplesBash Learning By Examples
Bash Learning By ExamplesArun Bagul
 
Ruby Programming Language
Ruby Programming LanguageRuby Programming Language
Ruby Programming LanguageDuda Dornelles
 

Mais procurados (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
 

Destaque

DBIC 2 - Resultsets
DBIC 2 - ResultsetsDBIC 2 - Resultsets
DBIC 2 - ResultsetsAran Deltac
 
Tools of the CPAN Ninja
Tools of the CPAN NinjaTools of the CPAN Ninja
Tools of the CPAN NinjaAran Deltac
 
How to Contribute to Apache Usergrid
How to Contribute to Apache UsergridHow to Contribute to Apache Usergrid
How to Contribute to Apache UsergridDavid M. Johnson
 
Introduction to Apache Cassandra
Introduction to Apache CassandraIntroduction to Apache Cassandra
Introduction to Apache CassandraAran Deltac
 
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 BaasAll Things Open
 
Usergrid Overview
Usergrid OverviewUsergrid Overview
Usergrid Overviewusergrid
 
Open Source Mobile Backend on Cassandra
Open Source Mobile Backend on CassandraOpen Source Mobile Backend on Cassandra
Open Source Mobile Backend on CassandraEd Anuff
 
Introduction to Cassandra Basics
Introduction to Cassandra BasicsIntroduction to Cassandra Basics
Introduction to Cassandra Basicsnickmbailey
 
Indexing in Cassandra
Indexing in CassandraIndexing in Cassandra
Indexing in CassandraEd Anuff
 
Introduction to Apache Cassandra
Introduction to Apache CassandraIntroduction to Apache Cassandra
Introduction to Apache CassandraRobert Stupp
 
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 CQL3Eric Evans
 
Advanced data modeling with apache cassandra
Advanced data modeling with apache cassandraAdvanced data modeling with apache cassandra
Advanced data modeling with apache cassandraPatrick McFadin
 
An Overview of Apache Cassandra
An Overview of Apache CassandraAn Overview of Apache Cassandra
An Overview of Apache CassandraDataStax
 
Cassandra Data Modeling - Practical Considerations @ Netflix
Cassandra Data Modeling - Practical Considerations @ NetflixCassandra Data Modeling - Practical Considerations @ Netflix
Cassandra Data Modeling - Practical Considerations @ Netflixnkorla1share
 

Destaque (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
 

Semelhante a Moose Best Practices

Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB jhchabran
 
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?Maksym Hopei
 
Google Visualization API
Google  Visualization  APIGoogle  Visualization  API
Google Visualization APIJason Young
 
Advance Techniques In Php
Advance Techniques In PhpAdvance Techniques In Php
Advance Techniques In PhpKumar S
 
Drupal Lightning FAPI Jumpstart
Drupal Lightning FAPI JumpstartDrupal Lightning FAPI Jumpstart
Drupal Lightning FAPI Jumpstartguestfd47e4c7
 
Drupal Development (Part 2)
Drupal Development (Part 2)Drupal Development (Part 2)
Drupal Development (Part 2)Jeff Eaton
 
Graph Databases
Graph DatabasesGraph Databases
Graph DatabasesJosh Adell
 
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)Michael Schwern
 
R57shell
R57shellR57shell
R57shellady36
 
Rails 2010 Workshop
Rails 2010 WorkshopRails 2010 Workshop
Rails 2010 Workshopdtsadok
 
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 itAndrew Shitov
 
Exploiting Php With Php
Exploiting Php With PhpExploiting Php With Php
Exploiting Php With PhpJeremy Coates
 
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 BrasilIasmin Marinho
 
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 tomorrowPete McFarlane
 

Semelhante a 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
 

Último

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
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...Miguel Araújo
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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...Martijn de Jong
 

Último (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
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...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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...
 

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, );