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:
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:
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.
Copyright © 2002 O'Reilly & Associates. All rights reserved.