Moose talk at FOSDEM 2011 (Perl devroom)
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Moose talk at FOSDEM 2011 (Perl devroom)

on

  • 7,075 views

This are the slides of the talk I gave at FOSDEM 2011 about Moose at the Perl devroom.

This are the slides of the talk I gave at FOSDEM 2011 about Moose at the Perl devroom.

It's an updated version of the slides I've previous uploaded.

Statistics

Views

Total Views
7,075
Views on SlideShare
7,073
Embed Views
2

Actions

Likes
3
Downloads
85
Comments
0

2 Embeds 2

http://orhowilearnedtolovetheperl.com 1
http://paper.li 1

Accessibility

Categories

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.

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

Moose talk at FOSDEM 2011 (Perl devroom) Presentation Transcript

  • 1. Moose A postmodern metaclass-based object system for Perl 5
  • 2. Moose Sawyer X * blogs.perl.org/users/sawyer_x * search.cpan.org/~xsawyerx/ * search.metacpan.org/#/author/XSAWYERX * github.com/xsawyerx
  • 3. Objects in Perl 5 (a short intro)
    • A blessed hashref
    • 4. Manual new() method
    • 5. No real attributes
    bless {}, __PACKAGE__; sub new { my ( $class, @args ) = @_; my $self = { @args }; # params bless $self, $class; return $self; } sub name { my ( $self, $name ) = @_; $name and $self->{'name'} = $name; return $self->{'name'}; }
  • 6. Why would you want to use Moose? Moose package Person; use strict; use warnings; use Carp qw( confess ); use DateTime; use DateTime::Format::Natural; sub new { my $class = shift; my %p = ref $_[0] ? %{ $_[0] } : @_; exists $p{name} or confess 'name is a required attribute'; $class->_validate_name( $p{name} ); 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'; } Plain old Perl 5 package User; use Email::Valid; use Moose; use Moose::Util::TypeConstraints; extends 'Person'; subtype 'Email' => as 'Str' => where { Email::Valid->address($_) } => message { "$_ is not a valid email address" }; has email_address => ( is => 'rw', isa => 'Email', required => 1, );
  • 7. Get it?
  • 8. Defining an object in Moose
    • use strict
    • 9. use warnings
    • 10. An object!
    • 11. new() method
    • 12. A pon-.. err.. a moose!
    package User; use Moose; 1; You get:
  • 13. Full-Affordance accessors
    • ' ro ' is also available for read-only attributes!
    • 14. You can set the getter or setter manually via reader / writer
    • 15. I'll show more attribute options later on
    has name => ( is => 'rw', );
  • 16. An attribute with type constraint
    • Lots of types: Str , Int , ArrayRef , HashRef , CodeRef , Regexp
    • 17. You can combine: ArrayRef[Str] , HashRef[ArrayRef[Int]]
    • 18. They have inheritance: Int is a Num
    • 19. Roll your own using subtype
    package User; use Moose; has name => ( is => 'rw', isa => 'Str', ); 1;
  • 20. Methods are the same as before sub method { my $self = shift; ... $self->more(); }
  • 21. Inheritance is as easy as...
    • Multiple inheritance is also possible, extends accepts an array
    package Punk; use Moose; extends 'Person'; 1; package Child; use Moose; extends qw/ Father Mother /; 1;
  • 22. Roles are even easier!
    • Multiple roles are recommend! with accepts an array too
    package Punk; use Moose; extends 'Person'; with 'Piercings'; 1; package Punk; use Moose; extends 'Person'; with qw/ Piercings Tattoos /; 1;
  • 23. 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; }; 1;
    • before
    • 24. after
  • 25. 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(@_); }; 1;
    • before
    • 26. after
    • 27. around
    • 28. inner
    • 29. augment
  • 30. Back to attributes options... has set => ( is => 'rw', isa => 'Set::Object', default => sub { Set::Object->new }, required => 1, lazy => 1, predicate => 'has_set', clearer => 'clear_set', builder => 'build_set', );
  • 31. Attribute options default => 'kitteh', # string default => 3, # number default => sub { {} }, # HashRef default => sub { [] }, # ArrayRef default => sub { Object->new }, # an Object etc. etc. (if you need a more elaborate sub, use builder ) default
  • 32. Attribute options required => 1, # required required => 0, # not required required
  • 33. Attribute options lazy => 1, # make it lazy Class will not create the slot for this attribute unless it absolutely has to. That is defined by whether it is accessed at all. Wasn't accessed? You don't pay the penalty! :) lazy = good lazy
  • 34. Attribute options builder => 'build_it', # subroutine name sub build_it { my $self = shift; # not a problem! return Some::Object->new( $self->more_opts, ); } # and, obviously... after build_it => sub { “they will come” }; (a builder sets the value of the attribute) builder
  • 35. Attribute options clearer => 'clear_it', # subroutine name # you don't need to create the subroutine sub time_machine { my $self = shift; $self->clear_it; # 'it' never happened :) } (a clearer clears the value, as if it never existed) (does not go back to default) clearer
  • 36. Attribute options predicate => 'has_it', # subroutine name # you don't need to create the subroutine sub try_to_do_it { my $self = shift; $self->has_it && $self->do_it(); } (a predicate checks an attribute value exists) (even false values) (which is good!) predicate
  • 37. Attribute options lazy_build => 1, # <3 # the same as: lazy => 1, builder => '_build_it', # private clearer => 'clear_it', predicate => 'has_it', ( lazy_build is recommended) lazy_build
  • 38. Moose
    • It's beautiful
    • 39. It's clean
    • 40. It's stable
    • 41. It's here to stay
    • 42. Check it out!
    • A ton more options
    • 43. More control
    • 44. Roles / Traits <3
    • 45. Extensions OMGZ!
    • 46. MooseX::SimpleConfig
    • 47. MooseX::Getopt
    • 48. MooseX::Declare
    • 49. MooseX::Types
    • 50. ... sub { goto CPAN; }
  • 51. Thank you!