Your SlideShare is downloading. ×
0
Moose A postmodern metaclass-based object system for Perl 5
Objects in Perl 5 (a short intro) <ul><li>A blessed hashref
Manual  new()  method
No real attributes </li></ul>bless {}, __PACKAGE__; sub new { my ( $class, @args ) =  @_; my $self = { @args }; # params b...
Why would you want to use Moose? Moose package Person; use strict; use warnings; use Carp qw( confess ); use DateTime; use...
Get it?
Defining an object in Moose <ul><li>use strict
use warnings
An object!
new()  method
A pon-.. err.. a moose! </li></ul>package User; use Moose; 1; You get:
Full-Affordance accessors <ul><li>' ro ' is also available for read-only attributes!
You can set the getter or setter manually via  reader / writer
I'll show more attribute options later on </li></ul>has name => ( is => 'rw', );
An attribute with type constraint <ul><li>Lots of types:  Str ,  Int ,  ArrayRef ,  HashRef ,  CodeRef ,  Regexp
You can combine:  ArrayRef[Str] ,  HashRef[ArrayRef[Int]]
They have inheritance:  Int  is a  Num
Roll your own using  subtype </li></ul>package User; use Moose; has name => ( is  => 'rw', isa => 'Str', ); 1;
Methods are the same as before sub method { my $self = shift; ... $self->more(); }
Inheritance is as easy as... <ul><li>Multiple inheritance is also possible,  extends  accepts an array </li></ul>package P...
Upcoming SlideShare
Loading in...5
×

Moose (Perl 5)

5,547

Published on

These are the slides from a lecture I did on Moose, the post-modern metaclass-based object system for Perl 5.

Published in: Technology, Business
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,547
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
98
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Transcript of "Moose (Perl 5)"

  1. 1. Moose A postmodern metaclass-based object system for Perl 5
  2. 2. Objects in Perl 5 (a short intro) <ul><li>A blessed hashref
  3. 3. Manual new() method
  4. 4. No real attributes </li></ul>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'}; }
  5. 5. 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 { &quot;$_ is not a valid email address&quot; }; has email_address => ( is => 'rw', isa => 'Email', required => 1, );
  6. 6. Get it?
  7. 7. Defining an object in Moose <ul><li>use strict
  8. 8. use warnings
  9. 9. An object!
  10. 10. new() method
  11. 11. A pon-.. err.. a moose! </li></ul>package User; use Moose; 1; You get:
  12. 12. Full-Affordance accessors <ul><li>' ro ' is also available for read-only attributes!
  13. 13. You can set the getter or setter manually via reader / writer
  14. 14. I'll show more attribute options later on </li></ul>has name => ( is => 'rw', );
  15. 15. An attribute with type constraint <ul><li>Lots of types: Str , Int , ArrayRef , HashRef , CodeRef , Regexp
  16. 16. You can combine: ArrayRef[Str] , HashRef[ArrayRef[Int]]
  17. 17. They have inheritance: Int is a Num
  18. 18. Roll your own using subtype </li></ul>package User; use Moose; has name => ( is => 'rw', isa => 'Str', ); 1;
  19. 19. Methods are the same as before sub method { my $self = shift; ... $self->more(); }
  20. 20. Inheritance is as easy as... <ul><li>Multiple inheritance is also possible, extends accepts an array </li></ul>package Punk; use Moose; extends 'Person'; 1; package Child; use Moose; extends qw/ Father Mother /; 1;
  21. 21. Roles are even easier! <ul><li>Multiple roles are recommend! with accepts an array too </li></ul>package Punk; use Moose; extends 'Person'; with 'Piercings'; 1; package Punk; use Moose; extends 'Person'; with qw/ Piercings Tattoos /; 1;
  22. 22. More hooks than a coat rack! package User::Secure; use Moose; extends 'User'; before login => sub { my $self = shift; $self->security_check(); }; 1; <ul><li>before
  23. 23. after
  24. 24. around
  25. 25. inner
  26. 26. augment </li></ul>
  27. 27. 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', );
  28. 28. 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
  29. 29. Attribute options required => 1, # required required => 0, # not required required
  30. 30. 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
  31. 31. 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
  32. 32. 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 attribute, not just the value) clearer
  33. 33. 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 exists) (even false values) (which is good!) predicate
  34. 34. 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
  35. 35. Moose <ul><li>It's beautiful
  36. 36. It's clean
  37. 37. It's stable
  38. 38. It's here to stay
  39. 39. Check it out! </li></ul><ul><li>A ton more options
  40. 40. More control
  41. 41. Roles / Traits <3
  42. 42. Extensions OMGZ!
  43. 43. MooseX::POE
  44. 44. MooseX::Getopt
  45. 45. MooseX::Declare
  46. 46. MooseX::Storage
  47. 47. ... sub { goto CPAN; } </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×