Moose - YAPC::NA 2012
Upcoming SlideShare
Loading in...5

Moose - YAPC::NA 2012



This is the Moose talk I gave at YAPC::NA 2012. ...

This is the Moose talk I gave at YAPC::NA 2012.

It included a practical example of a Moose objects code, a simple app called Comican. The code is not available online. If you want it, just email me (sawyer ATT cpan DOTT org).



Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds


Upload Details

Uploaded via as OpenOffice

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.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Moose - YAPC::NA 2012 Moose - YAPC::NA 2012 Presentation Transcript

  • MooseSawyer X
  • Sawyer„The Dancer guy!“Projects: Dancer, Module::Starter, MetaCPAN::API, MooseX::Role::Loggable, etc.
  • MooseObject systemMetaclass-basedAdvancedSophisticatedExtensibleProduction-ready
  • … but why Moose?package Person; package User;use strict; use Email::Valid;use warnings; use Moose; use Moose::Util::TypeConstraints;use Carp qw( confess );use DateTime; extends Person;use DateTime::Format::Natural; subtype Email     => as Strsub new {     => where { Email::Valid­>address($_) }    my $class = shift;     => message { "$_ is not a valid email     my %p = ref $_[0] ? %{ $_[0] } : @_; address" };    exists $p{name} has email_address => (        or confess name is a required attribute;     is       => rw,    $class­>_validate_name( $p{name} );     isa      => Email,     required => 1,    exists $p{birth_date} );        or confess birth_date is a required attribute;    $p{birth_date} = $class­>_coerce_birth_date( $p{birth_date} );    $class­>_validate_birth_date( $p{birth_date} );    $p{shirt_size} = l        unless exists $p{shirt_size}:    $class­>_validate_shirt_size( $p{shirt_size} );    return bless %p, $class;}sub _validate_name {    shift;    my $name = shift;    local $Carp::CarpLevel = $Carp::CarpLevel + 1;    defined $name        or confess name must be a string;}
  • Get it?
  • Writing objects in basic Perl 5Create a reference (usually to a hash)Connect it to a package (using „bless“)Provide subroutines that access the hash keysError check the hell out of it
  • Writing objects in basic Perl 5, e.g.package Dog;use strict; use warnings;sub new { my $class = shift; my $self = bless {}, $class; return $self;}1;
  • IssuesDefining the same new() concept every timeNo parameters for new() yetWill have to do all error-checking manually
  • Moosepackage Dog;use Moose;1;
  • You getuse strict;use warnings;new() methodTo hell with ponies, you get a moose!(roll picture of a moose beating a pony in soccer)
  • Full affordance accessors (attributes)has name => ( is => rw );„ro“ is available for read-only attributesYou can manually change setter/getter via writer/readerAttributes can have defaultsAttributes can have type constraintsAttributes can have traitsAttributes can be lazy, have builders, clearers, predicates...
  • Type constraintshas name => ( is => ro, isa => Str );Str, Int, Bool, ArrayRef, HashRef, CodeRef, Regex, ClassesCombine: ArrayRef|HashRef, ArrayRef[Str]Derivatives (Int is a Num)Create your own using subtype, available at Moose::Util::TypeConstraints
  • MethodsSame as beforesub run { my $self = shift; say qq{I make it a habit only to run when being chased!};}
  • Inheritance easy as...package Punkuse Moose;extends Person;Multiple inheritance also possible (extends accepts array)package Child;use Moose;extends qw/Father Mother/;
  • Roles are even betterpackage Punk;use Moose;with qw/Tattoos Piercings Squatter/;Roles are things you do, instead of things you are.
  • More hooks than a coat rack!package User::WinterAware;use Moose;extends User;before leaving => sub { my $self = shift; $self->cold and $self->take_jacket;};
  • More hooks than a coat rack!package User::Secure;use Moose;extends User;around login => sub { my $orig = shift; my $self = shift; $self->security_check and $self->$orig(@_);};
  • More hooks than a coat rack!BeforeAfterAroundInnerAugment
  • Back to attributes...has set => ( is => rw, isa => Set::Object, default => sub { Set::Object->new }, required => 1, lazy => 1, predicate => has_set, clearer => clear_set,
  • Attribute options: defaultdefault => kitteh # stringdefault => 3 # numberdefault => sub { {} } # HashRefdefault => sub { [] } # ArrayRefdefault => sub { Object->new } # an Object (if you need a more elaborate sub, use builder)
  • Attribute options: requiredrequired => 1 # requiredrequired => 0 # not required
  • Attribute options: lazylazy => 1 # make it lazyClass will not create the slot for this attribute unless itabsolutely has to, defined by whether it is accessed at all.No access? No penalty!Lazy == good
  • Attribute options: builderbuilder => build_it # subroutine namesub build_it { my $self = shift; # not a problem! return Some::Object->new( $self->more_opts );}
  • Attribute options: clearerclearer => clear_it # subroutine name# you dont need to create the subroutinesub time_machine { my $self = shift; $self->clear_it; # it never happened :)}
  • Attribute options: predicatepredicate => has_it # subroutine name# you dont need to create the subroutinesub try_to_do_it { my $self = shift; $self->has_it && $self->do_it();}
  • Attribute options: lazy_buildlazy_build => 1 # <3# the same as:lazy => 1,builder => _build_it, # privateclearer => clear_it,predicate => has_it,
  • Example: ComicanA hub for various comics stripsAllow you to fetch comic stripsStandard uniformed interface to add more comicsWell be using: Roles Lazy attributes Overriding attributes options Attribute predicates
  • Comican comic modules Comican::Comic::Dilber t interface role Comican::Comic::PennyArcadeComican::Comic::xkcd Comican:: Role::Comic main module user
  • Comican: is the users interfaceIts a hub for Comican::Comic::* modulesThey are objects that fetch specific comic stripsThey have a common interfaceDefined by Comican::Role::Comic
  • /^M(?:o(?:o|[ou]se)?)?$/Moose: standartMouse: subset, uses XS, faster(Any::Moose: use Mouse unless Moose already loaded)Moo: Pure-Perl, subset, blazing fastMo: As little as possible (incl. character count)M: Really as little as possibleHummus (HuMoose): Incompatible chickpeas paste
  • Thank you.