home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

3.4 The Perl Compiler

A native-code compiler for Perl is now (as of Perl 5.005) part of the standard Perl distribution. The compiler allows you to distribute Perl programs in binary form, which enables easy packaging of Perl-based programs without having to depend on the source machine having the correct version of Perl and the correct modules installed. After the initial compilation, running a compiled program should be faster to the extent that it doesn't have to be recompiled each time it's run. However, you shouldn't expect that the compiled code itself will run faster than the original Perl source or that the executable will be smaller - in reality, the executable file is likely to be significantly bigger.

This initial release of the compiler is still considered to be a beta version. It's distributed as an extension module, B, that comes with the following backends:


Translates a script into platform-independent Perl byte code.


Translates a Perl script into C code.


Translates a Perl script into optimized C code.


Regenerates Perl source code from a compiled program.


Extends the Perl -w option. Named after the Unix Lint program-checker.


Shows lexical variables used in functions or files.


Creates a cross-reference listing for a program.

Once you've generated the C code with either the C or the CC backend, you run the cc_harness program to compile it into an executable. There is also a byteperl interpreter that lets you run the code you've generated with the Bytecode backend.

Here's an example that takes a simple "Hello world" program and uses the CC backend to generate C code:

% perl -MO=CC,-ohi.c hi.pl
hi.pl syntax OK
% perl cc_harness -O2 -ohi hi.c
gcc -B/usr/ccs/bin/ -D_REENTRANT -DDEBUGGING -I/usr/local/include 
-I/usr/local/lib/perl5/sun4-solaris-thread/5.00466/CORE -O2 -ohi hi.c 
-L/usr/local/lib /usr/local/lib/perl5/sun4-solaris-thread/5.00466/CORE/libperl.a 
-lsocket -lnsl -lgdbm -ldl -lm -lposix4 -lpthread -lc -lcrypt
% hi
Hi there, world!

The compiler also comes with a frontend, perlcc . You can use it to compile code into a standalone executable, or to compile a module (a .pm file) into a shared object (an .so file) that can be included in a Perl program via use . For example:

% perlcc a.p         # compiles into the executable 'a'
% perlcc A.pm        # compiles into A.so

The following options can be used with perlcc :

-argv arguments

Used with -run or -e . Passes the string arguments to the executable as @ARGV .

-C c_code_name

Gives the name c_code_name to the generated C code that is to be compiled. Only valid if you are compiling one file on the command line.

-e perl_line_to_execute

Works like perl -e to compile "one-liners." The default is to compile and run the code. With -o , it saves the resulting executable.


Creates the intermediate C code but doesn't compile the results; does an implicit -sav .

-I include_directories

Adds directories inside include_directories to the compilation command.

-L library_directories

Adds directories in library_directories to the compilation command.

-log logname

Opens a log file (for append) for saving text from a compile command.


Tells perlcc to compile the files given at the command line as modules. Usually used with module files that don't end with .pm .

-o executable_name

Gives the name executable_name to the executable that is to be compiled. Only valid if compiling one file on the command line.


Tells perlcc to compile the files given at the command line as programs. Usually used with program files that don't end with a .p , .pl , or .bat extension.

-regex rename_regex

Provides the rule rename_regex for creating executable filenames, where rename_regex is a Perl regular expression.


Immediately run the generated Perl code. Note that the rest of @ARGV is interpreted as arguments to the program being compiled.


Tells Perl to save the intermediate C code.

-verbose verbose_level

Compile verbosely, setting verbose_level to control the degree of verbosity. verbose_level can be given as either a sum of bits or a list of letters. Values are:

Bit Letter Action
1 g Code generation errors to STDERR.
2 a Compilation errors to STDERR.
4 t Descriptive text to STDERR.
8 f

Code generation errors to file. Requires -log .

16 c

Compilation errors to file. Requires -log .

32 d

Descriptive text to file. Requires -log .

With -log , the default level is 63; otherwise the default level is 7.

There are two environment variables that you can set for perlcc : PERL_SCRIPT_EXT and PERL_MODULE_EXT. These can be used to modify the default extensions that perlcc recognizes for programs and for modules. The variables take colon-separated Perl regular expressions.

The modules that comprise the compiler are described in Chapter 8, Standard Modules . Also see the documentation that comes with the compiler, which includes more complete information on installing and using it.