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: