1.4.3. But How Do I Compile Perl?
You may be surprised to learn that all you have to do to
compile
Perl is to run it. When you run your program, Perl's internal
compiler first runs through your entire source, turning it into
internal
bytecodes
(an internal data structure representing the program); then
Perl's bytecode engine actually runs them.[35]
So, if there's a syntax error on line 200, you'll get
that error message before you start running line two.[36] If you have a loop that runs 5000
times, it's compiled just once; the actual loop can then run at
top speed. And there's no runtime penalty for using as many
comments and as much whitespace as you need to make your program easy
to understand. You can even use calculations involving only
constants, and the result is a constant computed once as the program
is beginning -- not each time through a loop.
To be sure, this compilation does take time -- it's
inefficient to have a voluminous Perl program that does one small
quick task (out of many potential tasks, say) and then exits, because
the runtime for the
program will be dwarfed by the compile time. But the compiler is very
fast; normally the compilation will be a tiny percentage of the
runtime.
An exception might be if you were writing a program to be run over
the
Web,
where it may be called hundreds or thousands of times every minute.
(This is a very high usage rate. If it were called a few hundreds or
thousands of times per day, like most programs
on the Web, we probably wouldn't worry too much about it.) Many
of these programs have very short runtimes, so the issue of
recompilation may become significant. If this is an issue for you,
you'll want to find a way to keep your program resident in
memory between invocations (whether it's written in Perl or
not); see the documentation for your web server and ask your local
expert for help with this.[37]
What if you could save the compiled bytecodes to avoid the overhead
of compilation? Or, even better, what if you could turn the bytecodes
into another language, like C, and then compile that? Well, both of
these things are possible (although beyond the scope of this book),
although they won't make most programs any easier to use,
maintain, debug, or install, and they may (for somewhat technical
reasons) make your program even slower.[38] We
don't know anyone who has ever needed to compile a Perl program
(except for experimental purposes), and we doubt you ever will ever
meet one, either.