3 perlcompile - Introduction to the Perl Compiler-Translator
7 Perl has always had a compiler: your source is compiled into an
8 internal form (a parse tree) which is then optimized before being
9 run. Since version 5.005, Perl has shipped with a module
10 capable of inspecting the optimized parse tree (C<B>), and this has
11 been used to write many useful utilities, including a module that lets
12 you turn your Perl into C source code that can be compiled into a
15 The C<B> module provides access to the parse tree, and other modules
16 ("back ends") do things with the tree. Some write it out as
17 semi-human-readable text. Another traverses the parse tree to build a
18 cross-reference of which subroutines, formats, and variables are used
19 where. Another checks your code for dubious constructs. Yet another back
20 end dumps the parse tree back out as Perl source, acting as a source code
21 beautifier or deobfuscator.
23 Because its original purpose was to be a way to produce C code
24 corresponding to a Perl program, and in turn a native executable, the
25 C<B> module and its associated back ends are known as "the
26 compiler", even though they don't really compile anything.
27 Different parts of the compiler are more accurately a "translator",
28 or an "inspector", but people want Perl to have a "compiler
29 option" not an "inspector gadget". What can you do?
31 This document covers the use of the Perl compiler: which modules
32 it comprises, how to use the most important of the back end modules,
33 what problems there are, and how to work around them.
37 The compiler back ends are in the C<B::> hierarchy, and the front-end
38 (the module that you, the user of the compiler, will sometimes
39 interact with) is the O module.
41 Here are the important back ends to know about, with their status
42 expressed as a number from 0 (outline for later implementation) to
43 10 (if there's a bug in it, we're very surprised):
49 Complains if it finds dubious constructs in your source code. Status:
50 6 (it works adequately, but only has a very limited number of areas
55 Recreates the Perl source, making an attempt to format it coherently.
56 Status: 8 (it works nicely, but a few obscure things are missing).
60 Reports on the declaration and use of subroutines and variables.
61 Status: 8 (it works nicely, but still has a few lingering bugs).
65 =head1 Using The Back Ends
67 The following sections describe how to use the various compiler back
68 ends. They're presented roughly in order of maturity, so that the
69 most stable and proven back ends are described first, and the most
70 experimental and incomplete back ends are described last.
72 The O module automatically enabled the B<-c> flag to Perl, which
73 prevents Perl from executing your code once it has been compiled.
74 This is why all the back ends print:
76 myperlprogram syntax OK
78 before producing any other output.
80 =head2 The Cross Referencing Back End
82 The cross referencing back end (B::Xref) produces a report on your program,
83 breaking down declarations and uses of subroutines and variables (and
84 formats) by file and subroutine. For instance, here's part of the
85 report from the I<pod2man> program that comes with Perl:
87 Subroutine clear_noremap
89 $ready_to_print i1069, 1079
98 %HTML_Escapes 1085, 1085
100 This shows the variables used in the subroutine C<clear_noremap>. The
101 variable C<$ready_to_print> is a my() (lexical) variable,
102 B<i>ntroduced (first declared with my()) on line 1069, and used on
103 line 1079. The variable C<$&> from the main package is used on 1086,
106 A line number may be prefixed by a single letter:
112 Lexical variable introduced (declared with my()) for the first time.
116 Subroutine or method call.
128 The most useful option the cross referencer has is to save the report
129 to a separate file. For instance, to save the report on
130 I<myperlprogram> to the file I<report>:
132 $ perl -MO=Xref,-oreport myperlprogram
134 =head2 The Decompiling Back End
136 The Deparse back end turns your Perl source back into Perl source. It
137 can reformat along the way, making it useful as a deobfuscator. The
138 most basic way to use it is:
140 $ perl -MO=Deparse myperlprogram
142 You'll notice immediately that Perl has no idea of how to paragraph
143 your code. You'll have to separate chunks of code from each other
144 with newlines by hand. However, watch what it will do with
147 $ perl -MO=Deparse -e '$op=shift||die "usage: $0
148 code [...]";chomp(@ARGV=<>)unless@ARGV; for(@ARGV){$was=$_;eval$op;
149 die$@ if$@; rename$was,$_ unless$was eq $_}'
151 $op = shift @ARGV || die("usage: $0 code [...]");
152 chomp(@ARGV = <ARGV>) unless @ARGV;
157 rename $was, $_ unless $was eq $_;
160 The decompiler has several options for the code it generates. For
161 instance, you can set the size of each indent from 4 (as above) to
164 $ perl -MO=Deparse,-si2 myperlprogram
166 The B<-p> option adds parentheses where normally they are omitted:
168 $ perl -MO=Deparse -e 'print "Hello, world\n"'
170 print "Hello, world\n";
171 $ perl -MO=Deparse,-p -e 'print "Hello, world\n"'
173 print("Hello, world\n");
175 See L<B::Deparse> for more information on the formatting options.
177 =head2 The Lint Back End
179 The lint back end (B::Lint) inspects programs for poor style. One
180 programmer's bad style is another programmer's useful tool, so options
181 let you select what is complained about.
183 To run the style checker across your source code:
185 $ perl -MO=Lint myperlprogram
187 To disable context checks and undefined subroutines:
189 $ perl -MO=Lint,-context,-undefined-subs myperlprogram
191 See L<B::Lint> for information on the options.
193 =head1 Module List for the Compiler Suite
199 This module is the introspective ("reflective" in Java terms)
200 module, which allows a Perl program to inspect its innards. The
201 back end modules all use this module to gain access to the compiled
202 parse tree. You, the user of a back end module, will not need to
207 This module is the front-end to the compiler's back ends. Normally
208 called something like this:
210 $ perl -MO=Deparse myperlprogram
212 This is like saying C<use O 'Deparse'> in your Perl program.
216 This module prints a concise (but complete) version of the Perl parse
217 tree. Its output is more customizable than the one of B::Terse or
218 B::Debug (and it can emulate them). This module useful for people who
219 are writing their own back end, or who are learning about the Perl
220 internals. It's not useful to the average programmer.
224 This module dumps the Perl parse tree in verbose detail to STDOUT.
225 It's useful for people who are writing their own back end, or who
226 are learning about the Perl internals. It's not useful to the
231 This module produces Perl source code from the compiled parse tree.
232 It is useful in debugging and deconstructing other people's code,
233 also as a pretty-printer for your own source. See
234 L</"The Decompiling Back End"> for details about usage.
238 This module inspects the compiled form of your source code for things
239 which, while some people frown on them, aren't necessarily bad enough
240 to justify a warning. For instance, use of an array in scalar context
241 without explicitly saying C<scalar(@array)> is something that Lint
242 can identify. See L</"The Lint Back End"> for details about usage.
246 This module prints out the my() variables used in a function or a
247 file. To get a list of the my() variables used in the subroutine
248 mysub() defined in the file myperlprogram:
250 $ perl -MO=Showlex,mysub myperlprogram
252 To get a list of the my() variables used in the file myperlprogram:
254 $ perl -MO=Showlex myperlprogram
260 This module prints the contents of the parse tree, but without as much
261 information as B::Debug. For comparison, C<print "Hello, world.">
262 produced 96 lines of output from B::Debug, but only 6 from B::Terse.
264 This module is useful for people who are writing their own back end,
265 or who are learning about the Perl internals. It's not useful to the
270 This module prints a report on where the variables, subroutines, and
271 formats are defined and used within a program and the modules it
272 loads. See L</"The Cross Referencing Back End"> for details about
277 =head1 KNOWN PROBLEMS
279 BEGIN{} blocks are executed while compiling your code. Any external
280 state that is initialized in BEGIN{}, such as opening files, initiating
281 database connections etc., do not behave properly. To work around
282 this, Perl has an INIT{} block that corresponds to code being executed
283 before your program begins running but after your program has finished
284 being compiled. Execution order: BEGIN{}, (possible save of state
285 through compiler back-end), INIT{}, program runs, END{}.
289 This document was originally written by Nathan Torkington, and is now
290 maintained by the perl5-porters mailing list
291 I<perl5-porters@perl.org>.