Revolves

Innovation

Perl vs Python – The Final Battle

November 29th, 2009 · 14 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:

14 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!

  • JChG

    I think you are missing the forest for the scrubbrush!-) Both languages have largely typeless variables and can do odd things. What does is one*two in Python? Depends on what was last assigned to one and two. What is $one*$two in Perl? Well it’s a number which can be treated as a string in string context…

    The big differences are that Perl (used to) flatten lists and not treat them as basic datum, and pass copies into subroutines. They tried to use globs to pass by reference, and eventually succumbed to tacking on a full ref/deref syntax. Python uses references from the get-go and passes by reference into functions. This makes it easier to construct nested data structures in Python, but you must constantly watch for the reference, shallow copy, deep copy issues.

  • smeezekitty

    I like perl (but not more then C) and i HATE python.

  • Dell

    From reading I like python and think it is a great language for a beginning programmer. I agree that if you can do regex then you can write perl. Now, you can use moose to complete OO and static typing for very large applications. Perl is extremely scalable and flexible. I am sometimes amazed at some of the things you can with Perl. At the end of the day I think that people who leave one language for another never really put in the time to learn or simply couldn’t understand that language. People left C++ for Java but C++ and C still are one of the most popular languages in the world because people are productive in them. I can say if you are productive in Perl or python then write in that language.

  • Truman

    Hmm….to all ur opinions.

    I think I can’t do programming any more but still
    do it for bread and butter. I am frustrated with my life thinking on how much to learn in this life?

    Okay! coming to the point….I like python but
    with perl Moose, there is no challenge from any
    language for perl for common applications.

    Python libraries are good but also annoy me
    with their narrow attitude. They appear to tell
    me like “use this only this way, otherwise ….. This is only the best way to do it”. But this is just my opinion.

    Finally, I stopped learning python/PHP and am sticking to only perl. When it rains, I too will get a proper framework and elegance (perl 6). Also, my waiting paid me MOOSE like library.

    I am with perl for lifetime (another 30 years?)
    So, the summary is, whatever the language you choose, stick to life for it. Someone will improve it for u. Even old COBOL jobs are around. HAAHHAHAHAHHAHAHAHAHA

    Cheers

  • Ben

    Asking for more of an opinion than anything else, I’m a confident Bash scripter looking for something a little more cross-platform and with a little more umph to it.

    My initial thoughts are of Perl, it looks a bit like a Bash script, and it is widely touted that it has things like awk and sed built-in.

    My main interests for scripting are in automating tasks within UNIX environments (predominately OS X and Solaris).

    Out of Python and Perl, which would you advise me to learn (first)?

  • admin

    @Ben

    I’m not very familiar with Bash. However, from my personal experience, I can cook up small apps quite fast in Perl, especially when dealing with text (well, that’s what Perl is for).

    What I’d suggest is to pick up a Perl book, and try writing a few automating scripts in it. If you don’t like Perl, move on to Python. But even if you stick to Perl, I’d recommend getting familiar with Python eventually. This is because both Python and Perl are popular languages for automation tasks, especially in UNIX environments where they’re pre-installed.

Leave a Comment