Perl has always had a compiler: your source is compiled into an internal form (a parse tree) which is then optimized before being run.
Since version 5.005,
Perl has shipped with a module capable of inspecting the optimized parse tree (B),
and this has been used to write many useful utilities,
including a module that lets you turn your Perl into C source code that can be compiled into a native executable.

The B module provides access to the parse tree,
and other modules ("back ends") do things with the tree.
Some write it out as semi-human-readable text.
Another traverses the parse tree to build a cross-reference of which subroutines,
formats,
and variables are used where.
Another checks your code for dubious constructs.
Yet another back end dumps the parse tree back out as Perl source,
acting as a source code beautifier or deobfuscator.

Because its original purpose was to be a way to produce C code corresponding to a Perl program,
and in turn a native executable,
the B module and its associated back ends are known as "the compiler",
even though they don't really compile anything.
Different parts of the compiler are more accurately a "translator",
or an "inspector",
but people want Perl to have a "compiler option" not an "inspector gadget".
What can you do?

This document covers the use of the Perl compiler: which modules it comprises,
how to use the most important of the back end modules,
what problems there are,
and how to work around them.

The following sections describe how to use the various compiler back ends.
They're presented roughly in order of maturity,
so that the most stable and proven back ends are described first,
and the most experimental and incomplete back ends are described last.

The O module automatically enabled the -c flag to Perl,
which prevents Perl from executing your code once it has been compiled.
This is why all the back ends print:

The cross referencing back end (B::Xref) produces a report on your program, breaking down declarations and uses of subroutines and variables (and formats) by file and subroutine. For instance, here's part of the report from the pod2man program that comes with Perl:

This shows the variables used in the subroutine clear_noremap. The variable $ready_to_print is a my() (lexical) variable, introduced (first declared with my()) on line 1069, and used on line 1079. The variable $& from the main package is used on 1086, and so on.

The Deparse back end turns your Perl source back into Perl source. It can reformat along the way, making it useful as a deobfuscator. The most basic way to use it is:

$ perl -MO=Deparse myperlprogram

You'll notice immediately that Perl has no idea of how to paragraph your code. You'll have to separate chunks of code from each other with newlines by hand. However, watch what it will do with one-liners:

This module is the introspective ("reflective" in Java terms) module, which allows a Perl program to inspect its innards. The back end modules all use this module to gain access to the compiled parse tree. You, the user of a back end module, will not need to interact with B.

This module prints a concise (but complete) version of the Perl parse tree. Its output is more customizable than the one of B::Terse or B::Debug (and it can emulate them). This module useful for people who are writing their own back end, or who are learning about the Perl internals. It's not useful to the average programmer.

This module dumps the Perl parse tree in verbose detail to STDOUT. It's useful for people who are writing their own back end, or who are learning about the Perl internals. It's not useful to the average programmer.

This module produces Perl source code from the compiled parse tree. It is useful in debugging and deconstructing other people's code, also as a pretty-printer for your own source. See "The Decompiling Back End" for details about usage.

This module inspects the compiled form of your source code for things which, while some people frown on them, aren't necessarily bad enough to justify a warning. For instance, use of an array in scalar context without explicitly saying scalar(@array) is something that Lint can identify. See "The Lint Back End" for details about usage.

This module prints the contents of the parse tree, but without as much information as B::Debug. For comparison, print "Hello, world." produced 96 lines of output from B::Debug, but only 6 from B::Terse.

This module is useful for people who are writing their own back end, or who are learning about the Perl internals. It's not useful to the average programmer.

This module prints a report on where the variables, subroutines, and formats are defined and used within a program and the modules it loads. See "The Cross Referencing Back End" for details about usage.

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