Main Content

Perl v PHP, choosing the right language

Archive - Originally posted on "The Horse's Mouth" - 2008-08-14 00:59:14 - Graham Ellis

"Should I use Perl or PHP?" - a question asked today about a heavy data processing application, but by a delegate who's main work is web site stuff and who is here on a PHP course ... with some prior Perl experience.

Not an easy question to answer; if the guy was only doing the data processing work then the answer would be "use Perl" - but he's mostly doing web stuff, and PHP stand alone is pretty darned good, will allow him to concentrate on the one language, to not have to involve himself with some of the idiosyncrasies of Perl which are fabulous once you know them well, and will let him share common code between the web and stand alone applications ...

Here's a demonstration program that we wrote to analyse a 30 Mbyte log file (or web site log for yesterday) and report on all the IP addresses that visited us, sorted by the number of hits from each.

In Perl



open (FH,"../elogs/ac_20080812");
 
while (<FH>) {
  ($hostid) = split;
  $counter{$hostid}++;
  }
 
@visihosts = sort {$counter{$a} <=> $counter{$b}}
  (keys %counter);
 
foreach $host(@visihosts) {
  print ("$host $counter{$host}\n");
  }



and in PHP



#!/usr/local/bin/php
<?php
$fh = fopen ("../elogs/ac_20080812","r");
 
while ($line = fgets($fh,4096)) {
  $parts = explode(" ",$line);
  $hostid = $parts[0];
  $counter[$hostid]++;
  }
 
asort($counter);
 
foreach (array_keys($counter) as $host) {
  print ("$host $counter[$host]\n");
  }
?>



What differences do I pick up?

Well - the Perl is about 10% shorter by its use of mechanisms such as the $_ variable (topicalisation), a trick that's great for the experienced programmer but a nightmare for the uninitiated. But the Perl looses some of that shortening because there's a need to specify a comparator to sort.

The Perl example uses both hashes (unordered collections with any scalar used as key) which are referenced as a whole with the % character, and element by element by placing the subscript in curly braces AND lists (ordered collections with numeric keys) where the whole is reference with an @ and element by element by placing the key in square brackets. The PHP code is easier to follow, as all variables including arrays and associative arrays start with a $ and all subscripts are square bracketed - but in the structure of the language you pay for this a little in performance as PHP associative arrays are ordered which makes them a little slower.

The PHP code is wrapped in <?php to ?> (to avoid it simply being displayed on the screen!) but that's not required for the Perl, and the #! line confirms that the Perl was installed at the time the system was build but the PHP had to be added later - but then it was added as part of the web server so that's not a serious extra, really!

And the similarities are more striking that the differences! Both languages using $ for variables, ; for statement separators, { ... } for blocks of code. Both case sensitive in variable names, both with auto vivification (i.e. creating variables on the fly) and auto typing ...

My conclusion for my client? Use Perl if you're doing a lot of this sort of thing, and PHP if it's just the occasional pieces of code as a sideshoot from your web work. But that's just the solution for HIS particular metrics. For a delegate who also knows Lua or Python or Ruby or Tcl, those languages would also provide good solutions to the example application above (and I'll teach to any of them!). But for this particular application, I would probably fight shy of using Java, C or C++ - all excellent languages for other jobs, though!