Main Content

Perl, PHP or Python? No - Perl AND PHP AND Python!

Archive - Originally posted on "The Horse's Mouth" - 2008-01-20 21:18:56 - Graham Ellis

"Which language is best for xxx task" I am often asked. And I'll listen to the questioner's description of his application, his background, and the sort of support he'll have at his place of coding before coming up with a suggestion. Never would I suggest he learn all three.

And yet, today, I found myself with a little task to do - it took me no more that a few minutes - and I used all three languages without even thinking about it.

The task. A 900 Mbyte web log file - recording all the traffic from the last year at our First Great Western Customer Forum and a requirement to get some information out of it about the geographic spread of the visitors.

1. A little bit of PERL (the Practical Extraction and Reporting Language) to pull all the different IP addresses that have visited the site out of the file.

#!/usr/bin/perl -na
$counter{$F[0]}++;
END {
@hl = sort keys %counter;
print $#hl+1," unique hosts\n";
foreach $ak(@hl) {
  print "$ak $counter{$ak}\n";
}}



2. A PHP script - as we have the PHP / MaxMind routines easily accessible - to add the country of origin for each visitor to the data (that's right - PHP running from the command line!)

<?php
include("../../include/stdphp.v8");
print "never mind!\n";
foreach (file("uniquehosts") as $line) {
  $lns = explode(" ",$line);
  $coco = getcountry($lns[0]);
  if (ereg("...",$coco[id])) {
  print "$coco[id] $line"; } else {
  print "XXX $line"; }
}
?>


3. Another piece of Perl to count the number of visiting IP addresses from each country

while (<DATA>) {
  ($c,$ip,$k) = split;
  $byc{$c}++;
  $byk{$c}+= $k;
}
foreach $co(sort {$byc{$b} <=> $byc{$a}} keys %byc) {
  print "$byc{$co} $co\n";
}

__END__
XXX 116.122.81.28 32
XXX 116.199.208.54 7
XXX 116.214.24.78 34
etc


4. and finally Interactive Python in place of a calculator to do some final statistical arithmetic on the results.

OK - I'll admit it; I got caught miscoding once by the different format of foreach in Perl and PHP, but otherwise it all worked pretty well. If you want to see the information extracted by this process, look here. And, no, I'm not really proud of the beauty of my code; written to fulfil a quick one-off hacking job.

If you think I've not given Python a fair run above, you're right. But then I have been using it all day as a notepad as I do my final personal tax calculations, with all the additions keyed in so I can go back and check my sums as I may need to over the next few days. That's code and numbers that I won't be sharing here, of course!