Wednesday, May 25, 2005

Jesus Hates Perl

My pal Jesus hates perl. He's a Java guy and Java is the only language he likes. That's okay, he's still my pal anyway! But I feel the need to respond to his blog, as it is, of course, wrong, and he likely is complaining about code I wrote!

First he complains about infix conditionals, such as:


return 0 unless $condition;


There are a couple of reasons this pops up in perl. One is that you can't say this:


if ($condition)
foo


I know, I know, it's weird, but you have to use braces, even if the block is just one line. That's a bit annoying, so people often will reduce it to an inline conditional (not just with return, but with other clauses as well).

The other reason -- and the reason I tend to use infix conditionals -- is that they let you emphasize the result over the condition. In most languages, the condition always comes first, inside the if clause. But it isn't always true that the condition is the more important part. For example:


print "The printer is on fire!\n" unless $quiet;
$obj->foobar() if $obj->can("foobar");


The more important thing here is the message, not the check, and so you see the message first. This is more a stylistic issue, but perl lets you be expressive, giving you the ability to emphasize one point over another. I like this, too, as I think it actually can lead to more readable code. Read this aloud and you will see the perl version is more natural-sounding:


if (debugging) {
printf("Something broke, check your stack\n");
}

vs

print "Something broke, check your stack\n" unless $debugging;


He also complains about types in perl; well, many other people have debated strict typing vs loose typing, and I'll let them wage that war.

His last complaint is about grep. Argh! grep is a wonderful function, because it is one of the parts of perl that allows for a more functional-style of programming. I guess Jesus doesn't like functional programming, which is a pity, because functional programming can be very handy (and, not to mention, very efficient).

But Jesus is a Java programmer, so functional programming is all but foreign to him, as is the 'relax, types will figure themselves out' approach of loosely typed languages.

But that's okay! Jesus is still my pal!

1 comments:

Greg Stein said...

That's bunk. The code is only going to execute if the condition holds. Thus, that conditional is the FIRST thing that you want to know about.

"My printer is on fire? Holy crap! oh, no... wait a sec. not always. whew. DAMN IT! Don't scare me like that!"

Perl's infix tells you the stuff in the wrong order.