Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

rjbs (4671)

rjbs
  (email not shown publicly)
http://rjbs.manxome.org/
AOL IM: RicardoJBSignes (Add Buddy, Send Message)
Yahoo! ID: RicardoSignes (Add User, Send Message)

I'm a Perl coder living in Bethlehem, PA and working Philadelphia. I'm a philosopher and theologan by training, but I was shocked to learn upon my graduation that these skills don't have many associated careers. Now I write code.

Journal of rjbs (4671)

Thursday August 07, 2008
11:06 AM

another plea to test and use strict

[ #37123 ]

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Don't you mean "eq" too?

    • 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.

      --
      rjbs