ActivePerl User Guide

Modules and sample scripts

NAME

ActivePerl-faq9 - Modules and sample scripts

DESCRIPTION

General questions about using modules and scripts with ActivePerl

How can I use modules from CPAN?

As of version 5.005, ActivePerl supports the MakeMaker utility. This allows you to install modules from CPAN, but requires you to have a make utility, such as nmake or dmake. Modules are generally distributed in gzipped tar files, such as Data-Dumper-2.08.tar.gz.

However, ActivePerl includes the Perl Package Manager (PPM), a utility that allows you to install modules, including modules that contain binary extensions. You should consider using PPM to install a module, as this greatly simplifies the management of modules.

Nevertheless, there are times when it is necessary to build a module from source. Typically, an installation session goes something like this:

  1. Extract the module. This creates a directory based on the name of the archive.

        gzip -d -c Data-Dumper-2.08.tar.gz | tar xvf -
    
  2. Change directory to the module's directory.

        cd Data-Dumper-2.08
    
  3. Consult the README file.

        more < README
    
  4. Run the Makefile.PL script. This uses the MakeMaker module to build a makefile you can build the extension with.

        perl Makefile.PL
    
  5. Run your make utility. This prepares the module for installation, and compiles any extension if one is present.

        nmake
    
  6. If this module has tests, run them.

        nmake test
    
  7. If the tests succeeded, install the module.

        nmake install
    

See Where can I find Win32 ports of UNIX tools? for information on the availability of tools like gzip and tar.

Where do I get make for Win32?

Nmake is a 'make' like program for Win32 systems by Microsoft. It is available from ftp://ftp.microsoft.com/Softlib/MSLFILES/nmake15.exe

How do I access databases from my Perl script?

DBI

DBI, a Database Interface Module for Perl provides a consistent interface for database application development. DBI supports ODBC drivers as well as the native APIs of certain databases, including Oracle. The DBI home page is located at:

    http://www.symbolstone.org/technology/perl/DBI/index.html

ADO

The ActiveX Data Objects (ADO) API can be used with the Win32::OLE module. You can use ADO to access any ODBC data source. The Perl-Win32-Database FAQ includes some information on ADO:

    http://www.fastnetltd.ndirect.co.uk/Perl/perl-win32-database.html

The ADO home page is located at:

    http://www.microsoft.com/data/ado/

Win32::ODBC

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

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

Sybase

The Sybperl module allows you to connect to Sybase SQL Server (and possibly Microsoft SQL Server). The Sybperl home page, which includes links to binary releases of Sybperl, is located at:

    http://www.mbay.net/~mpeppler/

If you choose to use a database API that depends on ODBC, you must have an ODBC driver for the DBMS you're using. You will need to configure a data source for that driver on each machine that needs to access the database. For more information on ODBC and how to configure a data source, check the ODBC Control Panel help.

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

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

Yes. See the documentation for the Win32::OLE module, which is included with Perl. You can use Win32::OLE to control a wide range of Win32 applications and APIs, including ADO.

After reading the documentation, you can read the FAQ on using OLE.

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

You should use Win32::OLE to access this API. See question Is there a way to use OLE Automation servers from my Perl script? and consult the DAO documentation.

Can I send Internet mail from ActivePerl?

If you need to send Internet mail, you should use Net::SMTP, Mail::Sender, or Mail::Sendmail. For more information on these modules, see How do I send email from ActivePerl?.

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

You can use the Win32::OLE module to create an instance of a MAPI session and send a message. In order for this to work, you must have messaging configured correctly on your machine. If you are using Microsoft Exchange, chances are this will work fine.

