Moose Best Practices
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Moose Best Practices

on

  • 4,904 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,904
Views on SlideShare
2,962
Embed Views
1,942

Actions

Likes
1
Downloads
13
Comments
0

13 Embeds 1,942

http://blog.bluefeet.net 1633
http://www.arandeltac.com 109
http://arandeltac.com 66
http://bluefeet.net 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://www.google.com 1
http://translate.googleusercontent.com 1
http://www.tumblr.com 1
http://news.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 Presentation 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,
    );