Python vs Perl (an example)

Ville Vainio ville at spammers.com
Thu Apr 1 00:48:09 EST 2004


>>>>> "Nick" == Nick Monyatovsky <mon-at-cox-net> writes:

    Nick> 2. There are many things which are very easy to do in Perl, and
    Nick> are very cumbersome in Python. Regular expressions is a
    Nick> vivid example. Saying very generically that Python is easier
    Nick> than Perl is just an invitation for flame wars.

Saying very generically that Python regexps are more cumbersome than
Perl one is just an invitation for flame wars. I have heard that
statement quite often, yet nobody ever bothers to substantiate it.

Hint - you can do

s = re.sub
m = re.match

if the function names are too long for you. And you don't need to
re.compile the regexps beforehand, just pass them as strings to the
functions. And backslash escaping behaviour can be averted by r"raw
strings".

The actual regexp syntax is the same as w/ Perl. I guess that's why
they are often called 'perl-style' regexps.

    Nick> If you'd like to demonstrate that Python is easier than
    Nick> Perl, you'll need to find a better case.

This is c.l.py - I don't think there are many who would claim the
opposite. Actually, I think Python and Perl are so far apart in the
ease of use/complexity/elegance, it's not even funny.

I'll give some comments on the script anyway.

    Nick> my @files;
    Nick> my $count = 0;
    Nick> my $total = 0;

In Python there's no need for 'my' - every assignment makes the
variable local by default. This is a Good Thing, and I doubt you would
contest that.

    Nick> sub retPrevFile 
    Nick> {
    Nick> my $index = shift;

In Py, you just list the arguments in signature.

    Nick> mkdir "$dirName/$slideName" or die;

This idiom is not necessary in Py. You just don't handle errors, the
program terminates w/ traceback automatically.

    Nick> open FILE , ">$file.htm";
    Nick> printf FILE $format, $title, $title, $file, $count + 1, $total,

Here's a clear inconsistency (FILE doesn't have $, others do). Things
like this make a language hard to understand. In Py you just have
objects.

-- 
Ville Vainio   http://tinyurl.com/2prnb



More information about the Python-list mailing list