Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] What is the most appropriate scripting language



Ian MacLean writes:

 > exactly :
 > 
 > import re
 > p = re.compile('[a-z]+')
 > if p.match("some text"):
 >    do_somthing()
 > 
 > in python vs :
 > 
 > if ( "some text" =~ /[a-z]+/ ) {
 >     do_somthing();
 > }
 > in perl.

That's not quite right.  The equivalent Python would be

if match ('[a-z]+', 'some text'):
    do_somthing ()

(it's unfair to burden the Python idiom with the import statement;
that's once per module overhead).  What you can't do with Python
"match" that you can do with Perl "=~" is to access match contents.

To be pedantic, this is less an issue with Python regexps than with
Guido's dislike for assignment expressions.  (He's even on record as
saying that introducing the "+=" idiom was a mistake!)  If he'd back
down on that, then you could do

if (m = match ('[a-z]+', 'some text')):
    do_somthing (m)

which isn't bad at all.  In fact, after many years of writing (more
usually, forgetting to write) stuff like

(progn
  (re-search-forward "[a-z]+")
  (save-match-data                ;; an easy-to-forget PITA
                                  ;; especially if hidden in somebody
                                  ;; else's code!
    (re-search-forward "[0-9]+")
    (insert (match-string 0)))
  (insert (match-string 0)))

I've come to the conclusion that I *want* explicit match objects.

YMMV, of course.



Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links