Perl across OS' (The Shebang)

The shebang line is the first line of the Perl script denoted by the #! which is the first two characters on the first line of the program which specifies the path to the script's interpreter, in the case of Perl it specifies the path to the Perl executable (interpreter). Perl has been successfully ported to Windows and is available in various flavors, e.g. Vanilla Perl, Strawberry Perl, Chocolate Perl and ActiveState Perl. This is the necessary newbie intro, now for the actual topic...

The shebang line really presents a problem when developers using Windows collaborate on scripts with non-Windows developers. All Perl scripts that are executed indirectly will (should) have a shebang line. Most Perl hackers code in a Linux (-like) environment which means their shebang line will look something like this:

#!/usr/bin/perl
#!/usr/bin/env perl

Windows Perl hackers shebang line would look something like this:

#!C:\Perl\bin\perl.exe
#!C:\strawberry\perl\bin\perl.exe

As you can see, this would present a problem if the Linux Perl hacker sent me (using Windows) the script, or more commonly, I fetched the script from a version control repository, updated the script using my shebang, made some modifications, then pushed the changes back to the repo only for the Linux user to have to repeat the same pattern.

The answer is a little know hack for WIndows whereby the path to Perl is mocked under the Windows file system by copying the Perl installation to a `usr` folder in the root drive's folder, e.g. copy C:\Perl to C:\usr:

Now, #!/usr/bin/perl in the script is treated the same as #!C:/usr/bin/perl.exe. The only problem with this is that when modules are updated or installed using CPAN, the copied instance of Perl is not updated, so instead of re-copying your Perl installation each time you update or install a new module, instead make a symbolic link. Wait, how do you create a symbolic link with Windows, why the same way you do in Linux, with one exception. First you need GNUWin32 (http://gnuwin32.sourceforge.net/), this will give you tools like grep, find, ls and what we want, ln.

cd C:\
ln -s C:\strawberry\perl\ usr

We just created a symbolic link to our Strawberry Perl installation. With the aid of GNUWin32, we now have a psuedo-Linux-like Perl programming environment armed and ready.

2 Comments

The problem is even worse than you imagine. Move your program between various machines of the same architecture but with perl installed in different places and you see the same thing. The shebang might not point to the perl that you want or need.

However, if you install your scripts with the standard distribution tools, your shebang line is automatically adjusted to be the right thing based on the perl you used to install it. :)

You would probably want to make a NTFS Junction rather than a symlink in Windows. It will make both C:\Perl and C:\usr look identical without applications needing to be symlink-aware. They can easily be created with the junction utility (http://technet.microsoft.com/en-us/sysinternals/bb896768.aspx).

Leave a comment

About awnstudio

user-pic Al Newkirk is a web application developer from Philadelphia, PA in the United States who specializes in Perl development with MySQL on a Windows platform using Strawberry Perl. I do a bunch of other stuff too, stick around and find out what!