Perl versus Python
I am used to program Perl for years now. Although I have seen a lot of Perl code from other people that is really unbelievably ugly I still think that Perl is my language. But it is not a clear winner for everyone. These are my thoughts after trying to learn Python:
Perl pros
- (still) more widely used
- CPAN - it has tons of well-tested modules
- Perl tries to use sane defaults where Python raises exceptions pretty often (like accessing an undefined hash element).
Perl's "or die" is but easier on the eye than the try/except hassle in Python.
Perl code is often more logical. join(',' , @array) is likely easier to read than ';'.join(list)
Perl cons
- handling nested data structures can become dirty (but opposed to Python you know when you copy something or create a reference)
- non-experts in Perl write very ugly code because there-is-more-than-one-way-to-do-it and their way is often bad style - but other people's projects may be as hard to read as in Perl
- parameter passing is worse than in Python since you don't have named parameters and need to take care to eventually pass by reference
Python pros
- easier to read than Perl code - especially the indentation makes it easier to read (although editors have it harder to format that source code appropriately)
- nested datastructures look better in Python
- creating OO classes in Python is trivial where in Perl it is unneededly complicated (on the other hand Perl can cleanly DESTROY a class while in Python this is not recommended)
very good list handling (where in Perl you would need hashes very often) and just look at those funky sets
- good object-oriented style (classes are set up quickly and easily)
Python cons
- Less modules than Perl. Especially in key issues. Where is Net::IPv4 in Python? Or Net::SNMP? (There is work in progress but nothing complete or stable.) And still no decent CGI support (no modules for session handling, nothing to create HTML headers). Modules are generally hard to find since there is no central place that you can search. Documentation is often very bad (worst example: MySQLdb) You will often find yourself reinventing the wheel.
- ranges aren't very logical in Python (1:3 returns two elements instead of three)
- strings are immutable in Python (to do something with a string you always need to create a copy. Chopping a string in Python means: string = string.rstrip()). It may be obvious for the developers of Python why this needs to be like that. But this isn't what a programmer should need to know.
- not as object-oriented as it should be. (why len(string) instead of string.len()?)
- in Python you may accidentally get aliases (a pointer to another object) where you want to get a copy. (You may often find yourself changing a variable that also changes other data structures although you did not intend that. Copying objects is a question of how deep/recursive the copy is supposed to be.)
- some Python features make the code barely readable (lambda operators, list comprehensions)
- try/except is just weird. It is even used to break out of an inner loop.
why doesn't sort() return a sorted list but just sort the list? (
I learned that since Python 2.4 there is a sorted() function for that purpose) - Python is work in progress. There aren't that many standards as in Perl. You need to depend on exact Python versions to not get undesirable results. Similar to PHP on this matter.
Opinionated
If I were asked to sum up my opinion of Perl versus Python I would put it like this: Perl may be about hacking together ugly code pieces if you are not careful but it has tools for every task you will ever need. Python is beautiful but incomplete. Perl is funky but practical.
Python is a very pretty language which makes code maintenance a bit easier on large projects. Still it is not magical and reading other people's code can be as weird in Python as it is in Perl. Python scripts do not look well if you do not document your work or use decent naming of variables and subroutines.
I tried to get an idea from other people's web sites whether Python is better than Perl and why. Actually most of those sites are bashing Perl. Mainly because the authors are unwilling to really learn Perl. There are reports where "experienced perl programmers" started with Python from scratch and one our later had solved a complex programming problem. Bullshit! There is good documentation on Python for starters. But it will keep you busy for at least an evening if you are really fast-paced. You need to understand the syntax and concepts before you can really start doing more than a print "Hello, world".
Python's syntax is easier than Perl's. Python provides syntax for programming tasks that often reoccur. Perl programmers merely find hacks and workarounds for Perl's syntactical flaws. Yes, Perl can handle all kinds of tasks. But it is not always clear from the source code what the programmer wants to accomplish.
When I explain my Perl code to others I find myself explaining why I did this and that because it is less obvious. Although I have not worked much with Python in a team yet I believe that there are fewer moments when I shrug because I cannot figure out why I did certain code passages. Perl sometimes is black magic but Python does not have that taste of mystery. There is seldom more than one way to do it in Python so if you are a source code artist you may find Python boring. Perl's often quoted TIMTOWTDI (there is more than one way to do it) often results in: there is no obviously right way to do it. Honestly: I like my Perl scripts. I have been programming Perl for years and with decent style (mainly using abstraction through classes) even large projects were maintainable. I also found it interesting to see other people do the same task in even fewer lines. But every nifty hack turned out to be boomerang because the niftier it was the worse I could understand it a few weeks later.
FWIW Python makes the simple tasks look good where Perl often uses less intuitive syntax. However using both languages for real life tasks both languages quickly can become ugly if you don't know the languages well. Comparing the Perl Cookbook and the Python Cookbook I definitely consider the Python hacks more ugly. And since Perl has a larger library of well documented modules it is mainly my language of choice currently. I have tried to get into Python time and again but the real life has often hit me.
My conclusion? If you are new to programming: start with Python. If you are an averagely experienced programmer and need to solve a task in team work you should try to get it done in Python - even you have no Python experience yet. Just calculate that you may reinvent features that you are used to use in Perl.
If you - like me - have been working with Perl for years then you will hardly find Python so sexy that you will instantly switch over to Python. Perhaps you should take the Python tutorial Dive into Python and just read how things are done in Python without thinking too much about where you will end. If I am in a hurry I will likely hack something together in Perl. Though on larger projects where I have more time to accomplish my goal I will likely try to do it in Python and get some practice.
