|Perl Monk, Perl Meditation|
Note-- Not everyone thinks that packing up perl applications into executables is a good idea. I am aware of the drawbacks (and do mention them, see "caveats"), but think there are some cases where it is useful. After fiddling around with App::Packer, I thought I'd share a step-by-step tutorial for those who need it.
Have you had .EXE-envy? That is, have you ever wished it would be as easy to ship your perl applications as it is with C/C++ or VisualBasic programs, without having to make sure perl is installed on the user's computer? If yes, you probably heard of perl2exe. And probably were discouraged by the steep price tag. Mattia Barbon created a wonderful set of modules distributed as App::Packer that can do the same thing for you. For free, and with source included.
This tutorial is geared towards not-so-experienced users of perl on Win32 platforms. Much of it should apply for Linux/Unix as well.
App::Packer is not a compiler. It merely packs up your script as well as all the modules needed in a nice bundle. This means:
For some applications, it might be much more practical to create a perl "distribution" on CD containing ActivePerl or IndigoPerl and the additional modules you are using, together with your application.
First, do a binary install using ppm:
ppm install http://wwwstud.dsi.unive.it/~mbarbon/p2e/App-Packer.ppd
Additionally, you need a modified version of Module::Info, available from http://wwwstud.dsi.unive.it/~mbarbon/p2e/. Install it using the usual procedure:
If you don't have nmake yet, get it by downloading nmake15.exe.
Creating an executable
There is a script p2e.pl supplied with App::Packer, which should be inside the directory perl\site\lib\App. You might want to copy it to another directory for your convenience. The first step to create an executable is
perl p2e.pl myapp.pl -o myapp.exe
If your script is using a lot of modules, this might take a while, because
App::Packer needs to find out the dependencies between the modules used, so if your script is using a lot of modules, this might take a while. This is especially true for large GUI packages like Wx. Future calls of p2e.pl will be much faster because the information about the modules analyzed is stored in a cache.
Something's missing... (1)
When starting the newly generated program, you might encounter a Windows error message about missing library files. This is probably because you are using a XS module. On Win32, perl stores the binary code in XS modules as DLL files inside your module paths. Unfortunately, Windows doesn't tell you the exact name of the DLL. If you have no idea which file is missing, get Dependency Walker. Run it, drag'n'drop your executable on it and select Profile|Start Profiling from the menu. After a while, your program will abort and Dependency Walker displays the name of the DLL that is missing. Go to your perl directory, search it for that DLL (usually it is somewhere in perl\site\lib\auto) and copy it over into your executable's directory. If your PATH variable doesn't include your perl directory, you will also need to copy the perl DLL (usually called something like perl\bin\perl56.dll).
Something's missing... (2)
Having done that, you might still encounter perl error messages that tell you that a certain module could not be found. Sometimes modules use other modules in weird ways that App::Packer doesn't understand. For example, XML::Parser uses File::Spec. In this case, call p2e.pl as follows:
perl p2e.pl myapp.pl -o myapp.exe --add-module File::Spec
You can use --add-module several times to add several modules.
Shipping your application
When sharing your application with others, make sure you include all the DLL files needed. Even if you do not use any other module and your executable runs "out of the box" on your computer, you will still need to include the perl DLL as mentined above, or else others will not be able to execute your application.
The finishing touches
The created executable is a console application, so you will still see a console window after you started it. To prevent this, you need to change the executable type. Download the source code for exetype.pl, then do a
perl exetype.pl myapp.exe WINDOWS
More information about App::Packer can be found on CPAN. If you like it, consider sending Mattia Barbon an e-mail to praise him for his work. If you have any amendments or corrections for this tutorial, please drop me a line. Thanks to Valdez for telling me about App::Packer.
Large apps can take quite a while to start up, e.g. when using Wx, XML::Parser etc. Small apps tend to start very quickly. A "Hello world" can amount to ~15 KB (+ perl DLL) and run faster than you can blink, so don't hesitate to use this approach for small utilities, designed to be run e.g. on Windows startup.
EDIT: added readmore tag, edited final thoughts