Your SlideShare is downloading. ×
  • Like
Moose Best Practices
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Moose Best Practices

  • 4,583 views
Published

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.

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,583
On SlideShare
0
From Embeds
0
Number of Embeds
6

Actions

Shares
Downloads
14
Comments
0
Likes
1

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. MooseBest Practices
    by Aran Deltac (CPAN: bluefeet)
  • 2. make_immutable()
    package MyClass;
    use Moose;

    __PACKAGE__->meta->make_immutable;
    1;
  • 3. default => $value
    has ‘is_clean’ => (
    is => ‘ro’,
    isa => ‘Bool’,
    default => 1,
    );
  • 4. 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) { … }
  • 5. 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) { … }
  • 6. required => 1
    has ‘ssn’ => (
    is => ‘ro’,
    isa => ‘Str’,
    required => 1,
    );
  • 7. 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
    []
    [ … ]

  • 8. builder => $sub_name
    has ‘dbh’ => (
    is => ‘ro’,
    isa => ‘dbh’,
    builder => ‘_build_dbh’,
    );
    sub _build_dbh {
    return DBI->connect(…);
    }
  • 9. lazy => $bool
    has ‘dbh’ => (
    is => ‘ro’,
    isa => ‘dbh’,
    builder => ‘_build_dbh’,
    lazy => 1,
    );
    sub _build_dbh {
    return DBI->connect(…);
    }
  • 10. lazy_build => 1
    has ‘dbh’ => (
    is => ‘ro’,
    isa => ‘DBI::db’,
    lazy_build => 1,
    );
    sub _build_dbh {
    return DBI->connect( … );
    }
  • 11. is => ‘ro’
    has ‘name’ => (
    is => ‘ro’,
    isa => ‘Str’,
    );
  • 12. 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!
  • 13. 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!
  • 14. writer => $sub_name
    has ‘color’ => (
    is => ‘ro’,
    isa => ‘Str’,
    write => ‘_color’,
    );
  • 15. init_arg => undef
    has ‘result’ => (
    is => ‘ro’,
    isa => ‘HashRef’,
    writer => ‘_set_result’,
    init_arg => undef,
    );