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 Perl script into optimized C code.
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:
|1||Code generation errors to STDERR.|
|2||Compilation errors to STDERR.|
|4||Descriptive text to STDERR.|
Code generation errors to file. Requires -log.
Compilation errors to file. Requires -log.
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.