Revolves

Innovation

Revolves header image 2

Perl vs Python – The Final Battle

November 29th, 2009 · 8 Comments · Perl, Python

You might have already seen a lot of Perl vs Python stuff. So I’m telling you in advance that I’ve too have read them all, and what you’re going to read here would be completely different than what you already have.

Firstly, let me start by saying that Perl and Python are great languages. But they have their own differences, which is precisely why we have two different languages. If they were the same, why have two different languages at all?

The people who are biased against a language are the one who try to write Python in Perl or Perl in Python. They want Perl to work like Python or vice versa. And if you’re intelligent enough, you’ll quickly understand that there is no point in having them both work in the exact same way.

I’ll shout out one basic difference between the two. This is one difference a beginner will likely notice. I won’t mention many of the “advanced differences”, for an experienced programmer doesn’t need my help for that.

Perl has only one primary data type, called a “scalar.” In Python, we have different types, i.e. int, string etc. You need not specify the type of the data a variable will hold, since both of these languages will find out about that themselves.

Once you store a number in a Python variable, the variable obtains a type int. You can’t concatenate a string and an integer like this: “hello” + s, where you might have previously written: ’s = 3′. You have to explicitly convert that number into a string using ’str(s)’.

Now, Perl doesn’t know the difference between a string and a number. Because all we have is scalar. So, how does Perl work then? Perl determines the type of your variable depending on the context. If you have ‘ $s = “3″ ‘, and then you write ‘ print $s * 4 ‘, Perl will print 12. Thus, when used in a numeric context, there is no difference between ‘ $s = 3 ‘ and ‘ $s = “3″ ‘.

This basic fact also influenced various further design decisions. In Python, “hello” * 4 would give “hellohellohellohello” while 4 * 4 would give 16. But if you type “hello” * 4 in Perl, it’ll give you 0. This is because, since you’ve used ‘*’, Perl thinks it’s a numeric context. It solely determines datatype on basis of context. So, it fruitlessly tries converting “hello” into an integer, which yields 0 at worst. But had our string been “4hello5″, Perl would have converted it into 4.

That’s why Perl has a separate operator for multiplying strings. Here, typing “hello” x 4 would print “hellohellohellohello”. The use of ‘x’ yells string context.

It might seem impossible to program using such “contextual” data. But that’s how we use our natural language. In English, every word can have different meaning on the basis of context. But it still doesn’t confuse you when properly used.

Another little example where beginners can get trapped. In Python, there are ‘int’ and ‘float’ data types. The former stores integers while the latter stores numbers having decimal point. So, looking at the type, you can pretty much know whether a variable would have a decimal point or not.

But Perl performs all calculations in floating-points. I.e., for it all numbers are floating points. Thus, it has a function called ‘int’. If you have ‘$s = 3.5′ and you do ‘$s = int($s)’, you’ll get 3 in $s. So, has Perl converted your decimal number into an integer? No! Remember, we only have a scalar datatype. Perl has merely truncated anything after the decimal point. That’s why there is no function to convert integers to floating points.

This sounds a little counter-intuitive just because we try to use our knowledge of other languages while learning a new language. We want the new language to work like the languages we’ve already learnt. Keep an open mind.

So, what’s the conclusion? I’m personally learning them both. Why? It’s easier to learn them both rather than fighting which one’s better. Python is elegant and powerful, and Perl has come crazy shortcuts which can help get tasks done quickly (of course, at a cost). They both have their own pluses and minuses. You should find their strengths and weaknesses for yourself, rather than being influenced by someone else.

And to people who say Perl is unreadable. If you know Perl, it is easy to read, else it’s going to be tough (obviously). If you can learn regex, you can learn Perl.

For Perl, the Llama or ‘Learning Perl’ book by O’Reilly is great. For Python, you can find great tutorials pretty much everywhere. I’d recommend practicing the same programs both in Python and Perl initially, to develop a knack for how these languages work.

Update: Added Amazon links to good Perl books

Liked The Post? Share And Enjoy!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Sphinn
  • Mixx
  • Diigo
  • BlinkList
  • Reddit
  • Blogosphere News
  • IndianPad
  • RSS

Tags:

8 Comments so far ↓

  • Jacinta Richardson

    “If you have ‘$s = 3.5′ and you do ‘int($s)’, you’ll get 3 in $s.”

    Technically you will still have 3.5 in $s. int($s) doesn’t change its argument, it merely returns the integer portion. Thus to get 3 in $s you’d have to write: ‘$s = int $s;’ (with or without the parens, it’s the same).

    Lots of functions like these don’t change the argument, but instead return the altered version. This is usually what you want.

  • admin

    Thanks for the correction Jacinta, I missed on that one! I’ve updated the original post.

  • inno

    While it’s true that Perl is weakly typed, it actually has three main data types: scalar, array, and hash. (Along with glob, code values, and pointers.)

    Internally, Perl represents scalars as one of five types: signed integer, unsigned integer, double, string, or another scalar.

    Take a look at perlguts for more info.

  • admin

    Yes, you’re right. But I personally consider arrays and hashes to be data structures, not primitive data types.

  • Jim

    Great article. I prefer Python personally. However it seems like in an industrial setting, Perl is taken more seriously. If you tell your boss you wrote a Python script to automate a task, he is like, “huh?”. If you say you wrote a Perl script, then it seems to be taken more seriously. From my own experience, I agree with you; you need to know how to program in both.

  • admin

    @Jim

    You’re right about the boss part. Some people only have a narrow knowledge about doing certain tasks, and normally don’t prefer any other methods of accomplishing it.

    I actually learned Python first, and then Perl because it was also a popularly used language (regardless of what online newbies suggest).

    Well, now that we’re talking about Python, I’m eagerly waiting for all those libraries and tools like iPython to be compatible with Python 3.

  • Jeff

    5 years ago I got sick of Perl, looked for a new language, and settled on Python. In hindsight it was definitely the right decision.

    Python has one fundamental data type; object. Everything else is a subclass of object. Variables can only hold references to objects.

    The inability to contact a string with an int is a property of the string and int classes, not a property of the core language. If you actually WANT to be able to concat strings and ints you can easily do it by subclassing string and int and overloading the + operator so that it does the conversion automatically. But that wouldn’t be very pythonic; one of the design goals of python is to be powerful without resorting to “magic”.

    One of the things about perl that pissed me off was never really knowing what was actually IN a scalar; is it a string? a number? a reference to a box with rocks in it? Maybe all three depending on whatever unintuitive implicit rules perl decides to apply to it depending on context. Ruby does the same stuff, drives me nuts. It’s fine for scripts, terrible for applications, and scripts have a bad way of turning into applications. “Every program attempts to expand until it can read mail.” — Jamie Zawinski

  • admin

    @Jeff

    You’re right. Python is indeed elegant.

    Personally for me, Perl is great for some small scripts doing quick tasks. I use Python for anything “larger.”

    Another good point you mentioned is about the bad way in which scripts turn into applications. Many people think that an application is “many scripts put together.” However, that’s wrong.

    I understand your frustration, because many people putting their code online don’t write it the way it should have been written. It’s a total mess to read them.

    At the end of the day, one thing’s clear. Keeping all the specific languages aside, writing well structured applications is an art in itself.

    And one more thing, thanks for the comment!

Leave a Comment