From: Dave Rolsky Date: Thu, 1 May 2008 23:14:01 +0000 (+0000) Subject: Took a shot at writing recipe 7 (it seemed easy enough) X-Git-Tag: 0_55~202 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5de2944ff9c8e64ceefdb4ca5c7c8035e1cb42e3;p=gitmo%2FMoose.git Took a shot at writing recipe 7 (it seemed easy enough) --- diff --git a/MANIFEST b/MANIFEST index 5b7f98b..beedbb0 100644 --- a/MANIFEST +++ b/MANIFEST @@ -21,6 +21,7 @@ lib/Moose/Cookbook/Recipe3.pod lib/Moose/Cookbook/Recipe4.pod lib/Moose/Cookbook/Recipe5.pod lib/Moose/Cookbook/Recipe6.pod +lib/Moose/Cookbook/Recipe7.pod lib/Moose/Cookbook/Snack/Types.pod lib/Moose/Cookbook/WTF.pod lib/Moose/Meta/Attribute.pm diff --git a/lib/Moose/Cookbook.pod b/lib/Moose/Cookbook.pod index c23632a..9261653 100644 --- a/lib/Moose/Cookbook.pod +++ b/lib/Moose/Cookbook.pod @@ -50,9 +50,10 @@ More type examples, including the use of type coercions. Demonstrates the use of C method modifiers, a way of turning the usual method overriding style "inside-out". -=item L - Making Moose fast with immutable (TODO) +=item L - Making Moose fast with immutable -I +Making a class immutable greatly increases the speed of accessors and +object construction. =item L - Managing complex relations with trigger (TODO) diff --git a/lib/Moose/Cookbook/Recipe7.pod b/lib/Moose/Cookbook/Recipe7.pod new file mode 100644 index 0000000..228546a --- /dev/null +++ b/lib/Moose/Cookbook/Recipe7.pod @@ -0,0 +1,64 @@ + +=pod + +=head1 NAME + +Moose::Cookbook::Recipe7 - Making Moose fast with immutable + +=head1 SYNOPSIS + + package Point; + use Moose; + + has 'x' => (isa => 'Int', is => 'ro'); + has 'y' => (isa => 'Int', is => 'rw'); + + __PACKAGE__->meta->make_immutable; + +=head1 DESCRIPTION + +The Moose metaclass API provides a method C. At a +high level, this calling this method does two things to your +class. One, it makes it faster. In particular, object construction and +accessors are effectively "inlined" in your class, and no longer go +through the meta-object system. + +Second, you can no longer make changes via the metaclass API such as +adding attributes. In practice, this won't be a problem, as you don't +usually need to do this at runtime after first loading the class. + +=head2 Immutabilization and C + +If you override C in your class, then the immutabilization code +will not be able to provide an optimized constructor for your +class. Instead, consider providing a C method. You can +probably do the same thing in a C method. + +Alternately, if you really need to provide a different C, you +can also provide your own immutabilization method. + +Discussing this is beyond the scope of this recipe, however. + +=head1 CONCLUSION + +We strongly recommend you make your classes immutable. It makes your +code much faster, basically for free. This will be especially +noticeable when creating many objects or calling accessors frequently. + +=head1 AUTHOR + +Dave Rolsky Eautarch@urth.orgE + +=head1 COPYRIGHT AND LICENSE + +Copyright 2006-2008 by Infinity Interactive, Inc. + +L + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut + + +=cut