Compiling Perl Scripts into Standalone Executables
A tutorial by NicholasSolutions
1. What, Why and How?
This tutorial outlines the steps you need to take in order to compile your perl scripts into standalone executables. There are a
few ways to do this, including the commerical Perl2Exe, and packages such as
PerlBin and
App::Packer, and a tool called perlcc. I have had the most luck using a free perl module called PAR, which
packs perl code into a single .exe file, including all DLLs and other dependencies, so that is what I will discuss here. It is a very reliable and
powerful tool, and has not disappointed me yet (which I cannot say for its alternatives). Please keep in mind as you read this that there is a reason
people still code in C++ instead of just using 'compiled' perl. This is a fun (and useful!) trick, but if you're making a widely-distributed application,
you should ask yourself if perl is really the correct language to use.
Why would you want to do this? Often, people wish to obscure their perl code by 'compiling' it. Unfortunately (or perhaps,
fortunately, depending on your point of view), it is possible to 'decompile' executables made from perl code and look at the original source. Of
course, compiling does add a layer of defense, and additional source-obscuring techniques are available through PAR (see the -f
option in the pp documentation below), but this by no means keeps a determined and savvy individual from looking at your source code.
Another reason to 'compile' perl source code is to make the code run faster. Note that the actual instructions are executed no faster in
a 'compiled' executable than in the original script: all that this process does is bundle everything you need to run the script together in one zip file (even
though it 'looks' like an .exe). You do eliminate the need to recompile the script each time it is run, but the first time it is executed,
there may be some delay while the archive is decompressed (the following executions should not have a delay -- the first time the script is run, the files are
unpacked into your temporary directory so that they can be used again later).
The 'real' reason to want to compile perl code into an executable is portability. Imagine you've written a killer perl script
that you want to give to your friend, but in order to use it, he has to install new modules, and maybe even upgrade his version of perl. Or even better, what
about that friend you have who doesn't even have perl, and has no idea how to install it. Compiling perl scripts allows you to create applications that anyone
can use without a hassle. In fact, you can even write scripts using Perl/Tk along with the compiler, to make full-featured applications with
GUIs.
PAR works on Unix as well, and the procedures for installing and
using it are virtually the same. Note also that you can install PAR using the ActiveState Perl Package Manager (ppm), but it's good to be able
to install modules from CPAN (which are often more up to date than the ones on ActiveState's site; some are not available from ActiveState at all), so that
is what I show below.2. Download the Files
nmake
Before we begin, you will need to download the necessary files. First, you will need the Windows version of the Unix make, which
builds projects based on the instructions in a makefile. You can download the Windows version, called nmake, from
this page
or directly using this link: Nmake15.exe
The file you download (nmake15.exe) is a self-extracting archive that we will use in just a minute.
Modules
You will also need the following modules:
Parse::Binary |
Documentation | Download Module |
Win32::Exe |
Documentation | Download Module |
Module::ScanDeps |
Documentation | Download Module |
PAR::Dist |
Documentation | Download Module |
PAR |
Documentation | Download Module |
3. Install nmake
Run the namke15.exe file. It will create three files: NMAKE.EXE, NMAKE.ERR, and README.TXT.
You can delete the README.TXT file.
Put the other two files in your perl bin directory (C:Perlbin for most people). Done! You just installed nmake.
4. Install the Modules
First, you will have to extract the files from the tarred, gzipped files you downloaded from CPAN. If you don't already have a utility to do this, I recommend 7-Zip -- it's great compression utility, and it's free. If you are using 7-Zip, simply right click on the archived files and select "extract files".
Once you have extracted the archived files, you will have three folders (one for each module). Open up a DOS command window (Start->Run and enter
Cmd on Windows XP or command on Windows 98), and do the following steps for each of the modules. Make sure you install
the modules in the order in which they appear in the list in section 2.
First, navigate to the folder containing the module files (using the
cd command).
Next, type the following commands and wait for each to execute:
perl Makefile.pl
nmake
nmake test
nmake install
Note: You may get some error messages when running the make test and make install commands for PAR. To be honest, I am not quite sure why these pop up, but everything seems to work fine with the modules on my machine, so I haven't worried about it too much.
5. Compile your script!
This is the part you've been waiting for: PAR comes with a utility called pp (the Perl Packager).
You can view the full documentation here. You can also view the documentation by
typing the following at a command prompt: perldoc pp
The usage is actually pretty self-explanatory, so I will not repeat the documentation here, but let's take a look at a few examples just for kicks. Imagine you have a really simple 'Hello World' script:
#!/usr/bin/perl
#this script is named hello.pl
print "hello world!n";
To compile it as an executable called 'helloWin.exe', you would do: pp -o helloWin.exe hello.pl
You can even use your own icon: pp --icon iconfile.ico -o helloWin.exe hello.pl
You can keep the exe from spawning a terminal window like this: pp --gui -o helloWin.exe hello.pl
Or use it all together:
pp --gui --icon iconfile.ico -o helloWin.exe hello.pl
There is quite a bit more that pp can do, but that should at least get you started -- see the
full documentation (perldoc pp
from the command line) for all the details.

Got a technical article or tutorial you want to publish on the Internet? Join
in the Round Table Forum and let the Mentors know what you have. If it meets ERT standards, is factual and can help ERT visitors, then ERT Mentors and Editors can help you (without charge) polish your offering so it can be published and promoted by ERT. An article published on ERT may be read by as many as 10,000 visitors a week; promoting you, your site, and your ideas. Please note ERT does not publish re-prints; promotional handouts, or pieces consisting mainly of links. So original technical content only please. If you prefer you can email the Editor
