Monthly Archives: November 2014

Python vs. Perl vs. PHP vs. Bash

There are a lot of posts about this topic! So why one more? This post is just to share my personal experiences with the few scripting languages I have used in the past few years. Contrary to hot debates about which is better, I consider each of these languages I use i.e. Python, Perl, PHP and Bash (or shell) as simply a knife in my swiss army knife. When a specific task can be accomplished easily with one, I just use it.

Perl

I begin with Perl because that’s the first scripting language I learnt. I have scripted a lot in Perl. Here are the parts I enjoy in Perl:

  1. I owe my love for regular expressions (regex) to Perl. If you want to learn regular expressions, you should learn them in Perl (or Python or PHP), and definitely not in Java :-). In my job (where we mainly use Java) when people need help with regexes, they go to people with scripting experience. I believe Java developers should not try to learn regexes with Java. They should try to learn regexes with a scripting language and then transition to Java regexes.
  2. Perl really shines when you need to parse a large text/log file line by line and do a bunch of regex matches.
  3. Perl makes accessing *nix command outputs very easy with back ticks. For example, you could just put back ticks around a long system command and capture its output in a variable like this:
  4. Perl’s string interpolation of variable values inside double quoted strings is a lovely feature. For example:
  5. Perl has a lot of one liners. It takes some time to get used to them, but once you learn them, they can be very useful.

And here are a few tasks for which I found Perl to be unnatural or difficult to use:

  1. If I need to work with classes and objects, I do not use Perl. Somehow I feel OOP support in Perl is not straight forward to learn and use.
  2. While Perl hashes have a lot of power, the sigils (especially in multi-dimensional hashes) made my head spin.
  3. The main reason I moved away from Perl was when I started working with MongoDB (from 2012). I am glad that I had learnt some Python by then, so I could compare the two languages for working with mongo. Python’s pymongo was a mature client library, while Mongo’s creator 10gen (now MongoDB, Inc.) did not seem to care much about Perl drivers. The Perl driver documentations were also not clear, and even as of today the Perl driver tutorial page does not even load! Also MongoDB’s JSON documents map seamlessly to Python dictionaries, which IMO are much easier to work with than Perl’s multi-dimensional hashes.

Python

I was using Perl happily until a few years ago, but then started seeing a lot of people using Python. And there were hot debates about which language is better. So I started learning Python from online tutorials. Python has one of the best documentations about any programming language I have ever read. Later, I read The Quick Python Book, Second Edition. And today I am an avid user (and learner) of Python! There are a lot of very nice things about Python and I am not going to list them all here. Just the top points:

  1. I love the Python IDLE. It makes development a lot of fun with Python. Usually I test some small function in IDLE and then copy it over to my Python script in Eclipse-PyDev (which IMO is a great Python code editor).
  2. Like I mentioned before, if I need to work with MongoDB, I use python and pymongo.
  3. For any decent sized project which we intend to keep for some time (especially in cron jobs), Python is my language of choice.
  4. No more curly braces and semi-colons making code shorter and cleaner.

While Python is my main scripting language of choice these days, I do miss two of Perl’s features: string interpolation and ease of command line output capture. It is possible to do these things in Python, but just not as easily as in Perl.

PHP

PHP is my language of choice when I need to build a quick dynamic web page. On most of our servers, PHP and Apache HTTP server are already installed, so building a web page in PHP is very easy. PHP provides a lot of built-in functions. Here are a few I used recently:

scandir – List files and directories inside the specified path
file_exists – Checks whether a file or directory exists
nl2br – Inserts HTML line breaks before all newlines in a string

Since I don’t usually build UI tools, my overall experience with PHP is limited. But if someone asks me to build a UI tool and provide them a link, I will most likely use PHP.

Bash/Shell Script

I love the *nix command line. I also use pipelines to do quick analysis (especially on log files). But if I need to put my code in a script, I stay away from bash and shell scripts. Somehow I find it difficult to just scan a bash script and understand what it does. The code has to be cluttered with comments to explain what it is doing. And if you have experience maintaining code for a while, you know people tend to forget to update comments when they update code. It is much easier for me to read a Python script and understand what it does quickly (without the need for comments).


So that’s the summary of when I use what :-).


You can vote for your favorite programming language below (no sign up required). Have fun voting and seeing the items move live as you vote!

Coolest Programming Languages