This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Be consistent in writing deobfuscator
[perl5.git] / pod / perlcompile.pod
CommitLineData
54a137f5
GS
1=head1 NAME
2
3perlcompile - Introduction to the Perl Compiler-Translator
4
5=head1 DESCRIPTION
6
7Perl has always had a compiler: your source is compiled into an
8internal form (a parse tree) which is then optimized before being
9run. Since version 5.005, Perl has shipped with a module
10capable of inspecting the optimized parse tree (C<B>), and this has
11been used to write many useful utilities, including a module that lets
d1be9408 12you turn your Perl into C source code that can be compiled into a
54a137f5
GS
13native executable.
14
15The 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
de125441
RGS
17semi-human-readable text. Another traverses the parse tree to build a
18cross-reference of which subroutines, formats, and variables are used
19where. Another checks your code for dubious constructs. Yet another back
20end dumps the parse tree back out as Perl source, acting as a source code
21beautifier or deobfuscator.
54a137f5
GS
22
23Because its original purpose was to be a way to produce C code
24corresponding to a Perl program, and in turn a native executable, the
25C<B> module and its associated back ends are known as "the
26compiler", even though they don't really compile anything.
27Different parts of the compiler are more accurately a "translator",
28or an "inspector", but people want Perl to have a "compiler
29option" not an "inspector gadget". What can you do?
30
31This document covers the use of the Perl compiler: which modules
32it comprises, how to use the most important of the back end modules,
33what problems there are, and how to work around them.
34
35=head2 Layout
36
37The 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
de125441 39interact with) is the O module.
54a137f5
GS
40
41Here are the important back ends to know about, with their status
42expressed as a number from 0 (outline for later implementation) to
4310 (if there's a bug in it, we're very surprised):
44
45=over 4
46
54a137f5
GS
47=item B::Lint
48
49Complains if it finds dubious constructs in your source code. Status:
506 (it works adequately, but only has a very limited number of areas
51that it checks).
52
53=item B::Deparse
54
55Recreates the Perl source, making an attempt to format it coherently.
56Status: 8 (it works nicely, but a few obscure things are missing).
57
58=item B::Xref
59
60Reports on the declaration and use of subroutines and variables.
61Status: 8 (it works nicely, but still has a few lingering bugs).
62
63=back
64
65=head1 Using The Back Ends
66
67The following sections describe how to use the various compiler back
68ends. They're presented roughly in order of maturity, so that the
69most stable and proven back ends are described first, and the most
70experimental and incomplete back ends are described last.
71
72The O module automatically enabled the B<-c> flag to Perl, which
73prevents Perl from executing your code once it has been compiled.
74This is why all the back ends print:
75
76 myperlprogram syntax OK
77
78before producing any other output.
79
4a4eefd0 80=head2 The Cross Referencing Back End
54a137f5 81
4a4eefd0 82The cross referencing back end (B::Xref) produces a report on your program,
54a137f5
GS
83breaking down declarations and uses of subroutines and variables (and
84formats) by file and subroutine. For instance, here's part of the
85report from the I<pod2man> program that comes with Perl:
86
87 Subroutine clear_noremap
88 Package (lexical)
89 $ready_to_print i1069, 1079
90 Package main
91 $& 1086
92 $. 1086
93 $0 1086
94 $1 1087
95 $2 1085, 1085
96 $3 1085, 1085
97 $ARGV 1086
98 %HTML_Escapes 1085, 1085
99
100This shows the variables used in the subroutine C<clear_noremap>. The
101variable C<$ready_to_print> is a my() (lexical) variable,
102B<i>ntroduced (first declared with my()) on line 1069, and used on
103line 1079. The variable C<$&> from the main package is used on 1086,
104and so on.
105
106A line number may be prefixed by a single letter:
107
108=over 4
109
110=item i
111
112Lexical variable introduced (declared with my()) for the first time.
113
114=item &
115
116Subroutine or method call.
117
118=item s
119
120Subroutine defined.
121
122=item r
123
124Format defined.
125
126=back
127
128The most useful option the cross referencer has is to save the report
129to a separate file. For instance, to save the report on
130I<myperlprogram> to the file I<report>:
131
132 $ perl -MO=Xref,-oreport myperlprogram
133
134=head2 The Decompiling Back End
135
136The Deparse back end turns your Perl source back into Perl source. It
c1e31494 137can reformat along the way, making it useful as a deobfuscator. The
54a137f5
GS
138most basic way to use it is:
139
140 $ perl -MO=Deparse myperlprogram
141
142You'll notice immediately that Perl has no idea of how to paragraph
143your code. You'll have to separate chunks of code from each other
144with newlines by hand. However, watch what it will do with
145one-liners:
146
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 $_}'
150 -e syntax OK
151 $op = shift @ARGV || die("usage: $0 code [...]");
152 chomp(@ARGV = <ARGV>) unless @ARGV;
153 foreach $_ (@ARGV) {
154 $was = $_;
155 eval $op;
156 die $@ if $@;
157 rename $was, $_ unless $was eq $_;
158 }
159
54a137f5
GS
160The decompiler has several options for the code it generates. For
161instance, you can set the size of each indent from 4 (as above) to
1622 with:
163
164 $ perl -MO=Deparse,-si2 myperlprogram
165
166The B<-p> option adds parentheses where normally they are omitted:
167
168 $ perl -MO=Deparse -e 'print "Hello, world\n"'
169 -e syntax OK
170 print "Hello, world\n";
171 $ perl -MO=Deparse,-p -e 'print "Hello, world\n"'
172 -e syntax OK
173 print("Hello, world\n");
174
175See L<B::Deparse> for more information on the formatting options.
176
4a4eefd0 177=head2 The Lint Back End
54a137f5 178
4a4eefd0
GS
179The lint back end (B::Lint) inspects programs for poor style. One
180programmer's bad style is another programmer's useful tool, so options
181let you select what is complained about.
54a137f5
GS
182
183To run the style checker across your source code:
184
185 $ perl -MO=Lint myperlprogram
186
187To disable context checks and undefined subroutines:
188
189 $ perl -MO=Lint,-context,-undefined-subs myperlprogram
190
191See L<B::Lint> for information on the options.
192
384e87d1
RGS
193=head1 Module List for the Compiler Suite
194
54a137f5
GS
195=over 4
196
197=item B
198
199This module is the introspective ("reflective" in Java terms)
200module, which allows a Perl program to inspect its innards. The
201back end modules all use this module to gain access to the compiled
202parse tree. You, the user of a back end module, will not need to
203interact with B.
204
205=item O
206
207This module is the front-end to the compiler's back ends. Normally
208called something like this:
209
210 $ perl -MO=Deparse myperlprogram
211
212This is like saying C<use O 'Deparse'> in your Perl program.
213
384e87d1
RGS
214=item B::Concise
215
216This module prints a concise (but complete) version of the Perl parse
217tree. Its output is more customizable than the one of B::Terse or
218B::Debug (and it can emulate them). This module useful for people who
219are writing their own back end, or who are learning about the Perl
220internals. It's not useful to the average programmer.
221
54a137f5
GS
222=item B::Debug
223
224This module dumps the Perl parse tree in verbose detail to STDOUT.
225It's useful for people who are writing their own back end, or who
226are learning about the Perl internals. It's not useful to the
227average programmer.
228
229=item B::Deparse
230
231This module produces Perl source code from the compiled parse tree.
232It is useful in debugging and deconstructing other people's code,
233also as a pretty-printer for your own source. See
234L</"The Decompiling Back End"> for details about usage.
235
54a137f5
GS
236=item B::Lint
237
238This module inspects the compiled form of your source code for things
239which, while some people frown on them, aren't necessarily bad enough
240to justify a warning. For instance, use of an array in scalar context
241without explicitly saying C<scalar(@array)> is something that Lint
242can identify. See L</"The Lint Back End"> for details about usage.
243
244=item B::Showlex
245
246This module prints out the my() variables used in a function or a
4375e838 247file. To get a list of the my() variables used in the subroutine
54a137f5
GS
248mysub() defined in the file myperlprogram:
249
250 $ perl -MO=Showlex,mysub myperlprogram
251
4375e838 252To get a list of the my() variables used in the file myperlprogram:
54a137f5
GS
253
254 $ perl -MO=Showlex myperlprogram
255
256[BROKEN]
257
54a137f5
GS
258=item B::Terse
259
260This module prints the contents of the parse tree, but without as much
261information as B::Debug. For comparison, C<print "Hello, world.">
262produced 96 lines of output from B::Debug, but only 6 from B::Terse.
263
264This module is useful for people who are writing their own back end,
265or who are learning about the Perl internals. It's not useful to the
266average programmer.
267
268=item B::Xref
269
270This module prints a report on where the variables, subroutines, and
271formats are defined and used within a program and the modules it
272loads. See L</"The Cross Referencing Back End"> for details about
273usage.
274
a45bd81d 275=back
54a137f5
GS
276
277=head1 KNOWN PROBLEMS
278
54a137f5
GS
279BEGIN{} blocks are executed while compiling your code. Any external
280state that is initialized in BEGIN{}, such as opening files, initiating
281database connections etc., do not behave properly. To work around
282this, Perl has an INIT{} block that corresponds to code being executed
283before your program begins running but after your program has finished
284being compiled. Execution order: BEGIN{}, (possible save of state
285through compiler back-end), INIT{}, program runs, END{}.
286
287=head1 AUTHOR
288
289This document was originally written by Nathan Torkington, and is now
290maintained by the perl5-porters mailing list
291I<perl5-porters@perl.org>.
292
293=cut