If you need to send Internet mail, you should use Net::SMTP. For more information on Net::SMTP, see How do I send email from ActivePerl?. Here is an example script that sends mail using MAPI:

    # Sender's Name and Password
    #
    my $sender = "YOUR NAME HERE";
    my $passwd = "YOUR PASSWORD HERE";

    # Create a new MAPI Session
    #
    use Win32::OLE;
    $session = Win32::OLE->new("MAPI.Session")
        or die "Could not create a new MAPI Session: $!";

    # Attempt to log on.
    #
    my $err = $session->Logon($sender, $passwd);
    if ($err) {
        die "Logon failed: $!";
    }

    # Add a new message to the Outbox.
    #
    $msg = $session->Outbox->Messages->Add();

    # Add the recipient.
    #
    $rcpt = $msg->Recipients->Add();
    $rcpt->{Name} = "RECIPIENT NAME HERE";
    $rcpt->Resolve();

    # Create a subject and a body.
    #
    $msg->{Subject} = "Test Message";
    $msg->{Text} = <<EOF;
    This is a sample test message.

    Cheers,

    Mr. Email

    EOF

    # Send the message and log off.
    #
    $msg->Update();
    $msg->Send(0, 0, 0);
    $session->Logoff();

Is there a DBM implementation available for ActivePerl?

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

    use SDBM_File;
    use Fcntl;

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

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

    untie( %myhash );

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

Is there a way to do GUI programming with ActivePerl?

As of the Perl Resource Kit for Win32, the Tk library has been successfully ported to ActivePerl. Tk version 8xx and above also now has the look and feel of Windows programs, and not the old UNIX look of previous versions.

You can also use the Win32::GUI module which also allows for GUI's to be made/manipulated using Perl. This module uses the standard Windows widgets to give the look and feel of Windows programs.

For Tk information, go to http://w4.lns.cornell.edu/~pvhp/ptk/ptkTOC.html

For Win32::GUI, go to http://dada.perl.it

Win32::MsgBox allows for a fast and easy way to display a Windows message box, for example:

use Win32;

MsgBox("Test", "This is a test", 48);
# display a message box with an exclamation mark and an 'Ok' button

sub MsgBox {
    my ($caption, $message, $icon_buttons) = @_;
    my @return = qw/- Ok Cancel Abort Retry Ignore Yes No/;
    my $result = Win32::MsgBox($message, $icon_buttons, $caption);
    return $return[$result];
}

The last value, $icon_buttons, is the sum of two values, the value for the icon and the value for the buttons. For example, if you need a message box with a question mark and the 'Ok' and 'Cancel' buttons the value you want is 32 (question mark) + 1 (Ok, Cancel) = 39. The values are listed here:

Icons:
   0 - no icon
  16 - Hand
  32 - Question
  48 - Exclamation
  64 - Asterisk

Buttons:
   0 - Ok
   1 - Ok, Cancel
   2 - Abort, Retry, Ignore
   3 - Yes, No, Cancel
   4 - Yes, No
   5 - Retry, Cancel

Is there a port of Oraperl for Win32?

Oraperl is available as an emulation layer on top of DBD::Oracle. Oraperl is included with the DBD::Oracle distribution. Because the DBI interface is evolving, the Oraperl emulation layer is recommended because its API is fairly stable. Patches are available for DBD::Oracle that allow it to build on Windows. More information is available at:

   http://www.symbolstone.org/technology/perl/DBI/index.html

What modules come with the ActivePerl distribution?

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

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

No longer included in the Win32 module:

Where can I find other modules for ActivePerl?

Modules for ActivePerl 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, huh?)

Is there a GD module available for ActivePerl?

GD now works with Windows, and can be installed using PPM (the Perl Package Manager). This graphics library allows the creation and some manipulation of industry standard PNG format images.

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 http://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 ActivePerl distributions as well as modules and scripts. However, read

    How can I use modules from CPAN?

for more information.

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 Is there a way to use OLE Automation servers from my Perl script? for more information.

As a special case, many database files, like Microsoft Access, FoxPro, dBase or Paradox files, can be accessed using ODBC (Open DataBase Connectivity). See How do I access databases from my Perl script? for details on how to use ODBC with ActivePerl.

AUTHOR AND COPYRIGHT

This FAQ was originally assembled and maintained by Evangelo Prodromou. It has been revised and updated by Brian Jepson of O'Reilly & Associates, David Grove and David Dmytryshyn of ActiveState, Henning Michael Møller-Nielsen of RTO, Kevin Meltzer and David Sparks of ActiveState.

This FAQ is in the public domain. If you use it, however, please ensure that you give credit to the original authors.

 ActivePerl FAQ - Modules and sample scripts