Perl for Win32 Frequently Asked Questions (FAQ)

Main Page

9. Modules/sample scripts

9.1. Many of the scripts/modules in my perl5/Lib directory don't work. Why not?

These modules are distributed AS-IS, without any guarantee to work. There are a couple of reasons why a module won't work:

You can try and fix the module, or get around, or see if someone else has already fixed it. See question 5.8.

9.2. Many of the scripts/modules I got from CPAN don't work. Why not?

See question 9.1. However, if you make a change to a CPAN module that makes it work on Perl for Win32, you have a better chance of getting the author to change the module than you do of getting a change in the standard library.

Note that many of the CPAN modules use the MakeMaker utility to build and install the modules. Since MakeMaker hasn't been ported to Perl for Win32, you'll usually have to move the module files to the Lib directory yourself. Remember to maintain sub-directory trees.

9.3. How do I access databases from my Perl script?

There are a couple of extensions that have been developed to access databases from Perl for Win32. Win32::ODBC is widely popular, and is available on CPAN and at this URL:

http://www.roth.net/odbc/

Another module, ODBCTable, lets you tie an ODBC table to a Perl hash and use it just as you would a hash. It's available at the following URL:

http://www.endcontsw.com/sampledownload.htm

The one you use is a matter of taste. Note that you must have an ODBC (Open DataBase Connectivity) driver for the DBMS you're using in order to use either of these modules. For more information on ODBC and how to configure database, check the ODBC applet in the Control Panel.

Some DBMSs, such as Microsoft Access and Microsoft SQL Server, can be controlled through OLE Automation (see question 9.4). See the product documentation for details.

9.4. Is there a way to use OLE Automation servers from my Perl script?

Yes. You can read all about this on the oleauto page in the Perl for Win32 documentation. There are two examples of using Perl for Win32 to access Microsoft Excel in the eg subdirectory of your perl directory.

Note that this is only OLE Automation, and that you can't do other OLE things like embed documents or provide drag-and-drop, etc. Also, note that, because of a bug in the OLE code, you can't use any OLE server that is implemented as a DLL (they use CLSCTX_LOCAL_SERVER for CoCreateInitialize rather than CLSCTX_SERVER, if that makes any sense to you).

This means that you can't use any OLE Controls (OCXs) or the Data Access Objects library. You can try getting the source code distribution, and patching the code as mentioned above (line 321 of ntole.cpp), but it's untested, so you're on your own.

9.5. Is there a way to access Data Access Objects (DAO) from my Perl script?

Yes and no. See question 9.4.

9.6. Is there a way to access MAPI from my Perl script?

Currently, there's no direct way to access MAPI from Perl scripts. Efforts are underway to create a Perl extension to do simple MAPI functions. Keep an eye on this space for more details.

[Is this right? Are there any MAPI extensions out there? -ESP]

It may be possible to control your mail program through OLE Automation (see question 9.4). Microsoft Exchange is reportedly OLE Automatable.

9.7. How do I send SMTP (Internet) mail from my Perl script?

The current favorite method is to use a program called BLAT from the Windows NT Resource Kit. This is a command line program that sends a file to a recipient. See question 4.7 for details.

There are also Perl scripts that send mail using sockets. Several examples are on Robin Chatterjee's Web page (see question 3.4).

9.8. Is there a DBM implementation available for Perl for Win32?

Yes, there is. SDBM_File is a free clone of DBM, and is implemented and distributed with the Perl for Win32 distribution. You can use it as follows:

    use SDBM_File;
    # use Fcntl; # doesn't work! see question 9.15
    
    sub O_CREAT { 0x0100 }
    sub O_BINARY { 0x8000 }
    sub O_RDWR { 0x0002 }

    tie( %myhash, "SDBM_File", 'myfile', O_RDWR | O_CREAT | O_BINARY,
      0666 )
      or die( "Can't tie: $!" );

    $myhash{"bibim"} = "bap";

    untie( %myhash );

Note that you _must_ use the O_BINARY flag, which is a proprietary flag defined by Microsoft. DBM files are "binary", and although this doesn't make a big difference for UNIX, it matters a lot for Win32.

After the DBM file is tied, you can use it just like any other hash table.

9.9. Is there a GDBM available for Perl for Win32?

No, there is not. You should be able to get scripts that use GDBM to use SDBM by doing a s/GDBM/SDBM/g. However, DBM files and GDBM files are not compatible, so you'll have to do some kind of conversion.

9.10. Is there a way to do GUI programming with Perl for Win32?

So far, not yet. The TK library has been ported to Win32 operating systems, however, so we should expect a perlTK port at some point. If you need it, maybe you should work on porting it. Information on this can be found in the perl/Tk distribution on CPAN.

Another option that some Perl programmers use is to develop front-end GUIs in Visual Basic and control them with OLE Automation.

