Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Perl v PHP, choosing the right language

"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!
(written 2008-08-14)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H101 - Introduction to PHP
  [4118] We not only teach PHP and Python - we teach good PHP and Python Practice! - (2013-06-18)
  [3025] Learning to Program ... in PHP. Course examples. - (2010-11-01)
  [2097] PHP Course - for hobby / club / charity users. - (2009-03-22)
  [1958] PHP - Parse error: syntax error, unexpected $end ... - (2008-12-23)
  [1717] Q - Should I use Perl or Python? - (2008-07-23)
  [1198] From Web to Web 2 - (2007-05-21)
  [1050] The HTML++ Metalanguage - (2007-01-22)
  [949] Sludge off the mountain, and Python and PHP - (2006-11-27)
  [924] The LAMP Cookbook - Linux, Apache, MySQL, PHP / Perl - (2006-11-13)
  [917] Syntax checking in PHP - (2006-11-07)
  [846] Is Perl being replaced by PHP and Python? - (2006-08-27)
  [795] Remember a site's non-technical issues too - (2006-07-07)
  [789] Hot answers in PHP - (2006-07-02)
  [712] Why reinvent the wheel - (2006-05-06)
  [691] Testing you Perl / PHP / MySQL / Tcl knowledge - (2006-04-19)
  [646] PHP - London course, Melksham Course, Evening course - (2006-03-14)
  [629] Choosing the right language - (2006-03-01)
  [624] It's REALLY easy to add a little PHP - (2006-02-26)
  [577] Learning to program in Perl or PHP - (2006-01-26)
  [433] FTP - how to make the right transfers - (2005-09-01)
  [382] Central London Courses - Perl, PHP, Python, Tcl, MySQL - (2005-07-18)
  [380] Bridging to the customer requirement - (2005-07-16)
  [341] Happy Birthday, PHP - (2005-06-09)
  [317] Programming languages - a comparison - (2005-05-20)
  [135] Too many Perls - (2004-11-30)
  [132] Portrait of the author - (2004-11-27)
  [124] PHP v Java - (2004-11-20)
  [93] Case Sensitive? - (2004-10-19)
  [55] Evening classes to learn PHP - (2004-09-19)
  [48] PHP - onwards and upwards - (2004-09-14)

P201 - Perl - Introduction
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [2812] What is Perl? - (2010-06-15)
  [2070] Converting to Perl - the sort of programs you will write - (2009-03-08)
  [1852] Perl and Blackberries - (2008-10-23)
  [743] How to debug a Perl program - (2006-06-04)
  [604] Perl - multiprocess applications - (2006-02-13)
  [594] Twice is a co-incidence and three times is a pattern - (2006-02-07)
  [25] Release numbers - (2004-08-23)

P701 - What is Perl?
  [3446] Awk v Perl - (2011-09-18)


Back to
Dynamic maps / geographics in PHP
Previous and next
or
Horse's mouth home
Forward to
Upgrade from PHP 4 to PHP 5 - the TRY issue
Some other Articles
Preserved railways - struggling to the future?
Ever had One of THOSE mornings?
Istanbul
Upgrade from PHP 4 to PHP 5 - the TRY issue
Perl v PHP, choosing the right language
Dynamic maps / geographics in PHP
Public Training Course Dates until July 2009
Glorious (?) 12th August - what a Pe(a)rl!
Using server side and client side programming together
Hotel room prices - Melksham, Wiltshire
4318 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1753_Per ... guage.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat