Moo, the universe and everything… 
Cincinnati.pm 
Henry Van Styn 
<vanstyn@cpan.org> 
Wednesday, 22 October, 2014
Agenda 
• Overview of Moo 
• Differences between Moo and Moose 
• Moosey concepts 
• Within the larger context of Perl OO 
• Some useful patterns (and anti-patterns)
Moo is… 
• Light version of Moose 
• Object system for Perl written in Perl 
• Sugar 
• Result of lessons learned from Moose, Mouse 
MooseX, etc 
• Current state-of-the-art*
Differences from Moose 
• Smaller set of features (~2/3 of Moose) 
• “Less is more” 
• Faster startup, more straightforward APIs 
• Types - simple CodeRef interface* 
• also use CodeRefs for default, coerce, etc…*
Moosey concepts 
• Declarative OO 
• MOP 
• Provides additional, higher-level object concepts: 
attributes, roles, method modifiers 
• Perl is still perl: classes (packages), methods, 
inheritance, blessed references
Peeling back the onion -- Perl OO… 
• “Bag of functions” 
• classes == packages + inheritance 
• blessed refs: attach data to a class == objects 
• runtime introspection and modification…
class-based OO 
• Push package names on @ISA (inheritance) 
• Early sugar: use base, use parent 
Some::Class::method(); 
# ^^ calls method() in Some::Class 
Some::Class->method(); 
# ^^ calls method() in Some::Class 
# - or the next parent of Some::Class * 
# - with ‘Some::Class’ passed as the first argument
object-based OO 
my $obj = bless($ref, ’Some::Class’); 
$obj->method(); 
# ^^ calls method() in Some::Class 
# - or the next parent of Some::Class * 
# - with $obj passed as the first argument 
# - $obj is like a merger of $ref and ‘Some::Class’
Multiple Inheritance 
• Push multiple package names on @ISA 
• Which is the “next parent” ? 
• Flattened (linearized) according to ‘MRO’ 
use mro ‘c3’; 
# ^^ Don’t be stupid 
# next::method() is now a thing 
# MRO::Compat before Perl 5.10 
# perldoc mro
constructors 
• Perl doesn’t have or need the concept 
• Do it however you want 
sub new { 
my $class = shift; 
bless({},$class); 
}
object attributes, etc 
• Perl doesn’t have or need the concept 
• Do it however you want 
sub thing { 
my ($self,$new_val) = @_; 
$self->{thing} = $new_val if ($new_val); 
$self->{thing} 
}
What Moo does 
• Sugar to write classes following a specific pattern 
• Designed to be instantiated into objects (via new) 
• Subclass using ‘extends’ 
• Declare attributes with ‘has’
How it does it 
• Uses introspection to modify the package 
• ‘extends’ modifies @ISA like base and parent 
• ‘has’ adds/changes methods
Roles (‘with’) 
• Also uses introspection to add/modify methods 
• Accomplish MI like designs w/o actually using Perl’s 
“MI” 
• No need to worry about MRO 
• You can still do MI if you really want to: 
pass multiple class names to ‘extends’ 
• But don’t - just use Roles
Method-Modifiers 
• Access MI concepts but also w/o worry about MRO 
around ‘foo’ => sub { 
my ($orig,$self,@args) = @_; 
$self->$orig(@args) 
}; 
sub foo { 
my $self = shift; 
$self->next::method(@_) 
}
Useful patterns 
• Chaining (like DBIC ResultSets) 
• Methods written to work on class or object 
• Using class names as params 
• Type::Tiny for types 
• Coercions
When to use Moo 
• When writing object classes 
• When extending anything 
• Extend Moose classes
Anti-Patterns 
• Getting too carried away 
• Doing things for dogma rather than benefit 
• Forgetting you are still writing Perl (and that it is 
wonderful) 
• There really is more than one way to do it
Code Examples 
https://github.com/vanstyn/LJ-moose-article-examples 
https://github.com/vanstyn/DBIx-Class-Schema-Diff 
https://github.com/vanstyn/Hash-Layout

