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

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Moose Best Practices

4,647

Published on

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
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,647
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
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<br />by Aran Deltac (CPAN: bluefeet)<br />
  • 2. make_immutable()<br />package MyClass;<br />use Moose;<br />…<br />__PACKAGE__->meta->make_immutable;<br />1;<br />
  • 3. default => $value<br />has ‘is_clean’ => (<br /> is => ‘ro’,<br />isa => ‘Bool’,<br /> default => 1,<br />);<br />
  • 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. 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. required => 1<br />has ‘ssn’ => (<br /> is => ‘ro’,<br />isa => ‘Str’,<br /> required => 1,<br />);<br />
  • 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. 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. 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. 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. is => ‘ro’<br />has ‘name’ => (<br /> is => ‘ro’,<br />isa => ‘Str’,<br />);<br />
  • 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. 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. writer => $sub_name<br />has ‘color’ => (<br /> is => ‘ro’,<br />isa => ‘Str’,<br />write => ‘_color’,<br />);<br />
  • 15. init_arg => undef<br />has ‘result’ => (<br /> is => ‘ro’,<br />isa => ‘HashRef’,<br /> writer => ‘_set_result’,<br />init_arg => undef,<br />);<br />

×