Commit | Line | Data |
5de2944f |
1 | |
2 | =pod |
3 | |
4 | =head1 NAME |
5 | |
6 | Moose::Cookbook::Recipe7 - Making Moose fast with immutable |
7 | |
8 | =head1 SYNOPSIS |
9 | |
10 | package Point; |
11 | use Moose; |
12 | |
13 | has 'x' => (isa => 'Int', is => 'ro'); |
14 | has 'y' => (isa => 'Int', is => 'rw'); |
15 | |
16 | __PACKAGE__->meta->make_immutable; |
17 | |
18 | =head1 DESCRIPTION |
19 | |
20 | The Moose metaclass API provides a method C<make_immutable()>. At a |
21 | high level, this calling this method does two things to your |
22 | class. One, it makes it faster. In particular, object construction and |
23 | accessors are effectively "inlined" in your class, and no longer go |
24 | through the meta-object system. |
25 | |
26 | Second, you can no longer make changes via the metaclass API such as |
27 | adding attributes. In practice, this won't be a problem, as you don't |
28 | usually need to do this at runtime after first loading the class. |
29 | |
30 | =head2 Immutabilization and C<new()> |
31 | |
32 | If you override C<new()> in your class, then the immutabilization code |
33 | will not be able to provide an optimized constructor for your |
34 | class. Instead, consider providing a C<BUILD()> method. You can |
35 | probably do the same thing in a C<BUILD()> method. |
36 | |
37 | Alternately, if you really need to provide a different C<new()>, you |
38 | can also provide your own immutabilization method. |
39 | |
40 | Discussing this is beyond the scope of this recipe, however. |
41 | |
42 | =head1 CONCLUSION |
43 | |
44 | We strongly recommend you make your classes immutable. It makes your |
45 | code much faster, basically for free. This will be especially |
46 | noticeable when creating many objects or calling accessors frequently. |
47 | |
48 | =head1 AUTHOR |
49 | |
50 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
51 | |
52 | =head1 COPYRIGHT AND LICENSE |
53 | |
54 | Copyright 2006-2008 by Infinity Interactive, Inc. |
55 | |
56 | L<http://www.iinteractive.com> |
57 | |
58 | This library is free software; you can redistribute it and/or modify |
59 | it under the same terms as Perl itself. |
60 | |
61 | =cut |