X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FManual%2FBestPractices.pod;h=9349718740891c6314ab0559b541f7b4d230feaf;hb=b5332f430eed2cf37cbaf345a911d4f4f1675323;hp=691f7d0ac0445d9899276d6f48d90853e849f42c;hpb=46c52442538cebcfa4bec51258bf5d14e60c7924;p=gitmo%2FMoose.git diff --git a/lib/Moose/Manual/BestPractices.pod b/lib/Moose/Manual/BestPractices.pod index 691f7d0..9349718 100644 --- a/lib/Moose/Manual/BestPractices.pod +++ b/lib/Moose/Manual/BestPractices.pod @@ -32,8 +32,11 @@ Moose sugar and making your class immutable. 1; -The C bit is simply good code hygiene, and making classes -immutable speeds up a lot of things, most notably object construction. +The C bit simply good code hygiene, as it removes all the +Moose keywords that are no longer needed once your class has been +built. C relinquishes your right to make further +changes to your class, and allows Moose to speed up a lot of things, +most notably object construction. =head2 Never override C @@ -134,8 +137,11 @@ changing the parents. =head2 Namespace your types Use some sort of namespacing convention for type names. We recommend -something like "MyApp.Type.Foo". I use "::" as the namespace -separator, since that overlaps with actual class names. +something like "MyApp::Type::Foo". + +If you're intending to package your types up for re-use using +L later, avoid using characters that are invalid in +perl identifiers such as a space or period. =head2 Do not coerce Moose built-ins directly @@ -146,15 +152,15 @@ type. # very naughty! coerce 'ArrayRef' => from Str - => via { [ split /,/ ] }; + => via { [ split /,/ ] }; Instead, create a subtype and coerce that: - subtype 'My.ArrayRef' => as 'ArrayRef'; + subtype 'My::ArrayRef' => as 'ArrayRef'; - coerce 'My.ArrayRef' + coerce 'My::ArrayRef' => from 'Str' - => via { [ split /,/ ] }; + => via { [ split /,/ ] }; =head2 Do not coerce class names directly @@ -165,15 +171,15 @@ have magical side effects elsewhere: # also very naughty! coerce 'HTTP::Headers' => from 'HashRef' - => via { HTTP::Headers->new( %{$_} ) }; + => via { HTTP::Headers->new( %{$_} ) }; Instead, we can create an "empty" subtype for the coercion: - subtype 'My.HTTP::Headers' => as class_type('HTTP::Headers'); + subtype 'My::HTTP::Headers' => as class_type('HTTP::Headers'); - coerce 'My.HTTP::Headers' + coerce 'My::HTTP::Headers' => from 'HashRef' - => via { HTTP::Headers->new( %{$_} ) }; + => via { HTTP::Headers->new( %{$_} ) }; =head2 Use coercion instead of unions @@ -204,7 +210,7 @@ will be faster when immutabilized. Many of these practices also help get the most out of meta programming. If you used an overridden C to do type coercion by hand, rather than defining a real coercion, there is no introspectable -metadata. This sort of thing is particularly problematic MooseX +metadata. This sort of thing is particularly problematic for MooseX extensions which rely on introspection to do the right thing. =head1 AUTHOR