1PERLCOMPILE(1)         Perl Programmers Reference Guide         PERLCOMPILE(1)
2
3
4

NAME

6       perlcompile - Introduction to the Perl Compiler-Translator
7

DESCRIPTION

9       Perl has always had a compiler: your source is compiled into an
10       internal form (a parse tree) which is then optimized before being run.
11       Since version 5.005, Perl has shipped with a module capable of
12       inspecting the optimized parse tree ("B"), and this has been used to
13       write many useful utilities, including a module that lets you turn your
14       Perl into C source code that can be compiled into a native executable.
15
16       The "B" module provides access to the parse tree, and other modules
17       ("back ends") do things with the tree.  Some write it out as semi-
18       human-readable text.  Another traverses the parse tree to build a
19       cross-reference of which subroutines, formats, and variables are used
20       where.  Another checks your code for dubious constructs.  Yet another
21       back end dumps the parse tree back out as Perl source, acting as a
22       source code beautifier or deobfuscator.
23
24       Because its original purpose was to be a way to produce C code
25       corresponding to a Perl program, and in turn a native executable, the
26       "B" module and its associated back ends are known as "the compiler",
27       even though they don't really compile anything.  Different parts of the
28       compiler are more accurately a "translator", or an "inspector", but
29       people want Perl to have a "compiler option" not an "inspector gadget".
30       What can you do?
31
32       This document covers the use of the Perl compiler: which modules it
33       comprises, how to use the most important of the back end modules, what
34       problems there are, and how to work around them.
35
36   Layout
37       The compiler back ends are in the "B::" hierarchy, and the front-end
38       (the module that you, the user of the compiler, will sometimes interact
39       with) is the O module.
40
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 10
43       (if there's a bug in it, we're very surprised):
44
45       B::Lint
46           Complains if it finds dubious constructs in your source code.
47           Status: 6 (it works adequately, but only has a very limited number
48           of areas that it checks).
49
50       B::Deparse
51           Recreates the Perl source, making an attempt to format it
52           coherently.  Status: 8 (it works nicely, but a few obscure things
53           are missing).
54
55       B::Xref
56           Reports on the declaration and use of subroutines and variables.
57           Status: 8 (it works nicely, but still has a few lingering bugs).
58

Using The Back Ends

60       The following sections describe how to use the various compiler back
61       ends.  They're presented roughly in order of maturity, so that the most
62       stable and proven back ends are described first, and the most
63       experimental and incomplete back ends are described last.
64
65       The O module automatically enabled the -c flag to Perl, which prevents
66       Perl from executing your code once it has been compiled.  This is why
67       all the back ends print:
68
69         myperlprogram syntax OK
70
71       before producing any other output.
72
73   The Cross Referencing Back End
74       The cross referencing back end (B::Xref) produces a report on your
75       program, breaking down declarations and uses of subroutines and
76       variables (and formats) by file and subroutine.  For instance, here's
77       part of the report from the pod2man program that comes with Perl:
78
79         Subroutine clear_noremap
80           Package (lexical)
81             $ready_to_print   i1069, 1079
82           Package main
83             $&                1086
84             $.                1086
85             $0                1086
86             $1                1087
87             $2                1085, 1085
88             $3                1085, 1085
89             $ARGV             1086
90             %HTML_Escapes     1085, 1085
91
92       This shows the variables used in the subroutine "clear_noremap".  The
93       variable $ready_to_print is a my() (lexical) variable, introduced
94       (first declared with my()) on line 1069, and used on line 1079.  The
95       variable $& from the main package is used on 1086, and so on.
96
97       A line number may be prefixed by a single letter:
98
99       i   Lexical variable introduced (declared with my()) for the first
100           time.
101
102       &   Subroutine or method call.
103
104       s   Subroutine defined.
105
106       r   Format defined.
107
108       The most useful option the cross referencer has is to save the report
109       to a separate file.  For instance, to save the report on myperlprogram
110       to the file report:
111
112         $ perl -MO=Xref,-oreport myperlprogram
113
114   The Decompiling Back End
115       The Deparse back end turns your Perl source back into Perl source.  It
116       can reformat along the way, making it useful as a deobfuscator.  The
117       most basic way to use it is:
118
119         $ perl -MO=Deparse myperlprogram
120
121       You'll notice immediately that Perl has no idea of how to paragraph
122       your code.  You'll have to separate chunks of code from each other with
123       newlines by hand.  However, watch what it will do with one-liners:
124
125         $ perl -MO=Deparse -e '$op=shift||die "usage: $0
126         code [...]";chomp(@ARGV=<>)unless@ARGV; for(@ARGV){$was=$_;eval$op;
127         die$@ if$@; rename$was,$_ unless$was eq $_}'
128         -e syntax OK
129         $op = shift @ARGV || die("usage: $0 code [...]");
130         chomp(@ARGV = <ARGV>) unless @ARGV;
131         foreach $_ (@ARGV) {
132             $was = $_;
133             eval $op;
134             die $@ if $@;
135             rename $was, $_ unless $was eq $_;
136         }
137
138       The decompiler has several options for the code it generates.  For
139       instance, you can set the size of each indent from 4 (as above) to 2
140       with:
141
142         $ perl -MO=Deparse,-si2 myperlprogram
143
144       The -p option adds parentheses where normally they are omitted:
145
146         $ perl -MO=Deparse -e 'print "Hello, world\n"'
147         -e syntax OK
148         print "Hello, world\n";
149         $ perl -MO=Deparse,-p -e 'print "Hello, world\n"'
150         -e syntax OK
151         print("Hello, world\n");
152
153       See B::Deparse for more information on the formatting options.
154
155   The Lint Back End
156       The lint back end (B::Lint) inspects programs for poor style.  One
157       programmer's bad style is another programmer's useful tool, so options
158       let you select what is complained about.
159
160       To run the style checker across your source code:
161
162         $ perl -MO=Lint myperlprogram
163
164       To disable context checks and undefined subroutines:
165
166         $ perl -MO=Lint,-context,-undefined-subs myperlprogram
167
168       See B::Lint for information on the options.
169

