Switch from perl to python?

Carl Banks imbosol at vt.edu
Sat Dec 14 04:35:34 EST 2002


Erik Lechak wrote:
> Hello all,
> 
> For years I have been using perl as my language of choice for most
> tasks.  And perl has been up to the challenge.  I recently downloaded
> python and started to tinker with it.  I like what I see. I am almost
> convinced to devote the time to make python my language of choice. I
> am about ready to start a new project where both perl and python would
> work.
> 
> Why should I use python rather than perl?
> (I like perl so saying that the syntax of python is superior to perl
> isn't an argument to get me to change.)

(It is, but, besides that,)

Larry Wall originally designed Perl with very little regard for the
principles of good programming.  Perl was a popularity whore; it added
any feature that was cool and time-saving, whether the feature
reflected good practice or not.  And the Perl developers have been
unsuccessfully trying to undo the damage done to it in its early days
since version 5.0.

In short, Perl was an ugly hack, and still is.

Python, in stark contrast, was developed with the principles of good
programming in mind.  Although Python development cycles are very
quick; features get added slowly.  And sometimes, bad features get
removed or fixed.  It takes guts to break backwards compatibility to
fix a language flaw, but Python's done it, and Python's probably the
language that needs it the least.

In short, Python is a well-designed, scalable language.

On to more concrete examples.

In Perl you do this: my ($a, $b, $c).

In Python, you do, um, nothing!  Just use a, b, and c; they're
automatically local.

Good programming practice is to use local variables.  Python makes
variables bound inside a function local by default, and forces the
programmer to declare variables global.  That's how it should be.
Perl does the opposite.

The ironic thing is, Perl claims to be the language of lazy
programmers.  [Warning: Dr. Dobbs QOTW material ahead. :-)] The
reality is, Perl forces the programmer to choose between laziness and
carelessness.  In Python, you can (to a certain extent) be lazy and
not careless.

Another example.  Have a look at the perlsyn manpage gives this an an
example of a "good" way to write a switch statement:

    SWITCH: {
        $abc = 1, last SWITCH  if /^abc/;
        $def = 1, last SWITCH  if /^def/;
        $xyz = 1, last SWITCH  if /^xyz/;
        $nothing = 1;
    }

This a travesty.  And the amazing thing is, the Perl community
supports this kind of crap.  Writing a switch in this way wouldn't
even be possible in Python; the most horrible switch you could write
in Python would probably not look so bad.  If you want to write a
switch, then you use a bunch of ifs:

     if (/^abc/) {
        $abc = 1
     } else if (/^def/) {
        $def = 1
     } else if (/^xyz/) {
        $xyz = 1
     }

You hardly need to glance at the above to know it's a switch; but you
actually have to read and decode the travesty to know what's it's
supposed to do.  The perlsym manpage suggests that this obvious,
readable, and proper way to do it is a "horror".

How do you pass a file handle around in Python?  Have they fixed this
problem yet?  No problem in Python, never has been, because it
occurred to the Python designers that someone might want to do that,
and rightly so.

Can you compose a regular expression programmatically in Perl yet?
Perl is supposed to be strong with regular expressions, yet it seems
that the programmer has no dynamic control over them except with eval.
Python, no problem.  People actually do this, too.

Object orientated programming.  Neither Perl nor Python had it in the
beginning, yet somehow OOP managed to be added to Python seamlessly,
whereas in Perl it pretty much violated every invariant in the system.

There many other examples of such backwardness in Perl; these are some
of the major ones.  All in all, they demonstrate how narrowminded and
poorly designed Perl is compared to Python.


You know, I don't think that I answered your question the way you
wanted me to.  You wanted me to tell you what was good about Python,
but, IMO, the number one reason you should do your project in Python
is that PERL SUCKS.

Note that several years ago, I was like you.  I thought I liked Perl.
Perl was everything cool.  Now, I'm embarrassed to admit I ever liked
it, so awful do I consider it now.  Perspective can do that people.


-- 
CARL BANKS



More information about the Python-list mailing list