SlideShare a Scribd company logo
1 of 43
use Moose;
Finding Object Oriented Enlightenment
Images Courtesy of
   Shutterstock

   http://www.shutterstock.com/
Shutterstock
Developer Ethos
Get feedback as early as possible, and work together
Prefer encapsulated, loosely coupled
   systems for core functionality
Choose the smallest implementation that doesn’t
         suck provides a way forward
Make your code readable,
understandable, maintainable
• Get feedback as early as possible, and work
  together

• Prefer encapsulated, loosely coupled
  systems for core functionality

• Choose the smallest implementation that
  provides a way forward

• Make your code readable, understandable,
  and maintainable
...the object-oriented approach
 encourages the programmer to place
       data where it is not directly
       accessible by the rest of the
 program. Instead, the data is accessed
  by calling specially written functions,
       commonly called methods...

http://en.wikipedia.org/wiki/Object-oriented_programming
A Person Walks and Talks
start_walking               stop_walking




                Person




            speak($something)
Prefer encapsulated, loosely coupled
systems for core functionality

Choose the smallest implementation that
provides a way forward

Make your code readable, understandable,
and maintainable
Ash Nazg Durbatulûk! Ash Nazg Gimbatul!
 Ash Nazg Gimbatul! Ash Nazg Gimbatul!
"I will take the Ring to
 Mordor. Though — I
do not know the way."
    -- Frodo, LoTR
Moose and Perl
Moose is a postmodern object
system for Perl 5 that takes the tedium
 out of writing object-oriented Perl. It
borrows all the best features from Perl
  6, CLOS (Lisp), Smalltalk, Java, BETA,
   OCaml, Ruby and more, while still
     keeping true to its Perl 5 roots.

         http://moose.iinteractive.com/
package Point;

use Moose;
 
has 'x' => (isa => 'Int', is => 'rw', required => 1);
has 'y' => (isa => 'Int', is => 'rw', required => 1);
 
sub clear {
  my $self = shift;
  $self->x(0);
  $self->y(0);
}
Building Blocks

•   Inheritance

•   Roles

•   Delegation
Inheritance
package Point;
use Moose;
 
has 'x' => (isa => 'Int', is => 'rw', required => 1);
has 'y' => (isa => 'Int', is => 'rw', required => 1);
 
sub clear {
  my $self = shift;
  $self->x(0);
  $self->y(0);
}

package Point3D;
use Moose;
 
extends 'Point';
 
has 'z' => (isa => 'Int', is => 'rw', required => 1);
 
after 'clear' => sub {
    my $self = shift;
    $self->z(0);
};
Roles
package Eq;
use Moose::Role;
requires 'equal';

my $defensive_coderef = sub { ... };
 
sub no_equal {
    my ($self, $other) = @_;
    !$self->equal($other);
}
 
package Currency;
use Moose;
with 'Eq';
 
sub equal {
    my ($self, $other) = @_;
    $self->as_float == $other->as_float;
}
Delegation
package MyObject;

use Moose;
use MyLogger;

has ‘logger’ => (
   is => ‘bare’,
   lazy_build => 1,
   handles => [qw/err warn/],
);

sub _build_logger { MyLogger->new }

sub something_worth_logging {
  my ($self) = @_;
  $self->err(“Hey, there’s a problem!!!”);
}
package MyObject;
use Moose;
use MooseX::Types::LoadableClass 'LoadableClass';

has logger_class => (
  is => 'ro',
  default => 'MyLogger',
  isa => LoadableClass,
  coerce => 1);

has logger_args => (
   is=>’ro’, isa=>‘HashRef’, default=> sub { +{} }
);

has ‘logger’ => (
  is => ‘bare’,
  lazy_build => 1,
  handles => [qw/err warn/]);

sub _build_logger {
  my $self = shift;
  $self->logger_class($self->logger_args)
    ->new
}
package MyObject;
use Moose;
 
with 'MooseX::Role::BuildInstanceOf' => {
   target => 'Logger'
};

has ‘+logger’ => (handles => [qw/err warn/]);
Examples
Test::DBIx::Class

• Automatically deploy temporary databases
  based on exist DBIx::Class schema
• Pluggable database targets (Sqlite, MySQL,
  PostgreSQL)
Approach

• Create a Base Class with default
  functionality
• Use Roles to allow ‘plugging in’ various
  target temporary database types
DBIx::Class::Migration

• Make it easy to create deploys and
  upgrades for database schemas
• Be flexible but have sane, community
  accepted standards
Approach

• Create a Base class with lots of delegate
  placeholders.
• Create defaults classes that provide
  required functionality
GeneralAssembly::Guestbook


 • A Test Application to Demonstrate Perl to
   inspired learners
 • Code is probably overkill for the actual
   problem
My Learnings
• Delegation seems the most normally useful
  approach to creating easily testable building
  blocks.
• Roles are great for enforcing contracts and
  for enhancing objects with cross cutting
  functionality.
• (Combine the two)++
Summary