Module List for the Compiler Suite

171       B   This module is the introspective ("reflective" in Java terms)
172           module, which allows a Perl program to inspect its innards.  The
173           back end modules all use this module to gain access to the compiled
174           parse tree.  You, the user of a back end module, will not need to
175           interact with B.
176
177       O   This module is the front-end to the compiler's back ends.  Normally
178           called something like this:
179
180             $ perl -MO=Deparse myperlprogram
181
182           This is like saying "use O 'Deparse'" in your Perl program.
183
184       B::Concise
185           This module prints a concise (but complete) version of the Perl
186           parse tree.  Its output is more customizable than the one of
187           B::Terse or B::Debug (and it can emulate them). This module useful
188           for people who are writing their own back end, or who are learning
189           about the Perl internals.  It's not useful to the average
190           programmer.
191
192       B::Debug
193           This module dumps the Perl parse tree in verbose detail to STDOUT.
194           It's useful for people who are writing their own back end, or who
195           are learning about the Perl internals.  It's not useful to the
196           average programmer.
197
198       B::Deparse
199           This module produces Perl source code from the compiled parse tree.
200           It is useful in debugging and deconstructing other people's code,
201           also as a pretty-printer for your own source.  See "The Decompiling
202           Back End" for details about usage.
203
204       B::Lint
205           This module inspects the compiled form of your source code for
206           things which, while some people frown on them, aren't necessarily
207           bad enough to justify a warning.  For instance, use of an array in
208           scalar context without explicitly saying "scalar(@array)" is
209           something that Lint can identify.  See "The Lint Back End" for
210           details about usage.
211
212       B::Showlex
213           This module prints out the my() variables used in a function or a
214           file.  To get a list of the my() variables used in the subroutine
215           mysub() defined in the file myperlprogram:
216
217             $ perl -MO=Showlex,mysub myperlprogram
218
219           To get a list of the my() variables used in the file myperlprogram:
220
221             $ perl -MO=Showlex myperlprogram
222
223           [BROKEN]
224
225       B::Terse
226           This module prints the contents of the parse tree, but without as
227           much information as B::Debug.  For comparison, "print "Hello,
228           world.""  produced 96 lines of output from B::Debug, but only 6
229           from B::Terse.
230
231           This module is useful for people who are writing their own back
232           end, or who are learning about the Perl internals.  It's not useful
233           to the average programmer.
234
235       B::Xref
236           This module prints a report on where the variables, subroutines,
237           and formats are defined and used within a program and the modules
238           it loads.  See "The Cross Referencing Back End" for details about
239           usage.
240

KNOWN PROBLEMS

242       BEGIN{} blocks are executed while compiling your code.  Any external
243       state that is initialized in BEGIN{}, such as opening files, initiating
244       database connections etc., do not behave properly.  To work around
245       this, Perl has an INIT{} block that corresponds to code being executed
246       before your program begins running but after your program has finished
247       being compiled.  Execution order: BEGIN{}, (possible save of state
248       through compiler back-end), INIT{}, program runs, END{}.
249

AUTHOR

251       This document was originally written by Nathan Torkington, and is now
252       maintained by the perl5-porters mailing list perl5-porters@perl.org.
253
254
255
256perl v5.10.1                      2009-04-11                    PERLCOMPILE(1)
Impressum