Forum Articles Tutorials Scripts LINKS News Mentors TOOLS Register

Compiling Perl Scripts into Standalone Executables

ERT Logo

A tutorial by NicholasSolutions

  1. What, Why and How?
  2. Download the Files
  3. Install nmake
  4. Install the Modules
  5. Compile your script!

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.

Note: While I am assuming you are doing this on a Windows machine, 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


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.


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
Note: The modules on CPAN get updated pretty frequently, so if you want to make sure you have the most recent versions, it's a good idea to follow the Documentation links above, and then follow the Latest Release link on that page. In the upper right, you'll see a link to download the most recent version.

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:

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:

#this script is named
print "hello world!n";

To compile it as an executable called 'helloWin.exe', you would do:
pp -o helloWin.exe
You can even use your own icon:
pp --icon iconfile.ico -o helloWin.exe
You can keep the exe from spawning a terminal window like this:
pp --gui -o helloWin.exe
Or use it all together:
pp --gui --icon iconfile.ico -o helloWin.exe

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.

post to Dzone Digg this! Add to Googleize this Add to reddit Save to myYahoo Add to furl Add to Netvouz! Spurl this! Add to Linkroll! Save to Simpy Give if thumbs up on StumbleUpon Save to Blinklist Add to Tektag Save to Bibsonomy Submit to Tweako
Search ERT on the Tools Page
Did you know? You can discuss this article with the mentor who wrote it and others interested in the topic? You are invited to join the discussion with Go to the forum

Got a technical article or tutorial you want to publish on the Internet? Join Go to the forum 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