July 2006


Some time ago a friend introduced me to precompiled regular expressions. Now, I have always been an absolute fan of regex–it is one of the cornerstones of Perl. I have written scripts that consisted of hundreds of regexes. But since my introduction to the precompiled regex I am a full time advocate!

The beauty of a precompiled regex is that it is fast. Every time that a regular expression is evaluated it has to be compiled from its text representation into a block of byte code. This byte code is then run and it produces the regex output that we all know and love. The downside is that compiling into byte code takes time. So if you have a script that loops for a long time you will be wasting time compiling the regex over and over again. Depending on the code and the complexity of the regex this time could be significant.

In a precompiled regex you compile the regex and assign it to a scalar just once. Then when you go to use it you will simply execute the precompiled byte code–there is no need to spend time compiling it.

For example:

 

Over in the Forums (http://www.roth.net/forums) a question was asked about converting VBScript into Perl. This reminded me of a question that I am still frequently asked about: how do you use COM objects in Perl? It just so happens that we have a PowerPoint presentation that I gave at the 2000 Perl Conference on this very subject: http://www.roth.net/conference/perl/2000/Perl2000.ppt

In brief, a Perl script can interact with most COM objects. This is done using the Win32::OLE extension which comes as part of Activestate’s Win32 Perl. There is experimental support for COM eventing but it has unfortunately been experimental for several versions now.