Competition is a common thing is hackerdom, and arguments over which one of two (or more things) is the better are common and keep coming and going (but never really go away). Some of the most common ones are: Java vs C#, Emacs vs Vi (and it’s clones), Linux vs BSD, Closed Source vs Open Source and everyone’s favorite: Microsoft vs everyone else. And the internet is littered with evidence of these great wars (which none too rarely take on epic proportions) and some digging will throw up evidence on forums, website, bulletin boards, Usenet archives and of course, blogs. Today I’m taking a look at one of these wars that I’ve had personal experience with: Python vs Perl.
The last few years of my life have been filled with an as yet unfulfilled to learn some real programming (see last post). A few months ago I sat down to really learn Python, because I had a read a lot online about how it is a great beginner’s language (though I’m not really a complete newbie at this point). Anyway, I’ve been at it for a while and I’m liking how things are going.
But before I had started Python, I had a brief brush with Perl a year ago. At first glance it seemed something I had dreamed of. After struggling through memory management in C++ and Java and sitting through long compile times (which are really irritating if you’re a new programmer making lots of mistakes), Perl’s write and run was a dream and built in memory management made me a lot happier. But it didn’t take too long for me to get dissatisfied. Reading other people’s code was never very easy and variables behaving differently in different contexts was not something that I liked. I left Perl in just over a month.
Looking for an alternative I quickly found out about the Python vs Perl debate and was especially inspired by Eric Raymond’s Linux Journal article. I started Python and quickly fell in love with it. It had all the things I liked in Perl, but none of the eccentricities that I had wanted to leave behind. For a long term I didn’t give Perl another thought. But being the avid netizen, open source fan and linux lover, I kept reading about Perl here and there. So, last December, I decided to look into Perl again.
What I found out since then is that the Perl vs Python debate is rather like apples vs oranges. One of the biggest complaints about Perl is that it is untidy. Perl’s philosophy of “There’s More Than One Way To Do It” means that there are numerous ways to twist your code syntax around and it can be made absolutely unreadable. And it’s not just the syntax. Perl’s later features, especially object-orientation, have a distinctly kludge-like feel about them, as if they were simply slapped on later rather than integrated. Python, on the other is much cleaner and better designed. But there’s a twist to this tale. It’s this: Perl and are Python are very different languages.
Firstly Perl was not meant to be a general purpose language. It was meant to be a sort of shell-script on steroids. It’s a glue language, aptly called a Swiss Army Chainsaw. If you have a myriad of different programs and data sources and all you need to do is bind them with the electronic equivalent of duct tape, use Perl. Keeping Perl to it’s original uses makes most of the problems disappear. You don’t come into contact the kludginess of more advanced features because you will rarely (if ever) use them and readability isn’t so big a problem, because your programs will not genrally be more than a hundred or two lines.
Python on the other hand, is a more general purpose language. Features like object orientation are better implemented because you’re expected to use them all the time. And there is a definite emphasis on writing clean code because your programs will be big with complex tasks, not just delegating to other programs.
So the final word is this: choose your tools carefully. If you need something that will update a web page with a list of recently played songs, use Perl. A good example of how to use Perl effectively would be Xterminus’ changes page, which uses a Perl script to gather data from his del.icio.us account and his wiki software and create a pure HTML page. If you want to write anything with more than a few layers of abstraction and of more than medium complexity, grab Python. If you have any experience with these two, let me know.