Sunday, January 4, 2009

Script Code: Comparing Kix and VBScript (part 3)

This post is going to be more "general" in nature than the previous two.  Rather than diving into more minutae, I wanted to skim over the top and just highlight some of the basic differences.

In many respects, KixTart and VBScript are much alike.  They both:
  • Use dedicated file types to contain script code (.kix or .udf for KixTart.  .vbs, .wsf for VBscript)
  • Use a dedicated script engine to execute code (kix32.exe or cscript.exe/wscript.exe)
  • Work fine with COM and WMI interfaces
  • Support Array constructs
  • Work with reading/writing and managing files and folders
  • Provide access to the registry and event logs
  • Provide standard conditional branching constructs (if, else, while, for, ...)
  • Provide some GUI utilities (MsgBox, InputBox*)
The most noticable difference are actually rather minor.  There are more significant differences, but they don't usually smack you in the face until you start working with "the other" more in-depth.  Among the immediate differences:
  • Kix prefixes variables with $ (as do other languages like PowerShell)
  • There is not "Then" in Kix (see IF/ELSE example below)
  • Kix supports runtime string expansion-and-evaluation (see EXPANSION example below)
  • VBScript runs in one two modes: GUI or CUI (wscript and cscript, respectively), whereas KixTart uses the same engine for either mode (the script code determines how it will behave)
  • Kix terminating statements are often combined.  So "End If" is "EndIf", "End Function" is "EndFunction".
  • There is no "Sub" in Kix.  Only "Function" constructs.  You control the behavior by coding to return a value or not.
  • Kix supports single or multi-line (aka "block") commenting.  Using either /* and */ for blocks, or semi-colon for single line.  VBscript only supports single-line comments (using a single-quote)
  • Kix allows you to "Include" another script file at runtime or during precompilation, allowing you to separate code into files for greater efficiency.
  • Display output is done using a question mark (?), where VBscript uses "Wscript.Echo"
Kix IF/ELSE Example:

    If $something = 0
        ? "something equals 0"
        ? "something does not equal 0"

Kix EXPANSION Example:

    $employee = "John Doe"
    ? "The employee is named $employee"

    (note: Use the SETOPTION NoVarsInStrings function to enable/disable this behavior)

Comparison of Multiple Branch Condition Evaluations:

    Select Case strColor
        Case "red":
            wscript.echo "The color is RED"
        Case "blue":
            wscript.echo "The color is BLUE"
        Case Else:
            wscript.echo "The color is something else"
    End Select

        Case $strColor = "red"
            ? "The color is RED"
        Case $strColor = "blue"
            ? "The color is BLUE"
        Case 1
            ? "The color is something else"

One thing I (personally) like about the above KixTart example is that each condition is independent.  So, I could have added another condition such as "Case $intColorNum = 256".  This provides enormous flexibility in some cases.  Other languages support this as well, such as LISP (although LISP goes further with native support for in-line evaluation and assignment within each independent expression).

Function Definitions
    ' add two numbers and return a result
    Function AddNumbers (intNum1intNum2)
        AddNumbers intNum1 + intNum2
    End Function

    ; add two numbers and return a result
    Function AddNumbers ($intNum1$intNum2)
        $AddNumbers = $intNum1 $intNum2

There's indeed much more to cover and I might do a few more of these.  I've also thought about comparing KixTart and PowerShell, but that would really be apples-vs-oranges.  Somewhat like comparing Ford cars to Bayliner boats, I suppose.  Oh well, I hope this helps someone, somehow.

* For more GUI features, check out KixForms as well.
Post a Comment