Is Perl *that* good?

Carl Banks imbosol at aerojockey.invalid
Tue Apr 27 19:26:49 EDT 2004


Paramjit Oberoi wrote:
> 
> 
>>     Asun> Regex is much more 'immediate' in Perl.  
>> 
>> Sure, it's syntactically bound into the language.  There will always be an
>> extra constant overhead to enable regular expressions in Python.  That
> 
> If only compiled regular expression objects allowed easy access to the
> last match object, python regxes would become significantly more
> convenient.  For example, today you have to write:
> 
> m = myregex.match(line)
> if (m):
>    xyz = m.group('xyz')
> 
> It would be much easier to do:
> 
> if myregex.match(line):
>    xyz = myregex.lastm.group('xyz')
> 
> Having to introduce a new variable name for a match object just
> muddles the code unnecessarily in common regex usage scenarios.


Hmm.  The reason this hasn't been done is that it makes the match
method non-reentrant.  For example, suppose you call some functions in
between the matching and use of the matched object, like this:

    if myregex.match(line):
        xyz = (subprocess(line[myregex.lastm.end():]) 
               + myregex.lastm.group(1))

And suppose subprocess goes on to use the same regexp.  By the time
subprocess returns, myregex.lastm could have been overwritten.  This
is not a far-fetched example at all; one could easily encounter this
problem when writing, say, a recursive descent parser.

Murphy's law says that if anything bad can happen, sooner or later it
will, and this is why non-reentrant functions like your proposed
myregex.match are so heinous.  So, I can't agree that this is a good
idea as it stands.



-- 
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