• Save
Moose
Upcoming SlideShare
Loading in...5
×
 

Moose

on

  • 1,179 views

Slides of the presentation I gave for Perl NYC Meetup 23 April 2012 (http://www.meetup.com/The-New-York-Perl-Meetup-Group/events/59990662/)

Slides of the presentation I gave for Perl NYC Meetup 23 April 2012 (http://www.meetup.com/The-New-York-Perl-Meetup-Group/events/59990662/)

Statistics

Views

Total Views
1,179
Views on SlideShare
1,179
Embed Views
0

Actions

Likes
1
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • great ideas are simple answers to common problems\n\n
  • maybe not so simple in the details!\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • great ideas are simple answers to common problems\n\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Moose Moose Presentation Transcript

  • use Moose;Finding Object Oriented Enlightenment
  • Images Courtesy of Shutterstock http://www.shutterstock.com/
  • ShutterstockDeveloper 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 coupledsystems for core functionalityChoose the smallest implementation thatprovides a way forwardMake 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 — Ido not know the way." -- Frodo, LoTR
  • Moose and Perl
  • Moose is a postmodern objectsystem for Perl 5 that takes the tedium out of writing object-oriented Perl. Itborrows 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