Is Perl *that* good?

Carl Banks imbosol at aerojockey.invalid
Tue Apr 27 22:19:35 EDT 2004


Roy Smith wrote:
> In article <ECCjc.30580$Vp5.25085 at fe2.columbus.rr.com>,
> Carl Banks <imbosol at aerojockey.invalid> wrote:
> 
>> It's worse because, unlike most objects, regexp objects are usually
>> global (at least they are when I use them).  Moreover, the library
>> encourages us to make regexp objects global by exposing the regexp
>> compiler.  So even if you personally use local regexps (and accept the
>> resulting performance hit), many will declare them global.
> 
> I don't see why regexps are usually global.  Nor do I see why exposing 
> the compiler encourages them to be global, or why making them local 
> should result in a performance hit.

Because if regexp objects are local, they have to be recompiled every
time you call the function.  If you're doing that, you could be taking
a performance hit.  I guess it depends on your functional style,
though.  If your scripts have only one or two functions where all the
regexps are and it only gets called a few times, then it probably
won't matter too much to you.

If you do stuff recursively (as I often do) or break up code into
smaller functions (as I often do) so that the functions with these
regexps get called numerous times, it can help performance to move the
compile step out of the functions.

The point is, the existence re.compile encourages people to make
regexp objects global so they only need to be compiled once, when the
module is loaded.  Because of this, and especially because regexps are
prone to being used in recursive functions and such, it's dangerous to
allow them to have state.



> I do a lot of regex work.   I just looked over a bunch of scripts I 
> happen to have handy and only found one where I used global regexp 
> objects.  In that script, the regexps were only used in a single 
> routine, so moving them down in scope to be local to that routine would 
> have made more sense anyway.  Looking back at the code, which I wrote 
> several years ago, I have no idea why I decided to make them global.

Well, that's fine.  For the reasons I've stated, I think there are
good reasons to not do it the way you did it.


-- 
CARL BANKS                      http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work." 
          -- Parody of Mr. T from a Robert Smigel Cartoon



More information about the Python-list mailing list