PHP vs Perl comparison: the Perl view

by Stanislav Shalunov        December 2007
Likes: arrays, their nesting, and web integration. Dislikes: no lexical scope, short cicruits, or seatbelts.

I've been actively using Perl since 1997. For background, before Perl, the only high-level language I used was Lisp. I found Perl's text-processing capacity impressive and stayed. Recently (a year ago), I was exposed to using Python for webdev.

Last Sunday, I decided to hold my nose and give PHP a try in a toy Sunday project. I liked it a lot more than I expected I would. For a taste, this implements a poor man's proxy/mirror:

$fp = fopen($_REQUEST['url'], 'r', false);
fpassthru($fp);
(Before jumping in with LWP::Simple, note that it starts sending before the entire file is downloaded.)

I have only looked at PHP 5. But then again, why would you look at the old version?

These are my very first impressions of the language. They may yet change.

PHP provokes strong reactions, much like alleged English food substance marmite. I'll start with the good.

Like: PHP arrays are universal composite data structures

Perl merges lists and arrays. PHP goes a final step further.

PHP does not separate hashes/dictionaries/maps, lists, and arrays. Instead, it uses a single composite data structure. That hash table with next links you implemented in C? They have that, plus optimization for random access by index, built right into the language. Powerful and frees you to think of what you actually want to do.

Make no mistake: this is a data structure from a real first-class programming language. To the point that its power feels out of place in a little webdev language.

Like: web integration

It just works with little more effort than HTML. (I use Apache anyway.) No deciding between mod_perl, CGI, FastCGI, SCGI, internal server, and whatever else you were considering. Things like persistent database connections are trivial and taken for granted. No wonder 40 out of 100 top web sites run on PHP.

And it's fast. On a server where locally getting static HTML file takes 13ms, getting a lightweight PHP file takes 15ms with the default configuration and no tuning.

Like: No auto-flattening

Perl's auto-flattening of lists is ugly and one of few Perl features that I intensely dislike. In PHP, you don't need to use references to use arrays to represent a tree.

array(1, array(2, 3), 4)

Like: Functions do what you want

There are a lot of standard functions and they are generally well thought out and do what you want to do during webdev. Good standard library is no small feat.

Like: It's obvious how to do things

Well, it you know Perl and HTML, that is. I hardly had to use the docs, and then mostly to look up standard functions. Easy entry.

Lukewarm like: Class syntax

I don't expect anyone likes Perl's objects. I don't, anyway. I'm not much into OOP in any language, but in Perl I avoid it completely. Pretty much anything is better than Perl's OOP system, and PHP's classes are just that: anything. Bland and Java-like. I haven't had a chance to use them, so I don't know if PHP supports generic functions.

Dislike: No lexical scope

PHP has no lexical scope. Worse, the local scope that they have in functions does not even shadow variables from above. Instead, you can use things like $GLOBALS['x'] or global $x; $y = $x ...;. Both variations are ugly. What is this? Some sort of misguided attempt to encourage functional programming?

Once the lack of lexical scope sinks in, you stop looking for anonymous functions. You couldn't form closures even if they had lambdas.

Dislike: No short-circuits

Did you like Perl's $x ||= $y? That's too bad, because it doesn't work in PHP. It's syntactycally kosher, but logical operations evaluate to 0 and 1. While my other dislikes are to some extent a matter of taste and design, this is inexcusable. PHP gains nothing by destroying information in logical operations.

Dislike: no strict or warnings

Remember the bad old days of Perl 4, when you could spend minutes debugging a problem that turned out to be a simple misspelled variable name? No? That's the state of the art in PHP in 2007. I hope that I am missing something and the equivalent of use warnings; use strict; is possible, but I haven't found anything of the sort.

Conclusion: Give it a whirl for your next webdev project

Warts and all, it deserves a chance. It's easy to whip something small out, and learning pays for itself within first hour or two. Don't go for tutorials. Just jump in.

Note that most of the warts were also present in Perl when it was PHP's age. And everything I list is easy to fix, except for lack of lexical scope. Let's hope they fix it soon. There's no shortage of uses.

Comments