More Alternative Languages for WSH
(Page 1 of 5 )
In my last article I began showing you alternative languages that are compatible with WSH. In this installment I’m going to profile a few more alternatives and show you how to simplify code execution when running scripts in the Windows Script Host environment.
Next to Python, Perl is probably the most widely used WSH alternative. When Larry Wall first introduced Perl in 1987, I doubt he had any idea how quickly it would grow or the impact that it would have on the programming community.
Perl was originally designed for text manipulation but has become quite a favorite for system administration and web development. Perl remains a favorite of web developers and is probably the most common language used in CGI programs. Since Perl’s conception, preprocessing languages such as ASP and PHP have emerged, but many web developers still hold on to their roots.
Perl Its widespread use and availability has made Perl a popular alternative for WSH as well. The language, while not always pretty to read, presents a fine mix of both power and efficiency. While Perl is used mostly by the Linux community, free Windows distributions such as ActiveState’s ActivePerl have helped to bring its popularity to a new platform. ActiveState’s installer also adds support for the WSH environment.
cscript //E:Perlscript myscript.pl
While WSH support is added by the installer, you will need to specify the scripting engine to use when running your scripts in the WSH environment using the command line above. You may wish to simplify this process by using the tip found at the end of this article.
$fso = $WScript->CreateObject("Scripting.FileSystemObject");
Using Perlscript is pretty straightforward. You’ll need to use the WScript object’s CreateObject method to connect to OLE and ActiveX objects. Notice the -> syntax instead of the object dot syntax you’re probably used to seeing.
$files = $fso->GetFolder("C: emp")->Files;
Here again you can see Perl’s syntax as we use the FileSystemObject to return a collection of File objects for a specified folder.
Working with Collections
Perl provides two different ways of working with collections. I’ll demonstrate both of them for you.
foreach $file (in $files) {
print $file->{Name}, "n";
}
The simplest of the two methods uses a foreach structure. This method allows Perl to access the collection objects directly, as in VBScript. This example iterates through the collection and prints the name of each file back to the screen.
$object->{property}
Notice the special syntax that is used to return a property value. Because of this syntax difference, you must take special care to read the documentation for whatever object you use so that you can properly differentiate between its methods and properties.
$object->{property} = newvalue;
The same syntax structure is used to assign a new value to a property as well.
$enum = Win32::OLE::Enum->new($files);
The second method of working with a collection makes use of an enumerator object as in Jscript.
while (defined($file = $enum->Next)) {
print $file->{Name}, "n";
}
A While loop can then be used to iterate through the collection. Unless you have a specific need to use an enumerator, you’re probably better off sticking to the first, and simpler method.
Additional Notes About Perl
Active Perl creates two file associations for Perl. The .pl file extension is associated with perl.exe and the .pls (Perlscript) file extension is associated with WSH.
You should also avoid using the print command when running scripts under Wscript.exe. The print command writes to the StdOut stream, which is unavailable. You should use the WScript object’s Echo method instead.
You will also not be able to use familiar command line switches in WSH. You will need to set these option values in the script as demonstrated in the solution below.
By default, whenever you misspell or make reference to an object’s property or method that does not exist, Perl will not generate an error. This can make debugging quite cumbersome.
$^W = 1;
As author Brian Knittel points out in his book Windows XP Under the Hood, you can overcome this pitfall by including the line above at the beginning of your script.
Be sure to check out the link above to read my review of the book and read its profile on Amazon.com!
Finally, any command line arguments you provide when launching your Perl script in WSH will not be placed into the ARGV array. You will need to fetch these from the WScript object’s Arguments collection instead.
Next: Object Rexx >>
More Windows Scripting Articles
More By Nilpo