Pop Quiz Time!

Assuming no bugs in the code or anything done "fancy", is the following code Perl?

$cssClass = "";
if ( $element->isOpen() ) {
    $cssClass = ' class="selected"';
}
if ( !$element->isHiddenInNavigation() ) {
    $filepath = $element->getId() . ".html";
    if ( substr( $element->getFilePath(), 0, 4 ) == 'http' ) {
        $filepath = $element->getFilePath();
    }
    $result .= '<li' 
      . $cssClass
      . '><a href="'
      . $filepath . '">'
      . $element->getLabel() . '</a>'
      . $this->generate( $element->getChildren() ) . '</li>';
}

Look at that code carefully. Really carefully. If we assume it does not have bugs, then we know this is probably not Perl (I'm not telling you the giveaway in case it takes you a while to see it).

That language is PHP, yet clearly the sigils haven't killed PHP. Due to PHP's extremely arbitrary case and argument ordering conventions, it seems to require more memorization than Perl does, thus suggesting that for two cleanly written pieces of code, the PHP might actually be harder to understand than Perl. So why have they cleaned Perl's clock in the Web space? I think there are two reasons.

First, when Perl hit it big in the late 90s, we were the easiest way to get CGI code up and running fast. PHP clearly wins in this area now.

Second, let's take a look at some more PHP, something which makes the distinction much clearer:

public function setHiddenInNavigation($hideInNavigation) {
    return $this->hideInNavigation = $hideInNavigation;
}

Can you do that in core Perl? No, you can't. It's an embarrassment. We're harder to use for the uses many (most?) people want and our language is so primitive in one of the most fundamental ways that we sort of deserve some of the harshness we get. Moose (and maybe MooseX::Declare) needs to be in the core, or at least something along those lines.

1 Comment

"But it's so much more flexible to roll your own parameter handling! Also remember backwards compatibility with Perl 5.000 programs written in 1994, or programs written with a Perl 5 version which didn't have a syntax error for unknown symbols in parameters! As well, any design decision brought into the Perl 5 core has to stay in its original form forever, as there's no chance of ever changing it! Do you want to break code? Why do you hate freedom? Go back to Java!"

If anything, the embarrassment is that not enough people find those ridiculous arguments I don't believe from the previous paragraph embarrassing.

Leave a comment

About Ovid

user-pic Freelance Perl/Testing/Agile consultant and trainer. See http://www.allaroundtheworld.fr/ for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book! http://www.amazon.com/Beginning-Perl-Curtis-Poe/dp/1118013840/