Back to Top

Sunday, December 31, 2006

Perl and Windows

Perl is a nice scripting language, but originally it wasn't designed for the Win32 OS. There have been many improvements over time however (the greatest of them all being ActivePerl with PPM, which - as opposed to CPAN - doesn't require you to have all those command line tools which you have on 99.9* on the *NIX systems, but you can only get it on Windows by installing several programs). Here are a few pointers for you if you are trying to make it work on Windows:

There is a dedicated subdomain on perl.org to the Win32 issues

Many Windows specific functions / methods do not set the $! or $^E variable correctly in case of errors, making debugging harder. In this case you can use the following construct:

use Win32::API;

...
warn (Win32::FormatMessage(Win32::GetLastError())) if (Win32::GetLastError());
Just remember to call this as soon as possible after the error, because other API calls may reset the value returned to 0 (which means no error).

If you are using ActivePerl, you should install alternative package sources. A great list can be found on the win32.perl.org wiki.

If you find modules on CPAN which are not present in the PPM repositories, you can try downloading the ZIP containing them and copying the directory structure manually to your Perl installation directory (of course you shouldn't just copy all the files, but try to figure out which file goes where - this process is made easier that the ZIP / TAR.GZ file itself has a tree structure which is similar to the target tree structure). Many times this is all that it required to install a module. If you need a free solution to extract the files, try IZArc (now also with command line).

And one final tip: if you created a multi-threaded script with the threads module and it keeps crashing on you, make sure that all your accesses to shared variables are synchronized with lock. Because there is no explicit unlock command, you can use the fact that lock is block scoped and write the code as follows:

... not  synchronized code ...
{
  lock($shared_variable);
  ... synchronized code ...
}
... not  synchronized code ...

0 comments:

Post a Comment

You can use some HTML tags, such as <b>, <i>, <a>. Comments are moderated, so there will be a delay until the comment appears. However if you comment, I follow.