start page | rating of books | rating of authors | reviews | copyrights

Book HomeJava and XSLTSearch this book

3.4. The Perl Compiler

Starting with Perl 5.005, the Perl compiler became part of the standard Perl distribution. You'll find that with Perl 5.6 and later, the Perl compiler has become far more stable. The compiler allows you to distribute Perl programs in binary form, which enables easy packaging of Perl-based programs without relying on the source machine to have the correct version of Perl and the correct modules installed. After the initial compilation, running a compiled program should be faster because 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:

Bytecode
Translates a script into platform-independent Perl bytecode.

C
Translates a Perl script into C code.

CC
Translates a Perl script into optimized C code.

Deparse
Regenerates Perl source code from a compiled program.

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

Showlex
Shows lexical variables used in functions or files.

Xref
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  
# You may have to provide the full path of where cc_harness lives
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 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 will be compiled. Valid only 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.

-gen
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.

-mod
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 will be compiled. Only valid if compiling one file on the command line.

-prog
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, in which rename_regex is a Perl regular expression.

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

-sav
Tells Perl to save the intermediate C code.

-verbose verbose_level
Compiles 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 listed in the following table.

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 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.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.