Perl vs Python: Why the debate is meaningless

2007 January 29
tags:
by Shrutarshi Basu

Competition is a common thing is hackerdom, and arguments over which one of two (or more things) is the better are common and keep coming and going (but never really go away). Some of the most common ones are: Java vs C#, Emacs vs Vi (and it’s clones), Linux vs BSD, Closed Source vs Open Source and everyone’s favorite: Microsoft vs everyone else. And the internet is littered with evidence of these great wars (which none too rarely take on epic proportions) and some digging will throw up evidence on forums, website, bulletin boards, Usenet archives and of course, blogs. Today I’m taking a look at one of these wars that I’ve had personal experience with: Python vs Perl.

The last few years of my life have been filled with an as yet unfulfilled to learn some real programming (see last post). A few months ago I sat down to really learn Python, because I had a read a lot online about how it is a great beginner’s language (though I’m not really a complete newbie at this point). Anyway, I’ve been at it for a while and I’m liking how things are going.

But before I had started Python, I had a brief brush with Perl a year ago. At first glance it seemed something I had dreamed of. After struggling through memory management in C++ and Java and sitting through long compile times (which are really irritating if you’re a new programmer making lots of mistakes), Perl’s write and run was a dream and built in memory management made me a lot happier. But it didn’t take too long for me to get dissatisfied. Reading other people’s code was never very easy and variables behaving differently in different contexts was not something that I liked. I left Perl in just over a month.

Looking for an alternative I quickly found out about the Python vs Perl debate and was especially inspired by Eric Raymond’s Linux Journal article. I started Python and quickly fell in love with it. It had all the things I liked in Perl, but none of the eccentricities that I had wanted to leave behind. For a long term I didn’t give Perl another thought. But being the avid netizen, open source fan and linux lover, I kept reading about Perl here and there. So, last December, I decided to look into Perl again.

What I found out since then is that the Perl vs Python debate is rather like apples vs oranges. One of the biggest complaints about Perl is that it is untidy. Perl’s philosophy of “There’s More Than One Way To Do It” means that there are numerous ways to twist your code syntax around and it can be made absolutely unreadable. And it’s not just the syntax. Perl’s later features, especially object-orientation, have a distinctly kludge-like feel about them, as if they were simply slapped on later rather than integrated. Python, on the other is much cleaner and better designed. But there’s a twist to this tale. It’s this: Perl and are Python are very different languages.

Firstly Perl was not meant to be a general purpose language. It was meant to be a sort of shell-script on steroids. It’s a glue language, aptly called a Swiss Army Chainsaw. If you have a myriad of different programs and data sources and all you need to do is bind them with the electronic equivalent of duct tape, use Perl. Keeping Perl to it’s original uses makes most of the problems disappear. You don’t come into contact the kludginess of more advanced features because you will rarely (if ever) use them and readability isn’t so big a problem, because your programs will not genrally be more than a hundred or two lines.

Python on the other hand, is a more general purpose language. Features like object orientation are better implemented because you’re expected to use them all the time. And there is a definite emphasis on writing clean code because your programs will be big with complex tasks, not just delegating to other programs.

So the final word is this: choose your tools carefully. If you need something that will update a web page with a list of recently played songs, use Perl. A good example of how to use Perl effectively would be Xterminus’ changes page, which uses a Perl script to gather data from his del.icio.us account and his wiki software and create a pure HTML page. If you want to write anything with more than a few layers of abstraction and of more than medium complexity, grab Python. If you have any experience with these two, let me know.

Tomorrow’s debate: Emacs vs Vi

Update: I had said above that I found memory management in Java and C++ tedious, I admit that was something of a typo. I hadn’t meant to say just C++. Thanks to Digg and Reddit users for pointing this out.

