본문 바로가기

iPhone/News

Perl vs Python – The Final Battle

출처: http://www.revolves.net/2009/11/29/perl-vs-python-the-final-battle/


on November 29, 2009

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.

파이썬과 펄의 차이점을 구글링해서 찾아낸 괜찮은 아티클 입니다.

'iPhone > News' 카테고리의 다른 글

아이폰 iOS 4.1 곧 탈옥 가능?  (0) 2010.09.27
7 tentpole features of iPhone OS 4.0  (0) 2010.04.10