9.11. Is there a port of oraperl for Win32?

So far, not yet. You can, however, use one of the ODBC modules to connect to an Oracle database. See question 8.3 for details.

9.12. What modules come with the Perl for Win32 distribution?

Along with the standard library files, there are several Win32-specific modules that are distributed with Perl for Win32. These include:

These are documented on the Win32mod documentation page. There are also a number of subs built into the Win32 package, namely:

[Can anyone straighten me out on some of these? -ESP]

More information on the Win32 extensions for perl can be found on Philippe LeBerre's page (http://www.inforoute.cgs.fr/leberre1/).

9.13. Where can I find other modules for Perl for Win32?

Modules for Perl for Win32 are available on CPAN (Comprehensive Perl Archive Network), a Perl archive that's mirrored around the world. To find your nearest mirror, check here:

http://www.perl.com/perl/CPAN/modules/by-module/Win32/

(Note that this will send you to your nearest mirror automagically. Cool, hunh?)

Some modules are also available on the ActiveWare ftp server, but these are rarely maintained, so you get a mixed bag, to say the least:

ftp://ftp.activeware.com/contrib/

9.14. Is there a GD module available for Perl for Win32?

Yes, a port of GD, Win32GD, is available at the following URL:

ftp://ftp.activeware.com/contrib/Win32GD_v960527.zip

[Anyone used this? -ESP]

9.15. Why doesn't Fcntl work?

The Fcntl module (for file control flags) is not included in the binary distributions of Perl for Win32. You need to get the source distribution, and build the Fcntl module yourself.

In addition, there is no supporting Fcntl.pm for the Fcntl.pll that building the module produces. You will need to create your own Fcntl.pm, which is difficult, because it uses Autoloader.

9.16. Why doesn't Win32::NetResource work?

The Win32::NetResource module isn't included in the binary distributions of Perl for Win32. You need to get the source distribution and build the module yourself (see question 1.6).

Note that the Win32::NetResource module doesn't build with all the other Win32 modules [which is probably why someone forgot to put it in the binary distributions... -ESP]. You need to open up the project file and build it separately.

9.17. Why don't File::Find or Cwd work?

[Thanks to Dave Wolfe (dwolfe@risc.sps.mot.com) for this answer. -ESP]

File::Find is the Perl 5 module for directory tree recursion; find.pl and finddepth.pl are the Perl 4 versions of those functions, and have been adapted for use with the Perl for Win32 system. Cwd is used to change the working directory.

The File::Find distributed with build 110 has a number of problems in the way the communication variables (i.e. $dont_use_nlink, $prune, $name, and $dir) are declared and other packages are called.

File::Find also uses return values from the stat command to determine where it is in the directory hierarchy. Since MS file systems don't use the concept of inodes and links to identify files, the "number of links" field (aka nlinks) for a directory has no meaning for them. Later versions of Find.pm use the OS name from Config.pm to decide whether or not to use nlinks.

Finally, File::Find depends on the faulty Cwd.pm.

For build 110, the following procedure will make File::Find and Cwd workable:

    elsif ($osname eq 'MSWin32') {
        *cwd        = \&_NT_cwd;
        *getcwd     = \&_NT_cwd;
        # BEGIN ADDITION: Cwd.pm, line 250
        *fastcwd    = \&_NT_cwd;
        # END ADDITION
        *fastgetcwd = \&_NT_cwd;
    }

Note that errors with File::Find and Cwd are corrected in builds 300 and higher of Perl for Win32.

9.18. What is CPAN and how do I use it?

CPAN is the Comprehensive Perl Archive Network, a collection of pretty much every file you could ever want for Perl programming. The original archive is in Finland, but it is mirrored on FTP servers throughout the world.

You can connect to your "closest" CPAN mirror using the CPAN redirector at www.perl.com. The following URL:

http://www.perl.com/CPAN/

will redirect your browser to the nearest CPAN mirror (one on your continent and maybe only a few hops away). This URL

http://www.perl.com/CPAN

lets you pick a CPAN mirror site yourself, as well as giving a little more information.

CPAN is good for finding Perl for Win32 distributions as well as modules and scripts. However, read question 9.2 for more information.

9.19. Is there a library to read or write Microsoft Office or other application files?

In general, there aren't any library modules to read or write application files, like Microsoft Word, Microsoft Excel, Microsoft Access, WordPerfect, Lotus 1-2-3, etc.

However, many if not most major Windows applications now support OLE Automation. You can use the OLE Automation support of Microsoft Office applications to read and write their file formats, for example. See the documentation on your application for information on its support for OLE Automation. See question 9.4 for how to use OLE Automation with Perl for Win32.

As a special case, many database files, like Microsoft Access, FoxPro, dBase or Paradox files, can be accessed using ODBC (Open DataBase Connectivity). See question 9.3 for details on how to use ODBC with Perl for Win32.