Advertisement
Advertisement

More Related Content

Advertisement

Recently uploaded(20)

Advertisement

Moose (Perl 5)

  1. Moose A postmodern metaclass-based object system for Perl 5
  2. Manual new() method
  3. 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'}; }
  4. 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, );
  5. Get it?
  6. use warnings
  7. An object!
  8. new() method
  9. A pon-.. err.. a moose! package User; use Moose; 1; You get:
  10. You can set the getter or setter manually via reader / writer
  11. I'll show more attribute options later on has name => ( is => 'rw', );
  12. You can combine: ArrayRef[Str] , HashRef[ArrayRef[Int]]
  13. They have inheritance: Int is a Num
  14. Roll your own using subtype package User; use Moose; has name => ( is => 'rw', isa => 'Str', ); 1;
  15. Methods are the same as before sub method { my $self = shift; ... $self->more(); }
  16. after
  17. around
  18. inner
  19. augment
  20. 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', );
  21. 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
  22. Attribute options required => 1, # required required => 0, # not required required
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. It's clean
  29. It's stable
  30. It's here to stay
  31. More control
  32. Roles / Traits <3
  33. Extensions OMGZ!
  34. MooseX::POE
  35. MooseX::Getopt
  36. MooseX::Declare
  37. MooseX::Storage
  38. ... sub { goto CPAN; }
Advertisement