Python vs. Perl, which is better to learn?

Andrew Dalke dalke at dalkescientific.com
Wed May 1 02:32:12 EDT 2002


Christopher Browne:
>Well, in Perl, [regular expressions are] "first class objects," as
>it were, requiring no extra references to functions to get them to
>function, as a base part of the language syntax.

He-he.  I remember once posting the following

$patterns = '';
sub add_pattern {
  my($pat, $name) = @_;
  $patterns .= "if (\$str =~ /$pat/) { \$count++;
                    print 'Found $name\n'; }\n";
}
sub search {
  my($str) = $_[0];
  my($count) = 0;
  eval $patterns;
  return $count;
}


because I learned Perl in the 4.036 days and that was the only way
to make a regexp pattern on the fly.  Nowadays there's the qr() and
other command (I've completely forgotten them by now).  Anyway, after
posting it someone else said:
] the technique of building a regex sub on the fly and eval'ing it is very
] old and was the standard way to test multiple regexes with decent
] efficiency. it is not well know by newbies but most perl hackers had it
] in their idiom toolbox.

which made me feel pretty ancient.

So in my way of viewing, Perl regexps are fundamental data types
but *were* second-class objects, in that it was impossible to have an
array of regular expressions.  I think you mean the first of these
and not the second.  Now I think Perl regexps are both fundamental
and first class.

I do love that Python's regexp patterns are parsed in Python, which
has let me play around with modified regexp patterns.  I also love
that Python's module approach lets it handle at least three different
regular expression libraries in a similar fashion (old-style regexp,
new-style re/src, and posix).

If you really want, there's a 403 article thread from late 1998 titled
  Perl & Java - differences and uses
which contains my thoughts on this matters.

>Mind you, I have found that the "More Pythonic Way" of constructing
>complex regexes by building components, like the following, to have
>merit:
>
>  digit = '[0-9]'
>  date = '\(' + digit + digit + digit + digit + '/' + digit + digit + '/'
>  date = date + digit + digit + '\)'

I've tried doing this, but it's almost impossible to find mismatched
parens.  Instead, consider something like Greg Ewing's Plex, which
uses functions to build up these terms, and let's Python's parsing
catch some of the problems.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list