Perl, PHP and Sour Grapes?

Search CPAN (Perls repository of re-usable modules) for “PHP” and you might be surprised by the results.

For example PHP::Strings. What’s interesting about this module is it partly implements PHP’s string functions in Perl, or tells you where to look for more info.

The following code, for example;


#!/usr/local/bin/perl -w
use strict;

use PHP::Strings qw( :trim );

my $word = " Hello World! ";
$word = trim($word);

Results in the following message;

.

…and refers you to the Perl FAQ on How do I strip blank space from the beginning/end of a string?. Nice stuff if you’re a PHP coder hacking away in Perl.

What’s not so nice is the related reading which links to a virtual web ring of “I hate PHP” rants. Trawling through the list, there are some valid technical points against PHP but largely mixed with misconceptions resulting from common “gotchas” that afflict Perl coders, when getting started with PHP. Although emphasis is technical comparison, the impression is that there’s some, more profound (irrational?) reason why (some) Perl coders feel the need to take a shot at PHP.

Why?

It’s tempting to suggest that some feel PHP stole Perl’s “rightful place” as the #1 web language. “Sour grapes” in other words but perhaps that’s unfair.

May be it’s because (superficially) Perl and PHP look too similar (sibling rivalry etc.). These days don’t think that’s true though. PHP has distinguished itself as a specialized language for solving the “web problem” while Perl remains a powerful, general purpose language. And although PHP was originally Perl-inspired, Java seems to be a bigger factor in PHP’s current design.

Have to confess I’m not Perl’s biggest fan, although I’d choose it (or better yet, Python) over PHP for tasks like batch processing and networking.

To me Perl offers too many opportunities to write unreadable code. The trim() function, that PHP::Strings refuses to implement, highlights the issue. What makes more sense (to a human being)?


$string = trim($string);

or


$string =~ s/^s+//;
$string =~ s/s+$//;

But that’s just my own preference.

Anyway, in the end holy wars lead nowhere. More useful is this issue of the Perl Review, which does an excellent, side by side, comparison of Perl and PHP which makes a good starting point if you know one and need to work with the other.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://boyohazard.net Octal

    I don’t like Perl much either. Apart from the ‘unreadable code’ syndrome I inevitably end up fiddling with file and directory permissions on the server – not what I want to do.

    As I learnt Java first, I have noticed that switching between that and PHP is almost seemless. So I’d have to agree with your statement that Java is a larger factor in PHP’s development than Perl was/is

  • Petras Kudaras

    Hello, I am a current maintainer of PHP::Strings. I have only taken this job very recently as a previous maintainer, Iain “Spoon” Truskett, has sadly passed away.

    PHP::Strings was largely a joke module, but it has turned into something potentially useful, and I do intend to make it even more so. Just give me some time to wade through unneccessary rants and actually implement the PHP functions ;-)

    There is also a plan to make even more PHP-like functions available in Perl, I know that some people are working on PHP::MySQL module, which would implement PHP MySQL functions.

    I am tempted to answer your rant about relative merits between Perl and PHP, but I’ll try to stay away from holy wars this time. I am sorry if you were offended by PHP::Strings attitude — it was meant to be a joke module. And I do repeat myself that I will try to make it more useful.

  • Duncan

    I resent the implication that I am not a human being, since I find the Perl form easier to understand. It says remove one or more whitespace characters from the beginning of a string, then remove one or more whitespace characters from the end of a string. What does ‘trim’ mean? When I get a trim at the barber he doesn’t cut my hair from both ends. And while that may seem like a contrived example, when I started with PHP I had to consult the manual several times just to be sure that a function did what I thought it should.

    As far as personal preference goes, what’s wrong with this?

    sub trim {
    $_[0] =~ s/^s+//o;
    $_[0] =~ s/s+$//o;
    $_[0];
    }

    now $string = trim($string) works just fine.

    Not trying to start any holy wars – I work with Perl and PHP and fell I therefore have the right to trash both of them :)