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

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Moose talk at FOSDEM 2011 (Perl devroom)

7,005

Published on

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.

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,005
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
96
Comments
0
Likes
4
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

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) <ul><li>A blessed hashref
  • 4. Manual new() method
  • 5. 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-&gt;{&apos;name&apos;} = $name; return $self-&gt;{&apos;name&apos;}; }
  • 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 &apos;name is a required attribute&apos;; $class-&gt;_validate_name( $p{name} ); exists $p{birth_date} or confess &apos;birth_date is a required attribute&apos;; $p{birth_date} = $class-&gt;_coerce_birth_date( $p{birth_date} ); $class-&gt;_validate_birth_date( $p{birth_date} ); $p{shirt_size} = &apos;l&apos; unless exists $p{shirt_size}: $class-&gt;_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 &apos;name must be a string&apos;; } Plain old Perl 5 package User; use Email::Valid; use Moose; use Moose::Util::TypeConstraints; extends &apos;Person&apos;; subtype &apos;Email&apos; =&gt; as &apos;Str&apos; =&gt; where { Email::Valid-&gt;address($_) } =&gt; message { &amp;quot;$_ is not a valid email address&amp;quot; }; has email_address =&gt; ( is =&gt; &apos;rw&apos;, isa =&gt; &apos;Email&apos;, required =&gt; 1, );
  • 7. Get it?
  • 8. Defining an object in Moose <ul><li>use strict
  • 9. use warnings
  • 10. An object!
  • 11. new() method
  • 12. A pon-.. err.. a moose! </li></ul>package User; use Moose; 1; You get:
  • 13. Full-Affordance accessors <ul><li>&apos; ro &apos; is also available for read-only attributes!
  • 14. You can set the getter or setter manually via reader / writer
  • 15. I&apos;ll show more attribute options later on </li></ul>has name =&gt; ( is =&gt; &apos;rw&apos;, );
  • 16. An attribute with type constraint <ul><li>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 </li></ul>package User; use Moose; has name =&gt; ( is =&gt; &apos;rw&apos;, isa =&gt; &apos;Str&apos;, ); 1;
  • 20. Methods are the same as before sub method { my $self = shift; ... $self-&gt;more(); }
  • 21. Inheritance is as easy as... <ul><li>Multiple inheritance is also possible, extends accepts an array </li></ul>package Punk; use Moose; extends &apos;Person&apos;; 1; package Child; use Moose; extends qw/ Father Mother /; 1;
  • 22. Roles are even easier! <ul><li>Multiple roles are recommend! with accepts an array too </li></ul>package Punk; use Moose; extends &apos;Person&apos;; with &apos;Piercings&apos;; 1; package Punk; use Moose; extends &apos;Person&apos;; with qw/ Piercings Tattoos /; 1;
  • 23. More hooks than a coat rack! package User::WinterAware; use Moose; extends &apos;User&apos;; before leaving =&gt; sub { my $self = shift; $self-&gt;cold and $self-&gt;take_jacket; }; 1; <ul><li>before
  • 24. after </li></ul>
  • 25. More hooks than a coat rack! package User::Secure; use Moose; extends &apos;User&apos;; around login =&gt; sub { my $orig = shift; my $self = shift; $self-&gt;security_check and $self-&gt;$orig(@_); }; 1; <ul><li>before
  • 26. after
  • 27. around
  • 28. inner
  • 29. augment </li></ul>
  • 30. Back to attributes options... has set =&gt; ( is =&gt; &apos;rw&apos;, isa =&gt; &apos;Set::Object&apos;, default =&gt; sub { Set::Object-&gt;new }, required =&gt; 1, lazy =&gt; 1, predicate =&gt; &apos;has_set&apos;, clearer =&gt; &apos;clear_set&apos;, builder =&gt; &apos;build_set&apos;, );
  • 31. Attribute options default =&gt; &apos;kitteh&apos;, # string default =&gt; 3, # number default =&gt; sub { {} }, # HashRef default =&gt; sub { [] }, # ArrayRef default =&gt; sub { Object-&gt;new }, # an Object etc. etc. (if you need a more elaborate sub, use builder ) default
  • 32. Attribute options required =&gt; 1, # required required =&gt; 0, # not required required
  • 33. Attribute options lazy =&gt; 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&apos;t accessed? You don&apos;t pay the penalty! :) lazy = good lazy
  • 34. Attribute options builder =&gt; &apos;build_it&apos;, # subroutine name sub build_it { my $self = shift; # not a problem! return Some::Object-&gt;new( $self-&gt;more_opts, ); } # and, obviously... after build_it =&gt; sub { “they will come” }; (a builder sets the value of the attribute) builder
  • 35. Attribute options clearer =&gt; &apos;clear_it&apos;, # subroutine name # you don&apos;t need to create the subroutine sub time_machine { my $self = shift; $self-&gt;clear_it; # &apos;it&apos; never happened :) } (a clearer clears the value, as if it never existed) (does not go back to default) clearer
  • 36. Attribute options predicate =&gt; &apos;has_it&apos;, # subroutine name # you don&apos;t need to create the subroutine sub try_to_do_it { my $self = shift; $self-&gt;has_it &amp;&amp; $self-&gt;do_it(); } (a predicate checks an attribute value exists) (even false values) (which is good!) predicate
  • 37. Attribute options lazy_build =&gt; 1, # &lt;3 # the same as: lazy =&gt; 1, builder =&gt; &apos;_build_it&apos;, # private clearer =&gt; &apos;clear_it&apos;, predicate =&gt; &apos;has_it&apos;, ( lazy_build is recommended) lazy_build
  • 38. Moose <ul><li>It&apos;s beautiful
  • 39. It&apos;s clean
  • 40. It&apos;s stable
  • 41. It&apos;s here to stay
  • 42. Check it out! </li></ul><ul><li>A ton more options
  • 43. More control
  • 44. Roles / Traits &lt;3
  • 45. Extensions OMGZ!
  • 46. MooseX::SimpleConfig
  • 47. MooseX::Getopt
  • 48. MooseX::Declare
  • 49. MooseX::Types
  • 50. ... sub { goto CPAN; } </li></ul>
  • 51. Thank you!

×