moose best practices
DESCRIPTION
This presentation was created for, and presented at, the August 11th Thousand Oaks Perl Mongers meeting.TRANSCRIPT
![Page 1: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/1.jpg)
MooseBest Practices
by Aran Deltac (CPAN: bluefeet)
![Page 2: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/2.jpg)
make_immutable()
package MyClass;use Moose;
…
__PACKAGE__->meta->make_immutable;1;
![Page 3: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/3.jpg)
default => $value
has ‘is_clean’ => ( is => ‘ro’, isa => ‘Bool’, default => 1,);
![Page 4: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/4.jpg)
Many Possible Stateshas ‘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) { … }
![Page 5: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/5.jpg)
Many Possible Stateshas ‘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) { … }
![Page 6: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/6.jpg)
required => 1
has ‘ssn’ => ( is => ‘ro’, isa => ‘Str’, required => 1,);
![Page 7: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/7.jpg)
Many Possible Statessubtype ‘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• []• [ … ]
…
![Page 8: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/8.jpg)
builder => $sub_name
has ‘dbh’ => ( is => ‘ro’, isa => ‘dbh’, builder => ‘_build_dbh’,);sub _build_dbh { return DBI->connect(…);}
![Page 9: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/9.jpg)
lazy => $bool
has ‘dbh’ => ( is => ‘ro’, isa => ‘dbh’, builder => ‘_build_dbh’, lazy => 1,);sub _build_dbh { return DBI->connect(…);}
![Page 10: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/10.jpg)
lazy_build => 1
has ‘dbh’ => ( is => ‘ro’, isa => ‘DBI::db’, lazy_build => 1,);
sub _build_dbh { return DBI->connect( … );}
![Page 11: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/11.jpg)
is => ‘ro’
has ‘name’ => ( is => ‘ro’, isa => ‘Str’,);
![Page 12: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/12.jpg)
Changing Statehas ‘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!
![Page 13: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/13.jpg)
Changing Statehas ‘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!
![Page 14: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/14.jpg)
writer => $sub_name
has ‘color’ => ( is => ‘ro’, isa => ‘Str’, write => ‘_color’,);
![Page 15: Moose Best Practices](https://reader036.vdocuments.mx/reader036/viewer/2022082804/5482d872b4af9f12188b459b/html5/thumbnails/15.jpg)
init_arg => undef
has ‘result’ => ( is => ‘ro’, isa => ‘HashRef’, writer => ‘_set_result’, init_arg => undef,);