Sharon Machlis

Perl vs. PHP vs. Ruby

January 24, 2010 1:52 PM EST

Noodling around on a project I'm interested in coding in-house -- showing a list of which Computerworld.com stories we've already Tweeted about and which ones have yet to be mentioned  -- has crystallized what I like best and least about three scripting languages I turn to most frequently: Perl, PHP and Ruby.

Perl advantage: The modules, the community. This project may seem like a straightforward comparison of two lists, Computerworld's RSS feed vs. Computerworld's Twitter feed. However, I can't compare URLs straight up, since the Twitter feed - given Twitter's 140-character message limit - almost always includes shortened URLs, usually using bit.ly. So first, I needed to find a way to expand shortened URLs from bit.ly -- or anywhere else.

Sure, I could dig around to find a third-party API that does so and then code to it, but knowing the depth and breadth of the Perl community, why not start off at CPAN and see if anyone's already created a module to do just that? And, not surprisingly, someone has: WWW::Lengthen. Install the module, add two lines of code and I'm done.

I did indeed find code on the Web to do the same thing in PHP and Ruby - and there very well may be someone who's packaged that Ruby code into a Ruby Gem -- but in general, because of Perl's larger user base and longer history, I'm most likely to find ready-to-use code for a specific task in Perl.

I'm also most likely to find someone ready to answer my questions. In tweeting my lament about a lack of a built-in Perl function to check whether an array contains a specific element (both Ruby and PHP have one), I received an answer within minutes on how to do so in Perl. I'm a Ruby fan for other reasons, but when it comes to existing code and community support, Perl is just plain impressive.

Perl disadvantage: Syntax. Perl enthusiasts revel in the fact that there are usually countless ways to approach the same problem. But Perl syntax can be difficult to handle not only for an occasional coder like me, but programming pros. I was chatting with an IT consultant last night who likes Perl for its power and flexibility, but admitted the creativity Perl offers can make it tough to maintain and update someone else's code.

"Only someone else's code?" I asked him. Six months later, can you read your own code and remember what the heck it was you were doing' No, he admitted, he often can't. Perl requires an enormous amount of commenting in order to know what it is the code was written to accomplish.

Ruby advantage: Human-readable code. Here's how to check in Ruby if the array "a" contains the element "x": a.include?("x"). This is breathtaking simplicity compared to the response on performing the same check in Perl: @a = map { if (EXPR) $_ } @YOURARRAY . I can tell you the odds of me remembering what that is doing six months later. (Update: There's a discussion of several other ways to achieve this in the comments below, including smart matching available in current versions of Perl).

Ruby advantage: OOP. Yes, Perl and PHP both have added robust object-oriented programming capabilities, but it's just not the same as Ruby, which was created at the outset for OOP. In Ruby, everything is an object with methods. MyVariable.length gives you, well, the length of a string or how many elements are in an array. MyVariable.class tells you what class it is. These methods also work on values and not only variables, such as 153.class or 126.even? MyArray.empty? tells you whether or not your array includes elements. It's all very logical and human readable.

PHP advantage: quick and dirty Web apps. I'd put PHP somewhere in the middle between Perl and PHP Ruby in terms of code readability; but when it comes to coding and posting a rudimentary Web-based tool for our staff in-house, PHP is my go-to scripting language of choice. I'm not talking here about a full-blown Web site; I mean something like: Enter your story information and I'll return you some suggested keywords.

Just as Ruby started out as an OOP language, giving it an advantage there, PHP began life to solve a Web problem (PHP did once stand for Personal Home Page) and it shows. It is trivial to mix PHP code in with a basic HTML page.

While it's certainly possible (and not all that tough) to use Perl on the Web as well, having coded both, I give PHP the nod in terms of doing it easily and elegantly. Embedded Ruby offers similar capabilities to PHP in terms of mixing Ruby and html on a page, but it's simply not very widespread; I'm much more likely to have access to a Web server with PHP than Embedded Ruby. And while I like Ruby on Rails for serious Web functionality, I find it overkill for a couple of editing tools.

Text manipulation and comparisons: It's all good. Emotionally, I give the nod to Perl for text manipulation. It's excellent for the task and has been from day one, and we have Perl to thank for programming languages offering powerful regular expression syntax. Truthfully, though, I've had good success and not much difficulty using all three languages to search and edit text and compare text files. If that's your project and it's not incredibly complex, I think personal preference on syntax and environment should win out.

But ah, to have a language with the community and power of Perl, the readability and OOP of Ruby and the html integration of PHP....

Sharon Machlis is online managing editor at Computerworld. You can follow her on Twitter Twitter@sharon000, send her e-mail at smachlis@computerworld.com or subscribe to her RSS feeds:
articles Machlis RSS | blogs Machlis RSS.