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
“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.
Thanks for the correction Jacinta, I missed on that one! I’ve updated the original post.
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.
Yes, you’re right. But I personally consider arrays and hashes to be data structures, not primitive data types.
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.
@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.
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
@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!
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.
I like perl (but not more then C) and i HATE python.
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.
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
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)?
@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.
@ Ben: I would recommend Perl as that is how the language came about. Unix integration is great and not bad on Windows. Also Perl 5 is a stable language WRT Python, which is still undergoing major changes (ver 2 -> 3). Ok Perl 6 is coming out but that is regarded as a sister language and not a replacement (bad name!). Perl 5 is not going to go away. I have frequently heard Python programmers state oh you need version x to run this where Perl scripts generally work without issue most of the time.
Generally:
If you like Python then go for it. Perl does have it’s drawbacks as does any language. I went on a Python course, hoping to understand why it got such praise. Quite frankly at the end of it I couldn’t see the point in the language. Generally Perl did stuff better or made it easier. Some things I noted about Python:
- No real data privacy. Privates are named with leading underscores but you can still get at them. My variables in Perl are truly private to the file that they are declared in.
- Syntactic white space! This was done more as an academic challenge rather than because it would improve the language. As a result we have a language that I find harder to read (no terminating } to count when looking at nested code) and is limited syntactically (you can’t have do – while loops for example).
- Needlessly odd syntax – why use `:’ why not just use C’s idiom of enclosing boolean/loop expressions in ().
- No up front variable declaration. So instead of a var keyword we have the nonlocal and global keywords – arrhh.
- No `built in’ text pattern matching. One thing that is really nice about Perl are the built in pattern matching, extraction and alteration operations. Python has all of these but as library/supporter classes as say C++ would have (indeed the class for regexs in Python is almost identical to the one I wrote for C++). However compared to Perl, these classes are very clumsy to use (be it C++ or Python).
There are of course many good points about Python. Given the choice I would always use Perl, but if the project dictates Python then so be it.
As for maintainability. Sorry but if you find Perl to be a write once language then that is down to you not being a sufficiently experienced software engineer. One should always write good code that is clear and maintainable.
I have found Perl to be ideal for larger projects (~20,000 lines of code), and generally it is a lot faster to develop reliable, moderately complex software in Perl than in C or C++. I have recently contributed mtn-browse to the Monotone project. This is written in Perl using Gtk2 bindings. There is no way that I could have completed it in the time I did if it had been written in C or C++ and it is a very robust application. Oh and before you wonder, I have had ~25 years experience in C, ~4 years in C++, ~4 years in Perl and ~8 years in C based GUI programming (X11/XView/Xt/Motif).
Also as has been mentioned previously, one should be quite familiar with a language before passing judgement on its readability. I found C++ hard going at first and Python’s list slicing syntax a bit impenetrable, but I wouldn”t criticize either for my lack of familiarity.
I would much rather use a language that gave me freedom and flexibility (e.g. C, C++, Perl) than one that is overly constraining, verbose or `dumbed down’ (Java, Pascal, Ada). I want to decide what to use and how to constrain myself.
Its a case of different horses for different courses. Just code and have fun!
.
Tony.
I am an old perl hack trying to learn python. So far it has been a little painful. The Perl regex are better as well.
Well, I’m native C/C++ programmer, but since I moved to OS X environment I needed to do something useful with scripting.
I learnt Objective-C (Cocoa) and wanted to expand the functionality of my programs using some of the scripts. I found Python far more advanced in certain things than Perl and AppleScript, so I stuck with it. That doesn’t necessarily mean that the Perl is a bad choice. I use it a lot while I’m writing patches for my web apps and for small scripts used in BT Linux for penetration testing. Perl is just great for stuff like that. And I agree with many of you here. If you can learn both of these, go for, it can only be advantage and never disadvantage.