0
MooseBest Practices<br />by Aran Deltac (CPAN: bluefeet)<br />
make_immutable()<br />package MyClass;<br />use Moose;<br />…<br />__PACKAGE__->meta->make_immutable;<br />1;<br />
default => $value<br />has ‘is_clean’ => (<br />   is => ‘ro’,<br />isa => ‘Bool’,<br />   default => 1,<br />);<br />
Many Possible States<br />has ‘ages’ => (<br />    is => ‘rw’,<br />isa => ‘ArrayRef’,<br />);<br />sub avg_age {<br />   ...
Many Possible States<br />has ‘ages’ => (<br />    is => ‘rw’,<br />isa => ‘ArrayRef’,<br />default => sub{ [] },<br />);<...
required => 1<br />has ‘ssn’ => (<br />    is => ‘ro’,<br />isa => ‘Str’,<br />   required => 1,<br />);<br />
Many Possible States<br />subtype ‘NonEmptyArrayRef'<br />   => as ‘ArrayRef’<br />   => where { @$_ > 0 }; <br />has ‘age...
builder => $sub_name<br />has ‘dbh’ => (<br />    is => ‘ro’,<br />isa => ‘dbh’,<br />builder => ‘_build_dbh’,<br />);<br ...
lazy => $bool<br />has ‘dbh’ => (<br />    is => ‘ro’,<br />isa => ‘dbh’,<br />    builder => ‘_build_dbh’,<br />lazy    =...
lazy_build => 1<br />has ‘dbh’ => (<br />    is => ‘ro’,<br />isa => ‘DBI::db’,<br />lazy_build => 1,<br />);<br />sub _bu...
is => ‘ro’<br />has ‘name’ => (<br />    is => ‘ro’,<br />isa => ‘Str’,<br />);<br />
Changing State<br />has ‘ages’ => (<br />is => ‘rw’,<br />isa => ‘ArrayRef’,<br />   default => sub{ [] },<br />);<br />ha...
Changing State<br />has ‘ages’ => (<br />is => ‘ro’,<br />isa => ‘ArrayRef’,<br />   default => sub{ [] },<br />);<br />ha...
writer => $sub_name<br />has ‘color’ => (<br />   is => ‘ro’,<br />isa => ‘Str’,<br />write => ‘_color’,<br />);<br />
init_arg => undef<br />has ‘result’ => (<br />    is => ‘ro’,<br />isa => ‘HashRef’,<br />   writer => ‘_set_result’,<br /...
Upcoming SlideShare
Loading in...5
×

Moose Best Practices

4,668

Published on

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,668
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Moose Best Practices"

  1. 1. MooseBest Practices<br />by Aran Deltac (CPAN: bluefeet)<br />
  2. 2. make_immutable()<br />package MyClass;<br />use Moose;<br />…<br />__PACKAGE__->meta->make_immutable;<br />1;<br />
  3. 3. default => $value<br />has ‘is_clean’ => (<br /> is => ‘ro’,<br />isa => ‘Bool’,<br /> default => 1,<br />);<br />
  4. 4. Many Possible States<br />has ‘ages’ => (<br /> is => ‘rw’,<br />isa => ‘ArrayRef’,<br />);<br />sub avg_age {<br /> my ($self) = @_;<br /> my $ages = $self->ages();<br /> return (sum @$ages ) / <br /> scalar @$ages;<br />}<br />Possible States:<br />undef<br />[]<br />[ … ]<br />if (defined($ages) and @$ages) { … }<br />
  5. 5. Many Possible States<br />has ‘ages’ => (<br /> is => ‘rw’,<br />isa => ‘ArrayRef’,<br />default => sub{ [] },<br />);<br />sub avg_age {<br /> my ($self) = @_;<br /> my $ages = $self->ages();<br /> return (sum @$ages ) / <br /> scalar @$ages;<br />}<br />Possible Values:<br />undef<br /> []<br /> [ … ]<br />if (@$ages) { … }<br />
  6. 6. required => 1<br />has ‘ssn’ => (<br /> is => ‘ro’,<br />isa => ‘Str’,<br /> required => 1,<br />);<br />
  7. 7. Many Possible States<br />subtype ‘NonEmptyArrayRef'<br /> => as ‘ArrayRef’<br /> => where { @$_ > 0 }; <br />has ‘ages’ => (<br /> is => ‘rw’,<br />isa => ‘NonEmptyArrayRef’,<br />required => 1,<br />);<br />sub avg_age {<br /> my ($self) = @_;<br /> my $ages = $self->ages();<br /> return (sum @$ages ) / <br /> scalar @$ages;<br />}<br />Possible Values:<br />undef<br />[]<br /> [ … ]<br />…<br />
  8. 8. builder => $sub_name<br />has ‘dbh’ => (<br /> is => ‘ro’,<br />isa => ‘dbh’,<br />builder => ‘_build_dbh’,<br />);<br />sub _build_dbh {<br /> return DBI->connect(…);<br />}<br />
  9. 9. lazy => $bool<br />has ‘dbh’ => (<br /> is => ‘ro’,<br />isa => ‘dbh’,<br /> builder => ‘_build_dbh’,<br />lazy => 1,<br />);<br />sub _build_dbh {<br /> return DBI->connect(…);<br />}<br />
  10. 10. lazy_build => 1<br />has ‘dbh’ => (<br /> is => ‘ro’,<br />isa => ‘DBI::db’,<br />lazy_build => 1,<br />);<br />sub _build_dbh {<br /> return DBI->connect( … );<br />}<br />
  11. 11. is => ‘ro’<br />has ‘name’ => (<br /> is => ‘ro’,<br />isa => ‘Str’,<br />);<br />
  12. 12. Changing State<br />has ‘ages’ => (<br />is => ‘rw’,<br />isa => ‘ArrayRef’,<br /> default => sub{ [] },<br />);<br />has ‘avg_age’ => (<br /> is => ‘ro’,<br />isa => ‘Int’,<br />lazy_build => 1,<br />);<br />sub _build_avg_age {<br /> my ($self) = @_;<br /> my $ages = $self->ages();<br /> return (sum @$ages ) / <br /> scalar @$ages;<br />}<br />my $obj = Class->new(<br /> ages => [4, 7],<br />);<br />print $obj->avg_age(); # 6<br />$obj->ages([4, 11]);<br />print $obj->avg_age(); # 6!<br />
  13. 13. Changing State<br />has ‘ages’ => (<br />is => ‘ro’,<br />isa => ‘ArrayRef’,<br /> default => sub{ [] },<br />);<br />has ‘avg_age’ => (<br /> is => ‘ro’,<br />isa => ‘Int’,<br />lazy_build => 1,<br />);<br />sub _build_avg_age {<br /> my ($self) = @_;<br /> my $ages = $self->ages();<br /> return (sum @$ages ) / <br /> scalar @$ages;<br />}<br />my $obj = Class->new(<br /> ages => [4, 7],<br />);<br />print $obj->avg_age(); # 6<br />$obj->ages([4, 11]);<br />print $obj->avg_age(); # 6!<br />
  14. 14. writer => $sub_name<br />has ‘color’ => (<br /> is => ‘ro’,<br />isa => ‘Str’,<br />write => ‘_color’,<br />);<br />
  15. 15. init_arg => undef<br />has ‘result’ => (<br /> is => ‘ro’,<br />isa => ‘HashRef’,<br /> writer => ‘_set_result’,<br />init_arg => undef,<br />);<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×