Perl for Win32 takes a quantum leap forward

Quantum leaps of functionality in well-established technologies, especially programming languages, aren't common. However, Perl Resource Kit--Win32 Edition is a quantum leap for Perl on the Win32 platform. The Perl resource kit is a product of O'Reilly & Associates, but ActiveState Tool contributed the new Win32 Perl software on the kit's CD-ROM. The resource kit contains advances in functionality that will significantly affect the Windows NT and Perl communities. These advancements include a new release of Perl, new tools and utilities, and extensive documentation.

New Release of Perl
The most significant advancement in the Perl resource kit is the release of Perl 5.005. This version consolidates the two most popular Win32 builds of Perl: CORE and Perl for Win32. Before version 5.005, users chose between these Win32 Perl implementations because the two builds offered different functions in some areas and implemented the same functions differently in others. Their disparity resulted in code that ran under one build but not the other. Version 5.005 effectively merges the two builds into one comprehensive package that eliminates past incompatibilities.

Fortunately, there are only minor code incompatibilities between version 5.005 and previous versions of CORE and Perl for Win32. The area primarily affected deals with Object Linking and Embedding (OLE) support. People who used ActiveState's OLE implementation will need to change the way they instantiate and access objects. This incompatibility and the other minor incompatibilities are well documented in the resource kit's release notes.

New Tools and Utilities
Perl 5.005 isn't the only software in the Perl resource kit. The kit also includes ActiveState's Perl Debugger, Perl for Internet Server API (ISAPI), Perl for Website, PerlScript, and Perl2Exe. Three new commercial software packages that I like are Perl Package Manager (PPM), PerlCOM, and PerlCtrl.

PPM. With PPM, users easily install, manage, and remove add-on Perl packages. Packages are third-party libraries, such as modules and extensions, that extend Perl's capabilities. For example, the Win32::AdminMisc module I discussed in last month's column ("How to Manage Your Enterprise's Passwords the Easy Way," August 1998) is a package.

If you are a long-time Perl user, you probably know about the hassles involved with installing, managing, and removing packages. For example, if you install a new version of Perl, you must verify that the code in your existing modules doesn't break. If problems arise, you must find a module update that is compatible with the new version of Perl. If an update module isn't available, you remove the Perl upgrade and wait until the module developer releases an updated version. Another problem users encounter when installing packages is that some packages require numerous installation steps, including a compilation phase that is beyond the scope of many Perl users.

With PPM, configuring third-party Perl packages is a snap. PPM comes in two versions: a console-based version, which retains the name PPM, and a browser-based version, Visual Package Manager (VPM). PPM and VPM have three primary functions: Install, which installs a new package from a specified repository URL; Remove, which removes a currently installed package; and Verify, which verifies whether an installed package is up-to-date and optionally updates the package if it's not.

Installing packages with VPM takes only a few mouse clicks. You select the URL repository, select which packages you want to download, and click Install, as Screen 1, page 196, shows.

Only time will tell how useful VPM and PPM will be. However, my prediction is that these two management tools will reduce the complexity and frustration with using third-party packages.

PerlCOM. With PerlCOM, you create instances of Perl and Perl modules from within any programming environment that supports Microsoft's component object model (COM), including Windows Scripting Host (WSH), Visual Basic (VB), Visual Basic for Applications (VBA), and Delphi. A PerlCOM.Script object makes PerlCOM's magic possible.

PerlCtrl. With PerlCtrl, you build COM automation servers in Perl. A COM automation server is a self-contained reusable .dll file that exposes methods and properties that any COM controller can instantiate and use.

With the Perl resource kit, using third-party packages is a snap.
The difference between PerlCOM and PerlCtrl is that PerlCOM is a COM automation server that exposes the PerlCOM.Script object and associated methods. PerlCtrl lets you build your own COM servers complete with their unique GUIDs and ProgIDs. ProgIDs are the friendly names you use to refer to an object class. For example, PerlCOM.Script and WScript.Shell are ProgIDs. Let's look at a PerlCOM example. I'll demonstrate PerlCtrl in a future column.

