• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Moose Best Practices
 

Moose Best Practices

on

  • 4,676 views

This presentation was created for, and presented at, the August 11th Thousand Oaks Perl Mongers meeting.

This presentation was created for, and presented at, the August 11th Thousand Oaks Perl Mongers meeting.

Statistics

Views

Total Views
4,676
Views on SlideShare
2,798
Embed Views
1,878

Actions

Likes
1
Downloads
13
Comments
0

12 Embeds 1,878

http://blog.bluefeet.net 1583
http://www.arandeltac.com 107
http://bluefeet.net 55
http://arandeltac.com 55
http://arandeltac.tumblr.com 32
http://www.bluefeet.net 30
http://old.bluefeet.net 8
http://quirktools.com 3
http://webcache.googleusercontent.com 2
http://translate.googleusercontent.com 1
http://www.tumblr.com 1
http://www.google.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

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 Best Practices Moose Best Practices Presentation Transcript

    • MooseBest Practices
      by Aran Deltac (CPAN: bluefeet)
    • make_immutable()
      package MyClass;
      use Moose;

      __PACKAGE__->meta->make_immutable;
      1;
    • default => $value
      has ‘is_clean’ => (
      is => ‘ro’,
      isa => ‘Bool’,
      default => 1,
      );
    • Many Possible States
      has ‘ages’ => (
      is => ‘rw’,
      isa => ‘ArrayRef’,
      );
      sub avg_age {
      my ($self) = @_;
      my $ages = $self->ages();
      return (sum @$ages ) /
      scalar @$ages;
      }
      Possible States:
      undef
      []
      [ … ]
      if (defined($ages) and @$ages) { … }
    • Many Possible States
      has ‘ages’ => (
      is => ‘rw’,
      isa => ‘ArrayRef’,
      default => sub{ [] },
      );
      sub avg_age {
      my ($self) = @_;
      my $ages = $self->ages();
      return (sum @$ages ) /
      scalar @$ages;
      }
      Possible Values:
      undef
      []
      [ … ]
      if (@$ages) { … }
    • required => 1
      has ‘ssn’ => (
      is => ‘ro’,
      isa => ‘Str’,
      required => 1,
      );
    • Many Possible States
      subtype ‘NonEmptyArrayRef'
      => as ‘ArrayRef’
      => where { @$_ > 0 };
      has ‘ages’ => (
      is => ‘rw’,
      isa => ‘NonEmptyArrayRef’,
      required => 1,
      );
      sub avg_age {
      my ($self) = @_;
      my $ages = $self->ages();
      return (sum @$ages ) /
      scalar @$ages;
      }
      Possible Values:
      undef
      []
      [ … ]

    • builder => $sub_name
      has ‘dbh’ => (
      is => ‘ro’,
      isa => ‘dbh’,
      builder => ‘_build_dbh’,
      );
      sub _build_dbh {
      return DBI->connect(…);
      }
    • lazy => $bool
      has ‘dbh’ => (
      is => ‘ro’,
      isa => ‘dbh’,
      builder => ‘_build_dbh’,
      lazy => 1,
      );
      sub _build_dbh {
      return DBI->connect(…);
      }
    • lazy_build => 1
      has ‘dbh’ => (
      is => ‘ro’,
      isa => ‘DBI::db’,
      lazy_build => 1,
      );
      sub _build_dbh {
      return DBI->connect( … );
      }
    • is => ‘ro’
      has ‘name’ => (
      is => ‘ro’,
      isa => ‘Str’,
      );
    • Changing State
      has ‘ages’ => (
      is => ‘rw’,
      isa => ‘ArrayRef’,
      default => sub{ [] },
      );
      has ‘avg_age’ => (
      is => ‘ro’,
      isa => ‘Int’,
      lazy_build => 1,
      );
      sub _build_avg_age {
      my ($self) = @_;
      my $ages = $self->ages();
      return (sum @$ages ) /
      scalar @$ages;
      }
      my $obj = Class->new(
      ages => [4, 7],
      );
      print $obj->avg_age(); # 6
      $obj->ages([4, 11]);
      print $obj->avg_age(); # 6!
    • Changing State
      has ‘ages’ => (
      is => ‘ro’,
      isa => ‘ArrayRef’,
      default => sub{ [] },
      );
      has ‘avg_age’ => (
      is => ‘ro’,
      isa => ‘Int’,
      lazy_build => 1,
      );
      sub _build_avg_age {
      my ($self) = @_;
      my $ages = $self->ages();
      return (sum @$ages ) /
      scalar @$ages;
      }
      my $obj = Class->new(
      ages => [4, 7],
      );
      print $obj->avg_age(); # 6
      $obj->ages([4, 11]);
      print $obj->avg_age(); # 6!
    • writer => $sub_name
      has ‘color’ => (
      is => ‘ro’,
      isa => ‘Str’,
      write => ‘_color’,
      );
    • init_arg => undef
      has ‘result’ => (
      is => ‘ro’,
      isa => ‘HashRef’,
      writer => ‘_set_result’,
      init_arg => undef,
      );