Having given both a whirl

29.04.2009 20:02

I'm fluent in both Perl and Python. I've been writing Perl scripts of various sizes since I began using Linux and more recently I've also worked on larger projects using Perl (from G-Tablix to Wikiprep). On the other hand my work at Zemanta has largely been about writing software in Python. Large amounts of complicated, production critical software in fact.

There's this very obvious philosophical difference between these two languages: Perl is all about there is more than one way to do it, while in Python there is (preferably) only One.

Recently I've rewritten software for this blog in Perl. My colleagues were surprised when I mentioned this. Why didn't I do it in Python, when I had plenty of examples at work of how easy it is to set up a web page with Django for example?

Perl and Python

In my experience with both languages the fundamental difference in design philosophy shows in an interesting way: Writing software in Perl is fun by itself. The multiplicity of solutions to a single problem that Perl offers gives you the opportunity to express yourself. You can stop for a moment to contemplate the most elegant way of doing something. It's about writing software in your own, distinct style. To match your thought patterns. Not surprisingly, Perl is often compared to poetry.

Writing Python on the other hand is dull. If you already know in advance the algorithm you are about to write, writing code in Python is pure drudgery. You only have this limited number of valid expressions. You are forced to do indentation just right. Chances are that you'll write exactly the same code as the other guy if he was given the same specification. It's boring because it's simple and straightforward.

You better be discovering something new on the algorithmic level when using Python, or it won't be very enjoyable. On the other hand writing Perl code itself is fun.

Python is for experiments on a logical level. Perl is for experiments on the syntax level.


Then there's the different approach to errors: In majority of cases Perl will do what you want (unless what you want is consistency, as perlfunc(1) author remarks), maybe giving you a warning that you can later research and fix if you're a perfectionist. But in most cases if an undef gets into the stream it's an one-in-a-thousand occurrence that you can safely ignore.

Python on the other will usually throw exceptions in the most unexpected places and a single uncaught exception will kill your program (which was probably 5 minutes away from completing that 100 hour task).

It's the bugs in the code where the situation turns around. Fixing bugs in Python is fun. You get a descriptive back trace by default on all errors, code is easy to read even when it's not yours and it's satisfying to follow that stray None value as it traversed half a dozen modules until it got to some function that actually tried to do arithmetic on it.

On the other hand when something does go seriously wrong in Perl you better be the author of the code you are looking at and be familiar with interpreter's guts. Since functions will silently pass around invalid values and wrongly encoded strings, the root cause of the error you're hunting may be thousands of lines separated from its effects.


Now it's probably clear why I wrote my blogging software in Perl. It's an application that will most likely never be used, maintained or extended by anyone else by me. I was reinventing the wheel. There is no opportunity to be creative with software design of a yet another blog. It's a straightforward job of telling the computer what you want. So at least I was able to get some satisfaction from the act of expressing my wishes by jotting them down in Perl.

Having said that, I still firmly believe Python is way better language for any kind of cooperative work when compared with Perl. Most of software is at one point worked on by more than one person, so on average Python is probably a better bet when starting a project from scratch.

On another thought, even going through your own code a few years later may be considered working together with your younger self. So far I hadn't had any problems understanding my oldest Perl scripts. Then again maybe they're just not old enough.

Posted by Tomaž | Categories: Ideas

Comments

Interesting observations. I don't see Python as dull, but I think you provided my reason very succinctly with: "Python is for experiments on a logical level. Perl is for experiments on the syntax level."

Have you ever given Lisp a try? You sound like someone who would greatly appreciate it.

Simon, I've tried Scheme once, which is a dialect of Lisp. The unit test framework for Tablix is written in it.

What I remember the most from that time is that I had big problems understanding my own code a month after I wrote it. That and the fact that nobody ever contributed any patches for that part.

Of course, it's possible I had problems because I didn't spend enough time with Scheme to really understand it.

Posted by Tomaž

If you want a language for easy object-oriented programming,
or you don't like the Perl ugliness, or you do like the con-
ceptof LISP, but don't like too much parentheses, Ruby may
be the language of your choice.

This is the advice I got from my MSc EE mentor after asking about Perl!
I did few thousand lines of Pascal coding before Windows and internet :-)

Posted by MMM

If you want easy Object Oriented Programming in Perl, it's a *lot* easier than changing languages to Ruby. . . you just use Moose. Now you have what is probably the best OO setup in any language I've ever used (and not just in features, it's also as pretty as any python/ruby anywhere).

You may also want to investigate the "Modern Perl" movement. There's no need to be writing ugly unmaintainable code in Perl. With very little effort, Modern Perl takes all the awesomeness that is perl and swipes a few good bits from Python and Ruby, too. The result is Perl that still rocks the socks off of the alternatives.

While I'm not a "major" programmer, I see that many languages are linear. Get it out of order and it won't work. Perl, on the other hand, is more like, spatial. Stuff can be all over the place and still work, plus there's usually 1001 ways to do the same exact thing.

Well, i'm quite surprise that so many peoples are saying at Perl code is ugly...
I personally found my Perl code quite beautiful.

Posted by LittleBreton

Add a new comment


(No HTML tags allowed. Separate paragraphs with a blank line.)