VBScript and Expression Processing

I’ve bitched about VBScript more than once, but I don’t think I mentioned this little gem. Apparently when it evaluates an expression it evaluates it completely, regardless of if it needs to be. For example, in Perl I can do this:

if( $foo and $foo->func() = 42 ) { … }

Which says to see if the variable $foo is defined, and if it is, call it’s function func() and compare it to something. Perl will not go farther than checking if $foo is defined because if it isn’t, the expression cannot possibly be true.

VBScript on the other hand, checks the entire expression before evaluating it. IE:

If Not IsNull(Foo) And CLng(Foo) = 42 Then

End If

Basically the same code, but this will barf (at least on the server I’m working on’s setup) because if the variable Foo isn’t defined it can’t run the CLng() function on it. So the above has to be written as:

If Not IsNull(Foo) Then
If CLng(Foo) = 42 Then

End If
End If

You can play the home Perl version with the following shell script:

$ perl -e ‘if( $foo and $foo->func() ) { print “hello world 1\n”; }
$ perl -e ‘if( not $foo and $foo->func() ) { print “hello world 2\n”; }

The second line is there to show the failure (Can’t call method “func” on an undefined value) if the second part of the expression was ever reached.

As an aside, the second link is actually helping people. How messed up is that. It’s also ranking #1 in google for “vg scroll mouse editor” and in the top ten for just vb scroll mouse“. Trippy.

One Comment on “VBScript and Expression Processing”

  1. Please don’t remind me of the pain… I just managed to delegate the VB code I was doing to someone else! 🙂
    Of course, I’m shooting myself by doing the R&D necessary to develop some MSMQ/Visio integrations, but that’s a bit more fun then vb stuff.
    I still can’t get over how poor the text formatting/parsing is in VB. I feel like I’m forever inventing my own functions to do it. http://www.nyetwork.org/wiki/VisualBasic