PerlCOM in Action
Suppose you want to use WSH with Visual Basic Script (VBScript) to execute an external command and capture the command's standard output. Although you can use WSH's Run method via the Shell object to execute the command, no simple method exists to capture the command's output. Instead, you must redirect the command's output to a file, open the file, parse it, close it, and finally delete the file.

Executing a command and capturing its output are much easier with the Perl resource kit because you can execute the Perl code via the PerlCOM object. The example BackTicks.vbs script in Listing 1 shows you how to create and use a PerlCOM object. Let's examine how this script works.

Like other objects used from within a WSH script, you first need to create an object of type "PerlCOM.Script" to execute Perl code. At callout A in Listing 1, the PerlCOM object is objPerlCOM. You use the EvalScript method to execute standard Perl code. With EvalScript, you can create Perl variables that become properties of the PerlCOM object, or define Perl subroutines that become methods of the object.

After creating objPerlCOM, the script creates a new scalar variable (sProperty) and initializes it with the string to the right of the assignment operator (=). You must enclose the Perl code that you want to pass to EvalScript in double quotes. The script then uses the standard VBScript MsgBox function and dot notation to access and display the property's value.

The code at B uses EvalScript to define the Perl subroutine, vbsBackTicks. This subroutine becomes the method that executes the external command and returns the command's standard output as a string. You must enclose the Perl code that you pass to EvalScript in double quotes. When the code spans several lines, you must use VBScript's line continuation character (_) and string concatenation character (&) to ensure that the script passes the entire subroutine to EvalScript as one string.

Like sProperty, the vbsBackTicks subroutine is basic Perl code. The subroutine begins by initializing the two local scalars, $command and $delimiter. (When the script later invokes vbsBackTicks, the script passes these values to the subroutine.) The next line of the subroutine executes the user-defined command using Perl's backtick operator (`) and assigns the command's output to the @output array. The subroutine's last line uses Perl's join operator and the user-defined delimiter to translate the array into a string and return the string to the calling object.

The next line in the script calls the vbsBackTicks method and passes the desired command ("net use") and delimiter ("@") to the vbsBackTicks method. The variable sOutput stores the value vbsBackTicks returns. The script uses VBScript's MsgBox function to display the output. The last line in the script uses VBScript's Nothing keyword to destroy the objPerlCOM object before the script ends.

After you run the BackTicks.vbs script, you will see the message boxes in Screen 2 and Screen 3. But PerlCOM's magic doesn't end here. You can use PerlCOM to create instances of Perl modules. With PerlCOM, you can extend Perl modules to any COM programming environment. For example, WSH doesn't provide a mechanism to read from or write to NT's event logs. You can use PerlCOM to create an instance of the Win32::EventLog module, making its functions available to WSH.

Extensive Documentation
The Perl resource kit features four volumes of documentation, totaling more than 1500 pages. Whether you're a novice or expert Perl programmer, you'll find the information in the four volumes of documentation useful.

The first volume is the Perl Utilities Guide, which covers the Perl Resource Kit's installation and setup, Perl language basics, and Perl programming for Web servers (including information specific to all major Web server platforms). This volume also contains the documentation for all the software included in the kit. The second module is Programming with Perl Modules, which teaches you how to leverage the hundreds of reusable Perl modules available from the Comprehensive Perl Archive Network (CPAN) at http://www.perl.com/CPAN. Programming with Perl Modules also teaches you how to create modules. The third and fourth modules constitute the Perl Module Reference. This two-volume set includes a comprehensive module index from which you can easily locate detailed module descriptions and usage instructions for hundreds of Perl modules.

A Significant Event
If you wrote BASIC programs in the late 1980s, you'll probably agree that what Microsoft has done to evolve the BASIC language is remarkable. You can say the same for what O'Reilly, ActiveState, and the Perl developer community have done for Perl. With a common code base in Perl 5.005, new utilities, new tools, and extensive documentation, Perl clearly becomes the most comprehensive scripting environment available for the Win32 platform.

You can purchase Perl Resource Kit--Win32 Edition from many retail bookstores. You can also purchase it online from O'Reilly & Associates (http://www.ora.com) and various online bookstores.