The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

=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<make_immutable()>. 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<new()>

If you override C<new()> in your class, then the immutabilization code
will not be able to provide an optimized constructor for your
class. Instead, consider providing a C<BUILD()> method. You can
probably do the same thing in a C<BUILD()> method.

Alternately, if you really need to provide a different C<new()>, 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 E<lt>autarch@urth.orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2006-2008 by Infinity Interactive, Inc.

L<http://www.iinteractive.com>

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut