When people talk about readability in programming languages, those who know Perl and Python usually put Python nearly at the top, if not completely at the top of the ladder as the most readable language and Perl ends up at the bottom of that ladder, beating only languages like unlambda, brainf*ck and APL.
The main argument is that Perl has too much line noise, too much syntax, too much special cases and that hinders its readability. On the other hand, Python has a strict and simple syntax. I recently started playing with Perl, and I have to admit that you need to be really careful with how you write your code, because it can quickly degrade into something that will puzzle the archelogists and linguists of the year 3,000.
Nevertheless, I think there are sometimes cases when Perl can be as readable, if not more, than Python. As I was doing some configuration for the admin interface of a quick web application that I am writing in Django, I saw some Python code that was not terribly pretty because it had a lot of punctuation, a lot of apostrophes and commas. Here is the code:
fields = (
(None, { 'fields': ('lastname', 'firstname',) }),
('Address', { 'fields': ('address', 'address2', 'city', 'postal_code') }),
('Telephone', { 'fields': ('phone', 'cell_phone', 'work_phone') }),
('Other', { 'fields': ('fax', 'email', 'contact'),
'classes': 'collapse'}),
)
The fields in the tuples need to be quoted, and it’s not long before you realize that you have a whole bunch of quotes in there. Out of curiosity, I took this code and I “converted” it to Perl code to see how it’d look if Django was built on Perl. The result is surprisingly clean:
@fields = (
[undef, { fields => [qw(lastname firstname)] }],
['Address', { fields => [qw(address address2 city postal_code)] }],
['Telephone', { fields => [qw(phone cell_phone work_phone)] }],
['Other', { fields => [qw(fax email contact)],
classes => 'collapse' }],
);
The two examples have nearly the same number of characters (331 for Python versus 317 for Perl), however the fact that Perl can use [qw()] to create an array reference of quoted words really helps the readability. I agree that [qw()] is 3x uglier that Python’s simple (), however the words in Perl are unquoted and separated by a single space while Python has apostrophes, commas and spaces between the words, which makes it not as easy on the eye to see what are the values of a given key.
Also, I think I helped Python’s readability by aligning the dictionaries. In PEP 8 — Style Guide for Python, under the “Whitespace in Expressions and Statements” section, one of the guideline says:
Avoid extraneous whitespace in the following situations:
[...]
– More than one space around an assignment (or other) operator to
align it with another.
I don’t know if the comma in a tuple must respect that guideline, but if it does, then the Python becomes even harder on the eyes. For the people wanting to improve the readabilty of their Perl code, the O’Reilly book “Perl Best Practices” has many very good recommendations.