19 Responses leave one →
  1. 2007 January 30

    You seem to suggest that different tasks conveniently/necessarily require different languages, but sticking to one language (a single syntax) often brings more power to the programmer. This also facilitates resue.

  2. 2007 January 31
    Frank permalink

    Unfortunately your limited time with perl shows.

  3. 2007 February 1

    There are established best practices that make Perl code very readable and easy to maintain.

    What’s up with those no-brackets-indentation-only approach huh? Doesn’t that make your head spin? :P

    I’m not saying that Perl is the best and that all other languages are useless. I believe every language has it’s strengths and weaknesses. But most of the time Perl’s strengths are greatly underestimated and it’s weaknesses overstated.

  4. 2007 February 1

    I mean in Python (the indentation-only approach)…

  5. 2007 June 11
    Nimish permalink

    I’ve write code in C, Java, Perl and Python. In all of them, if you want your code to be readable, you have to indent (except for python, you’re not required to). So the brackets are useless, since every language encourages the indentation. If you want to bracket python, simple do it with the help of the comment symbol #. Easy.

    while(True): #{
    doSomething #;

  6. 2007 July 5
    DavidM permalink

    To the Perl aff(licted|ianados):

    You are defending a scripting language with *pointers*.

    If I want to use pointers I will use C, thanks very much.

  7. 2007 October 25
    Arvind Padmanabhan permalink

    I don’t know Python but I can comment on Perl. I have written many elegant scripts for complex problems and I still love it. I often come across comments about how a programmer went back to his program six months later and had difficulty understanding it. For my part, I haven’t had this problem primarily because I use consistently a single syntax. If perl provides more than one way to do things, I choose and use only one. Secondly, I do agree that objects in perl are clunky and make for difficult writing/reading. I have never used them. This makes it difficult for me to write perl scripts for large projects. Perhaps this is where Python succeeds.

  8. 2008 January 9
    Peter permalink

    Perl vs Phyton
    Since my new job required Perl i also plumbed into this we discussion. I wondered should that job be done with Perl..
    I agree one might be able to read his own code back.
    But have you ever tried to repair or code from other people or add some extra functionality. I gues that’s a perl nightmare.

    Dont get me wrong i’ve used many languages from a beginning to novice degree and some expert, to give you an idea: Basic, Qbasic, Phython, Pascal (several versions), C++,C# plain C VB6 VB.net Kixstart VBscript and some others.

    My first impression of Perl, it’s like Kixstart and verry old Basic (without MS in front). Kixx a cutom shell language, and basic well it can deal with vars but in a bit $poor way.
    Looking at phyton i see a some good editors and a much more clean language.
    Notice that a final compiler doesnt compile spaces (..duh).
    Spaces are for readability.

    Other people will be able to read someone else his phyton, and understand it i’m sure. I dont believe this for perl to be treu.

    That doesnt make Perl a hardcore type cool language, it’s making it an old language. Computer Languages evolve
    In the end it doesnt make a big difference how you get things done, if you only wanted to get them done, but there is more..
    You see I’ve also did assembler once. And could get things done with it but does that code fast / or is it easy to repair to maintain ?
    Nope.. old languages are just not cool. You can stay with it but the next generation people will slam you.
    Old languages can be full of tricks (like kixx and perl) but are poor in meaning of the word “language”

    Sadly i was also just looking into VB.net en C.net again (new version), if you would compare those languages to perl of phyton then you see a huge difference in well first syntax but next support click a unknown verb and get forward to MSDN online help with samples and comments that must be magic to perl/phyton people.

    Serious programmers no they should once in a while look around to other languages.. that might be something for perl-scripters to get started to think about a language.

    I wish there was a perl.net
    you see even MS was abele to make Basic o-o

  9. 2008 February 6
    shane o mac permalink

    I was forced to learn Python in order to write scripts within Blender (Open Source 3D Modeler).

    White Hat:
    1. dir( object )
    This is nice as it shows functions and constants.

    Black Hat:
    1. Indention to denote code blocks. (!caca?)

    PERL was more of an experiment instead of necessity. Much of what I know about regular expressions probably came from reading about PERL. I never even wrote much code in PERL. You see CPAN (Repository for modules) alone makes up for all the drawbacks I can’t think of at the moment.

    White Hat:
    FreeBSD use to extensively use PERL for installation routines (4.7., I keep a copy of it in my music case although I don’t know why as I feel its a good luck charm of sorts). Then I read in 5.0 they started removing it in favor of shell script (BAsh). Why?

    Black Hat:
    I’m drawing a blank here.

    With freedom there are costs, you are alowed to do as you please. Place variables as you must and toss code a-muck.

    You can dislipline yourself to write great code in any language. VB-Script I write has the apperance of a stardard practices C application.

    I think it’s a waste of time to sit and debate over which language is suited for which project. Pick one and master it.

    So you can’t read PERL? Break the modules into several files. It’s more about information management than artisan ablility. Divide and Conquor.

  10. 2008 July 10
    John permalink

    ^
    It’s Perl, or perl.
    Not PERL.

  11. 2008 October 23
    Peter permalink

    I disagree with mastering one language. Often there will be trade-offs in a program that match very nicely with a particular program.

    For example, you could code everything in C++\C\assembler, but that only makes sense when you really need speed or memory compactness. After all, I find it difficult to write basic file processing applications in C in under 10 minutes.

    Perl examples use a lot of default variables and various ways to approach problems, but this is really a nightmare when you have to maintain someone else’s code. Especially if you don’t know Perl. I think its hard to understand (without a background in Perl)

    while() {
    chomp;
    print;
    }

    immediately. Not to mention shifting arguments to functions and the various ways to call one

    The equivalient in python:

    s = raw_input(”)
    while(s):
    s = s.strip()
    print s

    Even if you don’t know Python, you can understand it if you know Java\C++\C# etc.
    Indenting helps code readibility, sure some formating utilities don’t work nicely without braces, but its also a must for writing maintainable code for someone else.

  12. 2008 November 24
    Juls permalink

    I’m a perl guy not a py programmer so I won’t detract from python [except for the braces, Guido should at least let the language compile with them].

    Note: Perl is like English it’s a reflective language. So I can make nouns into adjectives and use the power of reflection. For example … ‘The book on my living room table’ vs [Spanish] ‘The book on the table of my living room’.

    And this makes sense … because Larry Wall was a linguist; and was very influenced by the fact that reflective languages can say more with less because much is implied based on usage. These languages can also say the same thing many different ways. [Perl makes me pull my hair out. | Perl makes me pull out my hair.] And being that we have chromosomes that wire us for human language … these difficulties are soon mastered by even children. But … but we don’t have the same affinity for programming languages (well most of us) so yes Perl can be a struggle in the beginning. But once you achieve a strong familiarity and stop trying to turn Perl into C or Python and allow Perl just to be Perl you really really start to enjoy it for those reasons you didn’t like it before.

    The biggest failure of Perl has been its users enjoying the higher end values of the language and failing to publish and document simple examples to help non-monks get there. You shouldn’t have to be a monk to seek wisdom at the monastic gates.

    Example … Perl classes. Obtuse and hard to understand you say? It doesn’t have to be … I think that most programmers will understand and be able to write their own after just looking at this simple example. Keep in mind, we just use ‘package’ instead or class. Bless tells the interpreter your intentions and is explicitly used because you can bless all kinds of things … including a class (package).

    my $calc = new Calc; # or Calc->new;
    print $calc->Add(1);
    print $calc->Add(9);
    print $calc->Pie(67);

    package Calc;
    sub new
    {
    my $class = shift; # inherits from package name, 'Calc'
    my $self =
    {
    _undue => undef,
    _currentVal => 0,
    _pie => 3.14
    };
    bless $self, $class; # now we have a class named 'Calc'
    return $self;
    1;

    sub Add
    {
    my ($self, $val) = @_;

    $self->{_undue} = $self->{_currentVal}; # save off the last value

    $self->{_currentVal} = $self->{_currentVal} + $val; # add the scalars
    return $self->{_currentVal}; # return the new value
    }

    sub Pie
    {
    my ($self, $val) = @_;

    $self->{_undue} = $self->{_currentVal}; # save off the last value

    $self->{_currentVal} = $self->{_pie} * $val; # add the scalars
    return $self->{_currentVal}; # return the new value
    }
    }

  13. 2008 December 7
    L_fla permalink

    I disagree with the notion, put forth by the original author, that you should use Perl instead of Python for small glue scripts. I know and work with both languages and write glue code on a regular basis, and can’t really say that either is better. But, I can say that Python definitely holds its own, and for truly one-off jobs (like data (wr|m)angling) the interactive Python prompt is quite handy (actually I hope something similar finds its way into Perl6, should there ever be a Perl6).

  14. 2009 January 17
    baboo permalink

    Hi Basu,

    you are highly prejudiced about perl. I programmed in perl then moved to python and Ruby…finally I am back to perl.
    The reason is that perl has many advantages than python or ruby.

    Python and ruby are slow. U need to know C to make python run faster using pypy, inline etc.

    Mod-perl is the superior to mod-python and WSGI-python; you can rip apart pache with mod-perl

    I don’t need to learn C (or) any other language again when using perl.

    Ruby looks like 3rd class crap to me with different kind of blocks.

    Actually python and ruby languages suck the hardness of perl and present the syntax in a nice way but see that’s the reason why they suck most.

    Try creating webapps with spaces using python and u suck desparately. Almost all programs in python are slow. “youtube” sucks most. NASA uses python to make their calculations wrong. Google uses python-ORM to improperly index databases so that we don’t get proper results for what we search.

    Python creators are sloths. They cnanot walk. They use wheel chair to go to toilet. Python was never useful in production environment as it sucks ass due to slow run time. Screw psycho, pypy and inline.

    If u don’t want to have mid life crisis use perl. There are no python jobs anywhere. Many reasons are:
    1) slow run time
    2) Refactoring sucks ass and double ass

    Use perl for anything. Python and ruby libraries have just 1/5 of perl libraries for doing any task.

    Web 2.0 is not so gr8 other than for buzzwords. AJAX is also not useful if u have large data.

    I can challenge any programmer of python or ruby for writing faster programs than perl.

    Readibility will be there more in perl if u use properly the language.

    perl can do anything and everything. It is universal panacea. U will know that when u try solving many problems that require help. Perl is big daddy to care for people like u when u lift hands in desperation after trying other languages. Don’t listen to 3rd class people about hteir sick opinions on perl6. Similar things were told about perl5 and see till now perl5 rules in speed and libraries.

    If u have nay doubt in achieving any task ask me or perl monks. almost all ur problems may have been already done.

    Catalyst web framework and Rose ORM are ultimate. Try them if u really need to make web 2.0 like apps.

    Think with calm mind and u know the truth.

    Cheers,
    Baboo

  15. 2009 January 17
    baboo permalink

    Just to add more—

    Python OOPS is shit to the power of 5000. Ruby and Java are atleast ok in that. Perl has an intelligent OOPs that u will appreciate once u code in that. It was designed to be more programmer friendly. Read Damian Conway book on OOP perl and u will open ur eyes to the truth.

    May god punish all those other language creators and programmers and burn them for the hatred and rumours they are spreading on perl.

    I am the first perl programmer to react so violently as I am fed up with 3rd and 4rth class bast*rds.

    cheers,
    baboo

  16. 2009 February 6
    Helgiks permalink

    Haha, that is actually more funny than it is true.

  17. 2009 February 15
    Jim permalink

    Perl is not a scripting language, strictly speaking, and neither is Python. Perl is an extaction and reporting language, and in that field it is pretty unbeatable. An general application language it is not, nor was ever intended to be.

    Perl is the sysadmins friend, because systems administrators do a lot of, well, extraction and reporting. Want to make sense of several million lines of awkwardly formatted log files ? Perl is your friend.

    My main beef with Python is that it looks like Visual Basic circa 1995.

  18. 2009 June 9
    Mark permalink

    One thing about perl that i have found that
    I haven’t been able to do in any other language
    is print a number to the 5 millionth place
    like this

    require “bigfloat.pl”;
    print fdiv(1,23**.2345, 5000000);

    if you can do that in any other language
    let me know… what language it is!

    “one thing is for sure is that I don’t think
    you could do that in less code.”

    I would love to be able to do that in C++ or C
    even using more code, PLEASE let me know
    if you figure out the code for that

    and ASM would be even better!
    mrbaker_mark@yahoo.com

Trackbacks & Pingbacks

  1. What programming languge best for scripting in Linux? | LINUX PROBLEMS

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS