Insane Perl Programming

No surprise that Perl has such a bad image, probably because it just makes things too easy. And too obvious to make wrong.

Look at the following CGI code snippet, which I am about to reengineer into a tiny OGC SOS framework:

my $request = "";
$request = $inparams{REQUEST} if ( $inparams{REQUEST});
$request = 'GetCapabilities' if ! $request;

That tries to solicit a value from %inparams and if that does not exist, use a default. Sounds like a sentence, right? So it is actually also one line in Perl:

my $request = $inparams{REQUEST} || 'GetCapabilities';

This is directly followed by this heavy-handed exception trigger

die "Unknown request '$request'"
    if ( $request ne 'GetCapabilities'
     and $request ne 'GetObservation'
     and $request ne 'DescribeSensor');

which can be shortened at least a bit without the need to read the line twice:

die "Unknown request '$request'"
    unless grep { $request eq $_ } qw(GetCapabilities
                                      GetObservation
                                      DescribeSensor);

But brutally ignorant is the following loop:

my $results = "";
foreach (@input_data){
        chomp;
        $results .= $_ . ' ';
}
$results =~ s/ $//;

It accumulates strings from @input_data into one variable whereby after any trailing CRs are dropped, blanks are injected in between them.

Dude, this is Perl territory:

my $result = join ' ', map { chomp } @input_data;

And it also gets rid of the awful oh, I appended something at the end and have to clean up my mess line.

And, please, do not name variables in plural (such as $results) when there can only be one value. One value: use a scalar. Many values: use a list. It is not really rocket science.

The good message, though, is that all these programmers now have moved on. And hopefully have moved on to Java. There, at least, insanely convoluted code seems to be the norm.

All's well that ends well.

Posted In

Applying technology

It is always about the application of technology and not technology itself. Same goes for Java.

It would be good to see the complete code section before and after. This would make the example even more impressive. It cuts from 13 lines to 6 and it increases readability. At least for an experienced programmer.

Robert Cerny (not verified) | Thu, 05/08/2008 - 09:30

Re: Applying technology

It is always about the application of technology and not technology itself. Same goes for Java.

That is certainly true, but different languages give people different ways to shoot themselves into the foot.

B&D (Bondage & Discipline) languages like Java limit the ways apriori. Which, sort of, is their reason of existence. With languages such as Perl, which sport immense flexibility and freedom a mediocre programmer can do a lot of harm.

Was there. Done that. Some say, I'm still there :-)

It would be good to see the complete code section before and after. This would make the example even more impressive. It cuts from 13 lines to 6 and it increases readability.

What actually happens, is that after an initial cleanup, the whole design problems spring right into your face. So you start to revamp the code until you end up with a few lines.

Recently I rewrote a whole OGC/SOS server into something with 3 lines (ok, I was cheating by using Topic Maps and TMQL underneath).

rho | Thu, 05/08/2008 - 09:51

Holes in the foot

That is certainly true, but different languages give people different ways to shoot themselves into the foot.

Many beginning programmers are so happy to finally get their finger on the trigger that they immediately shoot. They just did not know that they were aiming at someone's foot. Much later they realize that this someone's foot became their own foot. But then they are professional programmers and now they still shoot at their foot, because they are made to pull the trigger by some suit. The sad part is that holes in the foot are ok, as long as you wear shoes. And if you are Microsoft, you do not even need to wear shoes. Nor pants. But a shirt, a jacket, and a tie.

Robert Cerny (not verified) | Thu, 05/08/2008 - 11:15

Re: Holes in the foot

The sad part is that holes in the foot are ok, as long as you wear shoes. And if you are Microsoft, you do not even need to wear shoes. Nor pants. But a shirt, a jacket, and a tie.

Hehehe. This explains much about their promotional videos...

rho | Thu, 05/08/2008 - 20:39