MooseX::Compile - Moose ♥ .pmc
In MyClass.pm:
MyClass.pm
package MyClass; use Moose; # your moose class here
On the command line:
$ mkcompile compile --make-immutable MyClass
Or to always compile:
use MooseX::Compile; # instead of use Moose
This is alpha code.
If you decide to to use it please come by the #moose IRC channel on irc.perl.org (maybe this link works: irc://irc.perl.org/#moose).
#moose
irc.perl.org
Your help in testing this is highly valued, so please feel free to verbally abuse nothingmuch in #moose until things are working properly.
nothingmuch
The example in the "SYNOPSIS" will compile MyClass into two files, MyClass.pmc and MyClass.mopc. The .pmc file caches all of the generated code, and the .mopc file is a Storable file of the metaclass instance.
MyClass
MyClass.pmc
MyClass.mopc
.pmc
.mopc
When MyClass is loaded the next time, Perl will see the .pmc file and load that instead. This file will load faster for several reasons:
Moose is no longer required to load MyClass, all the methods Moose normally generates are already saved in the .pmc file.
The metaclass does not need to be loaded, at least until you introspect. meta for compiled classes will lazy load the already computed metaclass instance from the .mopc file. When it is needed the instance will be deserialized and it's class (probably Moose::Meta::Class) will be loaded.
meta
If all your classes are compiled and you don't use introspection in your code, you can then deploy your code without using moose.
This is not a source filter.
Due to the fragility of source filtering in Perl, MooseX::Compile::Compiler will not alter the body of the class, but instead prefix it with a preamble that sets up the right environment for it.
MooseX::Compile::Compiler
This involves temporarily overriding CORE::GLOBAL::require to hide Moose from this module (but not others), and stubbing the sugar with no-ops (the various declarations are thus effectively stripped without altering the source code), amongst other things.
CORE::GLOBAL::require
Moose
Then the source code of the original class is executed normally, and when the file's lexical scope gets cleaned up then the final pieces of the class are put in place and all the trickery is undone.
Until this point meta is replaced with a mock object that will silently or loudly ignore various method calls depending on their nature. For instance
__PACKAGE__->meta->make_immutable();
is a silent no-op, because when the compiler compiled it the class was already immutable, so the loaded version will be immutable too.
On the other hand
__PACKAGE__->meta->superclasses(qw(Foo));
will complain because the value of @ISA is already captured, and changing it is meaningless.
@ISA
$__mx_is_compiled
This variable is set at BEGIN for modules in a .pmc. This allows you to write conditional code, like:
BEGIN
use if not(our $__mx_is_compiled) metaclass => "Blah"; __PACKAGE__->meta->add_attribute( ... ) unless our $__mx_is_compiled;
__mx_compile_post_hook
If you add a subroutine named __mx_compile_post_hook to your class it will be called at the end of compilation, allowing you to to diddle the class after loading.
This developer release comes with some serious limitations.
It has so far only been tested with the Point and Point3D classes from the recipe.
Point
Point3D
This means:
No method modifiers are supported yet. We know how and it's going to take a while longer.
Only core, optimized Moose types are guaranteed to work (Int, Str, etc). Other types may or may not deparse properly.
Int
Str
Roles are not yet supported.
Stuff is definitely going to break. This is just a first release of a fairly complex project, so bear with us =)
There is a fairly long TODO file in the distribution.
MooseX::Compile::Compiler, MooseX::Compile::Bootstrap, MooseX::Compile::CLI.
http://code2.0beta.co.uk/moose/svn/MooseX-Compile/trunk
Yuval Kogman <nothingmuch@woobling.org>
Copyright (c) 2008 Infinity Interactive, Yuval Kogman. All rights reserved This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2 POD Errors
The following errors were encountered while parsing the POD:
'=item' outside of any '=over'
You forgot a '=back' before '=head1'
To install MooseX::Compile, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::Compile
CPAN shell
perl -MCPAN -e shell install MooseX::Compile
For more information on module installation, please visit the detailed CPAN module installation guide.