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:
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:
Used with -run or
-e. Passes the string
arguments to the executable as @ARGV
.
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.
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.
Adds directories inside include_directories to the compilation command.
Adds directories in library_directories to the compilation command.
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.
Gives the name executable_name to the executable that will 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.
Provides the rule rename_regex for creating executable filenames, in which rename_regex is a Perl regular expression.
Immediately runs the generated Perl code. Note
that the rest of @ARGV
is
interpreted as arguments to the program being compiled.
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 | | Code generation errors to STDERR. |
2 | | Compilation errors to STDERR. |
4 | | Descriptive text to STDERR. |
8 | | Code generation errors to file. Requires -log. |
16 | | Compilation errors to file. Requires -log. |
32 | | 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. Also see the documentation that comes with the compiler, which includes more complete information on installing and using it.
18.189.189.67