Moo the universe and everything

  • 1.
    Moo, the universeand everything… Cincinnati.pm Henry Van Styn <vanstyn@cpan.org> Wednesday, 22 October, 2014
  • 2.
    Agenda • Overviewof Moo • Differences between Moo and Moose • Moosey concepts • Within the larger context of Perl OO • Some useful patterns (and anti-patterns)
  • 3.
    Moo is… •Light version of Moose • Object system for Perl written in Perl • Sugar • Result of lessons learned from Moose, Mouse MooseX, etc • Current state-of-the-art*
  • 4.
    Differences from Moose • Smaller set of features (~2/3 of Moose) • “Less is more” • Faster startup, more straightforward APIs • Types - simple CodeRef interface* • also use CodeRefs for default, coerce, etc…*
  • 5.
    Moosey concepts •Declarative OO • MOP • Provides additional, higher-level object concepts: attributes, roles, method modifiers • Perl is still perl: classes (packages), methods, inheritance, blessed references
  • 6.
    Peeling back theonion -- Perl OO… • “Bag of functions” • classes == packages + inheritance • blessed refs: attach data to a class == objects • runtime introspection and modification…
  • 7.
    class-based OO •Push package names on @ISA (inheritance) • Early sugar: use base, use parent Some::Class::method(); # ^^ calls method() in Some::Class Some::Class->method(); # ^^ calls method() in Some::Class # - or the next parent of Some::Class * # - with ‘Some::Class’ passed as the first argument
  • 8.
    object-based OO my$obj = bless($ref, ’Some::Class’); $obj->method(); # ^^ calls method() in Some::Class # - or the next parent of Some::Class * # - with $obj passed as the first argument # - $obj is like a merger of $ref and ‘Some::Class’
  • 9.
    Multiple Inheritance •Push multiple package names on @ISA • Which is the “next parent” ? • Flattened (linearized) according to ‘MRO’ use mro ‘c3’; # ^^ Don’t be stupid # next::method() is now a thing # MRO::Compat before Perl 5.10 # perldoc mro
  • 10.
    constructors • Perldoesn’t have or need the concept • Do it however you want sub new { my $class = shift; bless({},$class); }
  • 11.
    object attributes, etc • Perl doesn’t have or need the concept • Do it however you want sub thing { my ($self,$new_val) = @_; $self->{thing} = $new_val if ($new_val); $self->{thing} }
  • 12.
    What Moo does • Sugar to write classes following a specific pattern • Designed to be instantiated into objects (via new) • Subclass using ‘extends’ • Declare attributes with ‘has’
  • 13.
    How it doesit • Uses introspection to modify the package • ‘extends’ modifies @ISA like base and parent • ‘has’ adds/changes methods
  • 14.
    Roles (‘with’) •Also uses introspection to add/modify methods • Accomplish MI like designs w/o actually using Perl’s “MI” • No need to worry about MRO • You can still do MI if you really want to: pass multiple class names to ‘extends’ • But don’t - just use Roles
  • 15.
    Method-Modifiers • AccessMI concepts but also w/o worry about MRO around ‘foo’ => sub { my ($orig,$self,@args) = @_; $self->$orig(@args) }; sub foo { my $self = shift; $self->next::method(@_) }
  • 16.
    Useful patterns •Chaining (like DBIC ResultSets) • Methods written to work on class or object • Using class names as params • Type::Tiny for types • Coercions
  • 17.
    When to useMoo • When writing object classes • When extending anything • Extend Moose classes
  • 18.
    Anti-Patterns • Gettingtoo carried away • Doing things for dogma rather than benefit • Forgetting you are still writing Perl (and that it is wonderful) • There really is more than one way to do it
  • 19.
    Code Examples https://github.com/vanstyn/LJ-moose-article-examples https://github.com/vanstyn/DBIx-Class-Schema-Diff https://github.com/vanstyn/Hash-Layout