another plea to test and use strict

rjbs on 2008-08-07T16:06:00

A few months ago, I spend a few days (yes, days) rewriting an extremely complex set of procmail programs into modern Perl. Replacing procmail with Moose? Priceless.

Anyway, the procmail program largely existed to send mail to a program written for Perl 4. It had no strictures and no useful tests.

When I finally threw the big switch and started to send mail through the real Perl 4 program instead of the mock version, I ended up with mail records like this:

From: icardo Signes   Subject: his is a test

What? Why did that happen? Well, this line exists in the code.

$subject = substr($subject, 1) if ($subject[0] == " ");

The intent is "clearly" to remove a single leading whitespace if it exists. Of course, that's not what the code actually says. It says, "If the first element of an array called @subject is a single space, then remove the first character from the scalar $string."

This didn't throw a compile-time error (about @subject not being declared) because the program didn't (and more or less cannot, yet) use strict. This didn't display any bad behavior because all of the program's input had one leading whitespace.

The fix was easy. I threw in a `s/^\s+//'. Still... what an excellent demonstration of the power of strictures to remind you that HEY! you're programming Perl here, not Python.


Huh? ==?

autarch on 2008-08-07T17:04:38

Don't you mean "eq" too?

Re:Huh? ==?

rjbs on 2008-08-07T17:30:58

The person should've used eq, but then it would've failed. @string didn't exist, so $string[0] was undef, which is ne " ". They used ==, though, so they got undef-as-num (0) == " "-as-num (0) and it was true.

Lots of nice bugs in that one line.