Tuesday, September 22, 2009

But Why Python? Why Not Perl?

I enjoy using Python. It's clean, it's simple, it's readable. Even truly bizarre stuff manages to maintain some level of readability, at least for me. This doesn't change the fact that so many people, on hearing me say that I enjoy Python, make the statement "But why? I mean... it's Python, for god's sake!"

You're right, it is. And that's kind of the point, really. Python is not perfect. In fact, one thing always jumps out at me as being glaringly annoying: The white space. I hate it. I've always liked having a clearly delineated begin/end pair (such as {} in C, Java, etc). I know, it's silly, but it is a personal preference. No coherent reason for hating it, I just do.

The rest of the language, though, is wonderful. The syntax is consistent, the introspection capabilities allow you to do things easily that would be difficult (or impossible) in others, unit testing is easily done, and using language features you can make large scale API changes rather easily and safely.

I'm going to go with just a short example of why I prefer Python over Perl: Method signatures. Consider this method signature in Perl:

sub edit_form_data {
    my ($name, $email, $url, @addresslines, $city, $state, $zip, $extradata) = @_;

Experienced Perl people will point out the error in there: @addresslines will take on the remainder of the values, so that $city, $state, $zip, and $extradata will all be marked as undef. If you're a relative newbie to Perl, that will bite you as you struggle to figure out why your remaining form fields are always blank.

With Python, you would do something like this:

def edit_form_data(name, email, url, addresslines, city, state, zip, extradata):

Now, your data will always match up. Each of the items being passed in is an object, and those objects are self-contained enough not to suck up other objects.

A feature I left out of that signature, but that is expected by other Python authors, is the presence of a docstring for your method. It's actually a built in expectation of the language, since there exists a function named "help" that extracts that docstring and displays it to the user. Perl has no similar facility as a built-in. Yes, perldoc exists, but it is an add-on, and one that is all too frequently ignored.

Also, for that method, in Perl, what exactly are you passing in? Those $ variables could be scalars (numbers and strings), or references. If they are references, they could be references to scalars, lists, or associative arrays. Getting at the actual object for each of those is different, and the syntax is hideous.

Now, here's where the fun comes in: Notice the lack of documentation. If you're trying to call this method from outside, you have to know what the method needs (as is true with any language). But, with Perl, you have to stop, read the code, analyze the syntax, and determine the exact type based on what the syntax needs. And, if you get it wrong, you're going to pass in the wrong type of reference, and the called method is going to blow up. You might not even get a clear reason why, leaving you to debug a mess.

Suffice to say that, with Perl, knowing what you need to do when using an API is harder. I would go so far as to say that one of the only languages which is harder is assembly. Even C is easier, since you get some information from the function prototype.

Perl has frequently been called a write-only language. After trying to maintain Perl programs that have been poorly documented and have little to no unit tests, it is easy to see why.

Yes, Python can be written badly, same as Perl. But, for some reason, it usually is not. With Python, the code is normally documented, frequently has tests, and is quite readable even without the docs and tests. I don't know why that is.

But I do know that the Perl community needs to find a way to change that. Until something even better comes along, I'll stick with Python.


Adriano Meis said...

But comparing Python to Perl is so '99.. back then, it made more sense :-/

Michael Pedersen said...

It might very well be. But I still get asked this question. Heck, I hear it regularly at work. Isn't Python that stupid language? Why not stick with something decent like Perl?

Personally, it drives me nuts.

Webmaster said...

What a joke. That is not a Perl method signature at all; apples to oranges.

Python folks hate Perl because they weren't clever enough to grok it. No one likes being beat by a programming language.

use MooseX::Method::Signatures;

method edit_form_data(Str $name, Str $email, Str $url, ArrayRef $addresslines, Str $city, Str $state, Int $zip, $extradata) {}

Michael Pedersen said...

I didn't see this comment until just now, my apologies.

Now, let me see if I've got this straight: Your response to a criticism of the Perl language is to tell me to go and get a non-standard library (that, before today, I'd never heard of, and is almost completely unused) to try to overcome deficiencies in the core language?

And yes, I can fairly safely say nearly completely unused. On my Ubuntu system, I just ran a search on the complete Package listing. 29007 "Depends" lines were listed. Of those, 3097 mentioned perl (i.e.: Of 29007 packages with dependencies, 3097 of those had something to do with Perl). Of those 3097, precisely 1 needed MooseX::Method::Signatures.

While I haven't searched CPAN, I would be surprised to find it much different.

Mere words are incapable of describing what a wonderful response this is to me. You can keep your idea of method signatures. I'll stick with the nice stuff I get from Python.