Windows Scripting
  Home arrow Windows Scripting arrow More Alternative Languages for WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

More Alternative Languages for WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-08-18

    Table of Contents:
  • More Alternative Languages for WSH
  • Object Rexx
  • KixStart
  • Ruby
  • Tips and Other Languages

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Windows Scripting Articles
    More By Nilpo


       · WSH in not limited to Microsoft's VBScript or JScript languages. In fact, support...
     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT