1Module::Compile(3) User Contributed Perl Documentation Module::Compile(3)
2
3
4
6 Module::Compile - Perl Module Compilation
7
9 package Foo;
10 use Module::Compile -base;
11
12 sub pmc_compile {
13 my ($class, $source) = @_;
14 # Convert $source into (most likely Perl 5) $compiled_output
15 return $compiled_output;
16 }
17
18 In Bar.pm:
19
20 package Bar;
21
22 use Foo;
23 ...
24 no Foo
25
26 or (implied "no Foo;"):
27
28 package Bar;
29
30 {
31 use Foo;
32 ...
33 }
34
35 To compile Bar.pm into Bar.pmc:
36
37 perl -c Bar.pm
38
40 This module provides a system for writing modules that compile other
41 Perl modules.
42
43 Modules that use these compilation modules get compiled into some
44 altered form the first time they are run. The result is cached into
45 ".pmc" files.
46
47 Perl has native support for ".pmc" files. It always checks for them,
48 before loading a ".pm" file.
49
51 You can declare a "v6.pm" compiler with:
52
53 package v6;
54 use Module::Compile -base;
55
56 sub pmc_compile {
57 my ($class, $source) = @_;
58 # ... some way to invoke pugs and give p5 code back ...
59 }
60
61 and use it like:
62
63 # MyModule.pm
64 use v6-pugs;
65 module MyModule;
66 # ...some p6 code here...
67 no v6;
68 # ...back to p5 land...
69
70 On the first time this module is loaded, it will compile Perl 6 blocks
71 into Perl 5 (as soon as the "no v6" line is seen), and merge it with
72 the Perl 5 blocks, saving the result into a MyModule.pmc file.
73
74 The next time around, Perl 5 will automatically load MyModule.pmc when
75 someone says "use MyModule". On the other hand, Perl 6 can run
76 MyModule.pm s a Perl 6 module just fine, as "use v6-pugs" and "no v6"
77 both works in a Perl 6 setting.
78
79 The v6.pm module will also check if MyModule.pmc is up to date. If it
80 is, then it will touch its timestamp so the ".pmc" is loaded on the
81 next time.
82
84 Module::Compile compilers gives you the following benefits:
85
86 · Ability to mix many source filterish modules in a much more sane
87 manner. Module::Compile controls the compilation process, calling
88 each compiler at the right time with the right data.
89
90 · Ability to ship precompiled modules without shipping
91 Module::Compile and the compiler modules themselves.
92
93 · Easier debugging of compiled/filtered code. The ".pmc" has the real
94 code you want to see.
95
96 · Zero additional runtime penalty after compilation, because "perl"
97 has already been doing the ".pmc" check on every module load since
98 1999!
99
101 NOTE: *** NOT FULLY IMPLEMENTED YET ***
102
103 Module::Compile attempts to make source filtering a sane process, by
104 parsing up your module's source code into various blocks; so that by
105 the time a compiler is called it only gets the source code that it
106 should be looking at.
107
108 This section describes the rather complex algorithm that
109 Module::Compile uses.
110
111 First, the source module is preprocessed to hide heredocs, since the
112 content inside heredocs can possibly confuse further parsing.
113
114 Next, the source module is divided into a shallow tree of blocks:
115
116 PREAMBLE:
117 (SUBROUTINE | BAREBLOCK | POD | PLAIN)S
118 PACKAGES:
119 PREFACE
120 (SUBROUTINE | BAREBLOCK | POD | PLAIN)S
121 DATA
122
123 All of these blocks begin and end on line boundaries. They are
124 described as follows:
125
126 PREAMBLE - Lines before the first C<package> statement.
127 PACKAGES - Lines beginning with a C<package statement and continuing
128 until the next C<package> or C<DATA> section.
129 DATA - The DATA section. Begins with the line C<__DATA__> or
130 C<__END__>.
131 SUBROUTINE - A top level (not nested) subroutine. Ending '}' must be
132 on its own line in the first column.
133 BAREBLOCK - A top level (not nested) code block. Ending '}' must be
134 on its own line in the first column.
135 POD - Pod sections beginning with C<^=\w+> and ending with C<=cut>.
136 PLAIN - Lines not in SUBROUTINE, BAREBLOCK or POD.
137 PREFACE - Lines before the first block in a package.
138
139 Next, all the blocks are scanned for lines like:
140
141 use Foo qw'x y z';
142 no Foo;
143
144 Where Foo is a Module::Compile subclass.
145
146 The lines within a given block between a "use" and "no" statement are
147 marked to be passed to that compiler. The end of an inner block
148 effectively acts as a "no" statement for any compile sections in that
149 block. "use" statements in a PREFACE apply to all the code in a
150 PACKAGE. "use" statements in a PREAMBLE apply to all the code in all
151 PACKAGES.
152
153 After all the code has been parsed into blocks and the blocks have been
154 marked for various compilers, Module::Compile dispatches the code
155 blocks to the compilers. It does so in a most specific to most general
156 order. So inner blocks get compiled first, then outer blocks.
157
158 A compiler may choose to declare that its result not be recompiled by
159 some other containing parser. In this case the result of the
160 compilation is replaced by a single line containing the hexadecimal
161 digest of the result in double quotes followed by a semicolon. Like:
162
163 "f1d2d2f924e986ac86fdf7b36c94bcdf32beec15";
164
165 The rationale of this is that randoms strings are usally left alone by
166 compilers. After all the compilers have finished, the digest lines will
167 be expanded again.
168
169 Every bit of the default process described above is overridable by
170 various methods.
171
173 Module::Install makes it terribly easy to prepare a module distribution
174 with compiled .pmc files. Module::Compile installs a
175 Module::Install::PMC plugin. All you need to do is add this line to
176 your Makefile.PL:
177
178 pmc_support;
179
180 Any of your distrbution's modules that use Module::Compile based
181 modules will automatically be compiled into .pmc files and shipped with
182 your distribtution precompiled. This means that people who install your
183 module distribtution do not need to have the compilers installed
184 themselves. So you don't need to make the compiler modules be
185 prerequisites.
186
188 Module::Install
189
191 Ingy dA~Xt Net <ingy@cpan.org>
192
193 Audrey Tang <autrijus@autrijus.org>
194
196 Copyright (c) 2006. Ingy dA~Xt Net. All rights reserved.
197
198 This program is free software; you can redistribute it and/or modify it
199 under the same terms as Perl itself.
200
201 See <http://www.perl.com/perl/misc/Artistic.html>
202
203
204
205perl v5.12.0 2006-10-17 Module::Compile(3)