Upcoming SlideShare
×

Moose Best Practices

5,109 views
5,001 views

Published on

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

Published in: Technology
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
5,109
On SlideShare
0
From Embeds
0
Number of Embeds
2,178
Actions
Shares
0
15
0
Likes
1
Embeds 0
No embeds

No notes for slide

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 />