• There is no necessary tension between
  writing the least code and writing good
  code
• Good design lets you evolve over time
  without nasty hacks
Moose

More Related Content

What's hot

CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
None
 
Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)
Kris Wallsmith
 
Amazon Cloud Services and Zend Framework
Amazon Cloud Services and Zend FrameworkAmazon Cloud Services and Zend Framework
Amazon Cloud Services and Zend Framework
Shahar Evron
 

What's hot (19)

Introduction to Moose
Introduction to MooseIntroduction to Moose
Introduction to Moose
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Coffee Script
Coffee ScriptCoffee Script
Coffee Script
 
Introduction to CoffeeScript
Introduction to CoffeeScriptIntroduction to CoffeeScript
Introduction to CoffeeScript
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: Prototypes
 
Scaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
Scaling Symfony2 apps with RabbitMQ - Symfony UK MeetupScaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
Scaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
 
Power of Puppet 4
Power of Puppet 4Power of Puppet 4
Power of Puppet 4
 
"The worst code I ever wrote"
"The worst code I ever wrote""The worst code I ever wrote"
"The worst code I ever wrote"
 
Replacing "exec" with a type and provider: Return manifests to a declarative ...
Replacing "exec" with a type and provider: Return manifests to a declarative ...Replacing "exec" with a type and provider: Return manifests to a declarative ...
Replacing "exec" with a type and provider: Return manifests to a declarative ...
 
Your JavaScript Library
Your JavaScript LibraryYour JavaScript Library
Your JavaScript Library
 
Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)
 
Puppet modules for Fun and Profit
Puppet modules for Fun and ProfitPuppet modules for Fun and Profit
Puppet modules for Fun and Profit
 
5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScript
 
Php on the Web and Desktop
Php on the Web and DesktopPhp on the Web and Desktop
Php on the Web and Desktop
 
You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)
 
Non-Relational Databases
Non-Relational DatabasesNon-Relational Databases
Non-Relational Databases
 
Amazon Cloud Services and Zend Framework
Amazon Cloud Services and Zend FrameworkAmazon Cloud Services and Zend Framework
Amazon Cloud Services and Zend Framework
 
Troubleshooting Puppet
Troubleshooting PuppetTroubleshooting Puppet
Troubleshooting Puppet
 
Koajs as an alternative to Express - OdessaJs'16
Koajs as an alternative to Express - OdessaJs'16Koajs as an alternative to Express - OdessaJs'16
Koajs as an alternative to Express - OdessaJs'16
 

Similar to Moose

Node.js introduction
Node.js introductionNode.js introduction
Node.js introduction
Prasoon Kumar
 

Similar to Moose (20)

Python and Oracle : allies for best of data management
Python and Oracle : allies for best of data managementPython and Oracle : allies for best of data management
Python and Oracle : allies for best of data management
 
55j7
55j755j7
55j7
 
Advancing JavaScript with Libraries (Yahoo Tech Talk)
Advancing JavaScript with Libraries (Yahoo Tech Talk)Advancing JavaScript with Libraries (Yahoo Tech Talk)
Advancing JavaScript with Libraries (Yahoo Tech Talk)
 
DjangoCon 2010 Scaling Disqus
DjangoCon 2010 Scaling DisqusDjangoCon 2010 Scaling Disqus
DjangoCon 2010 Scaling Disqus
 
The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at Twitter
 
Apache Superset at Airbnb
Apache Superset at AirbnbApache Superset at Airbnb
Apache Superset at Airbnb
 
Java Secure Coding Practices
Java Secure Coding PracticesJava Secure Coding Practices
Java Secure Coding Practices
 
DevOps for database
DevOps for databaseDevOps for database
DevOps for database
 
Django at Scale
Django at ScaleDjango at Scale
Django at Scale
 
Pwning with powershell
Pwning with powershellPwning with powershell
Pwning with powershell
 
Objective-C Is Not Java
Objective-C Is Not JavaObjective-C Is Not Java
Objective-C Is Not Java
 
Bioinformatica p6-bioperl
Bioinformatica p6-bioperlBioinformatica p6-bioperl
Bioinformatica p6-bioperl
 
Node.js introduction
Node.js introductionNode.js introduction
Node.js introduction
 
Persistences
PersistencesPersistences
Persistences
 
Laravel ppt
Laravel pptLaravel ppt
Laravel ppt
 
Bioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeBioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekinge
 
Learn you some Ansible for great good!
Learn you some Ansible for great good!Learn you some Ansible for great good!
Learn you some Ansible for great good!
 
44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...
44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...
44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...
 
55 New Features in Java 7
55 New Features in Java 755 New Features in Java 7
55 New Features in Java 7
 
Rails Tips and Best Practices
Rails Tips and Best PracticesRails Tips and Best Practices
Rails Tips and Best Practices
 

Recently uploaded

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
panagenda
 

Recently uploaded (20)

Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
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
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
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
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
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...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

