How to Program with VBScript in a Perl Script

Programming Perl Applications Using Only VBScript Code

© Mark Alexander Bain

Aug 5, 2009
How to Program with VBScript in a Perl Script, Mark Alexander Bain
Today VBScript programmers can become Perl programmers without learning any new code. Well, almost no new code. They will have to learn how to load a module.

In the past there have been Perl programmers and there have been VBScript programmers. Both have always done similar jobs, but they've never really mixed that much. That's because VBScript is exclusively for Windows while, until recently, Perl has been only for Unix and Linux. But that's all changed.

Today, however, the picture is blurred. Now anyone can install Perl on Windows by obtaining Cygwin, ActivePerl or Strawberry Perl, and then they can then create scripts that will run on either Windows or Linux.

The question is then whether to use Perl or VBScript for a new project. The solution is to use both. A VBScript programmer can also use Perl, and they won't even have to learn much new code. All they need to know is how to use a Perl module, and that module is the Inline-WSC module.

Using the Inline-WSC Module

The programmer can create VBScript functions and subroutines that can then be used in the main body of a Perl script. These functions and subroutines are then passed by the module to a Windows Script Component that it creates.. The first step for the programmer is, therefore to load the module:

use Inline::WSC VBScript => <<'EndOfVBScript';
' VBScript functions and subroutines defined here
EndOfVBScript

Any methods must be placed between the "EndOfVBScript" markers shown in the block of code shown above.

Adding VBScript Methods

The programmer must place the VBScript functions and subroutines between markers in the code, for example:

use Inline::WSC VBScript => <<'EndOfVBScript';
' A simple "Hello World" example:
Function helloText
helloText = "Hello World!"
End Function
' These comments are required
Sub hello (ipText)
msgbox ipText
End Sub
' The main body of the code:
Sub main
hello helloText
End Sub
EndOfVBScript

Here a function and two subroutines have been defined:

  • helloText - simply returns some text
  • hello - this is a simple subroutine that displays text in a message box
  • main - this is is used to run all of the other functionality (but it doesn't have to be called "main")

It's worth noting here that the comments are required and the script will abort if they are omitted.

Running the VBScript Code from Perl

If the code is run at this point then nothing will happen. The subroutines must now be called from Perl, for example:

main;

This must be placed after the "EndOfVBScript" marker, and it's worth noting that Perl is case sensitive (something that VBScript programmers are not used to). However, once the programmer has saved the code then it can be run from the command line (as shown in figure 1 at the bottom of this article) and a message box with the words "Hello world" will be displayed.

The VBScript programmer has not written anything new or different here, but what they will have done is written the code so that it can be used seamlessly in a Perl application. And if they save their code as a Perl module then they will have VBScript code that can be used in any Perl based project.


The copyright of the article How to Program with VBScript in a Perl Script in Windows Programming is owned by Mark Alexander Bain. Permission to republish How to Program with VBScript in a Perl Script in print or online must be granted by the author in writing.


How to Program with VBScript in a Perl Script, Mark Alexander Bain
Figure 1: A Perl Script Written in VBScript, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo