Upcoming SlideShare
×

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.

Standard text messaging rates apply

# Moose Best Practices

4,610
views

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

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
4,610
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
14
0
Likes
1
Embeds 0
No embeds

No notes for slide

### 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,
);