Moose

  • 1. use Moose; Finding Object Oriented Enlightenment
  • 2. Images Courtesy of Shutterstock http://www.shutterstock.com/
  • 3.
  • 4.
  • 5.
  • 6.
  • 8. Get feedback as early as possible, and work together
  • 9. Prefer encapsulated, loosely coupled systems for core functionality
  • 10. Choose the smallest implementation that doesn’t suck provides a way forward
  • 11. Make your code readable, understandable, maintainable
  • 12. • Get feedback as early as possible, and work together • Prefer encapsulated, loosely coupled systems for core functionality • Choose the smallest implementation that provides a way forward • Make your code readable, understandable, and maintainable
  • 13. ...the object-oriented approach encourages the programmer to place data where it is not directly accessible by the rest of the program. Instead, the data is accessed by calling specially written functions, commonly called methods... http://en.wikipedia.org/wiki/Object-oriented_programming
  • 14. A Person Walks and Talks
  • 15. start_walking stop_walking Person speak($something)
  • 16. Prefer encapsulated, loosely coupled systems for core functionality Choose the smallest implementation that provides a way forward Make your code readable, understandable, and maintainable
  • 17.
  • 18. Ash Nazg Durbatulûk! Ash Nazg Gimbatul! Ash Nazg Gimbatul! Ash Nazg Gimbatul!
  • 19. "I will take the Ring to Mordor. Though — I do not know the way." -- Frodo, LoTR
  • 20.
  • 22. Moose is a postmodern object system for Perl 5 that takes the tedium out of writing object-oriented Perl. It borrows all the best features from Perl 6, CLOS (Lisp), Smalltalk, Java, BETA, OCaml, Ruby and more, while still keeping true to its Perl 5 roots. http://moose.iinteractive.com/
  • 23. package Point; use Moose;   has 'x' => (isa => 'Int', is => 'rw', required => 1); has 'y' => (isa => 'Int', is => 'rw', required => 1);   sub clear { my $self = shift;   $self->x(0);   $self->y(0); }
  • 24.
  • 25. Building Blocks • Inheritance • Roles • Delegation
  • 27. package Point; use Moose;   has 'x' => (isa => 'Int', is => 'rw', required => 1); has 'y' => (isa => 'Int', is => 'rw', required => 1);   sub clear { my $self = shift;   $self->x(0);   $self->y(0); } package Point3D; use Moose;   extends 'Point';   has 'z' => (isa => 'Int', is => 'rw', required => 1);   after 'clear' => sub {     my $self = shift;     $self->z(0); };
  • 28. Roles
  • 29. package Eq; use Moose::Role; requires 'equal'; my $defensive_coderef = sub { ... };   sub no_equal {     my ($self, $other) = @_;     !$self->equal($other); }   package Currency; use Moose; with 'Eq';   sub equal {     my ($self, $other) = @_;     $self->as_float == $other->as_float; }
  • 31. package MyObject; use Moose; use MyLogger; has ‘logger’ => ( is => ‘bare’, lazy_build => 1, handles => [qw/err warn/], ); sub _build_logger { MyLogger->new } sub something_worth_logging { my ($self) = @_; $self->err(“Hey, there’s a problem!!!”); }
  • 32. package MyObject; use Moose; use MooseX::Types::LoadableClass 'LoadableClass'; has logger_class => (   is => 'ro',   default => 'MyLogger',   isa => LoadableClass,   coerce => 1); has logger_args => ( is=>’ro’, isa=>‘HashRef’, default=> sub { +{} } ); has ‘logger’ => ( is => ‘bare’, lazy_build => 1, handles => [qw/err warn/]); sub _build_logger { my $self = shift; $self->logger_class($self->logger_args) ->new }
  • 33. package MyObject; use Moose;   with 'MooseX::Role::BuildInstanceOf' => { target => 'Logger' }; has ‘+logger’ => (handles => [qw/err warn/]);
  • 35. Test::DBIx::Class • Automatically deploy temporary databases based on exist DBIx::Class schema • Pluggable database targets (Sqlite, MySQL, PostgreSQL)
  • 36. Approach • Create a Base Class with default functionality • Use Roles to allow ‘plugging in’ various target temporary database types
  • 37. DBIx::Class::Migration • Make it easy to create deploys and upgrades for database schemas • Be flexible but have sane, community accepted standards
  • 38. Approach • Create a Base class with lots of delegate placeholders. • Create defaults classes that provide required functionality
  • 39. GeneralAssembly::Guestbook • A Test Application to Demonstrate Perl to inspired learners • Code is probably overkill for the actual problem
  • 40.
  • 41. My Learnings • Delegation seems the most normally useful approach to creating easily testable building blocks. • Roles are great for enforcing contracts and for enhancing objects with cross cutting functionality. • (Combine the two)++
  • 42. Summary • There is no necessary tension between writing the least code and writing good code • Good design lets you evolve over time without nasty hacks

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. great ideas are simple answers to common problems\n\n
  5. maybe not so simple in the details!\n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. great ideas are simple answers to common problems\n\n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n