VB vs PERL

I have had to support VB Script for a while now. While I would rather not use it, I really don’t have many options. It is needed to run actions on Windows servers and desktops, it is readily supported out of the box, and it kinda works.

Recently, we needed a script that would run at startup that would replace a configuration line in a settings file with an updated line. Basically, we needed to search the file for a the string that stated that the line was a certain config parameter, then replace that entire line with the new version of that parameter. Since this is running on everyone’s desktops, I would have to write it in VB Script.

Let me tell you, this is no easy task. I finally found a way to do it, here is the code for it.

strComputer="."

Set RegularExpressionObject = New RegExp
With RegularExpressionObject
  .Pattern = "StringToLookFor"
  .IgnoreCase = False
  .Global = True
End With

strReplace = "StringToInsert"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\SomeFile.txt",1)

Do Until objFile.AtEndOfStream
  strLineText = objFile.Readline
  expressionmatch = RegularExpressionObject.Test(strLineText)
  If expressionmatch Then
    strNewText = strNewText & strReplace & vbCrLf
  Else
    strNewText = strNewText & strLineText & vbCrLf
  End If
  Set expressionmatch = Nothing
Loop

objFile.Close

Set objFile = objFSO.OpenTextFile("C:\SomeFile.txt",2)
objFile.WriteLine strNewText
objFile.Close

Set objFile = Nothing

That was not the easiest thing to figure out. Just for fun, I wrote the same thing in PERL.

open (TXTFILE, "C:\SomeFile.txt") || die "Read error for $!\n";

while ($temp_line = ) {
	chomp ($temp_line);
	if ($temp_line =~ /.*(StringToLookFor).*/) 
	{ $txt_line = "StringToInsert"; } else
	{ $txt_line = $temp_line; }
	$output .= $txt_line . "\n";
}

close (TXTFILE);

open (TXTFILE, ">C:\SomeFile.txt") || die "Write error for $!\n";
print TXTFILE $output;
close (TXTFILE);

exit (0); 

Without blank lines for spacing, or any comments, there are 25 lines of VB Script and 12 for PERL. Now, I did cheat a little with the PERL script. I usually like the make it a little neater, and using strict variable declarations. Switching to strict variables adds 5 lines, and then my usual layout adds another 4, for a total of 21 lines.

Now, I know that comparing raw lines of code is not a valid comparison of two languages. So what is? I like to look at how, in this case, each handles Regular Expressions.

VB: You have to declare an object as a regular expression object. Then, you define each aspect of it, and then you have to call it with .Test(variable) to test against the string in the variable. Don’t get me started on that damn Hungarian Notation of putting str before every string, or obj before objects. Oddly, I couldn’t find the notation for a regular expression object.

PERL: You simply call the variable with a =~, then you can use // to basic pattern match. If you wanted to actually replace the string in that line, you could use tr///, or if you wanted to just return part of the original string, s//.

Actually, the calls to open and close to files are very similar in length. Therefore, the real difference is the code to do the string comparison. It is just weirdly convoluted in VB. Not ass backwards, just convoluted. Hell, ass backwards would have been bad enough, the convoluted just makes it weird.

Advertisement