Sunday, March 13, 2005
Too many emails
Email.
Or rather, an abundance of email.
Smirk called, saying that one of our servers was dying under a severe server load, and asked if I could write a script to clean out the sendmail queues.
So I spent some time writing some Perl code to do just that.
I don't like Perl. To me, it represents the worst of
/bin/sh, awk, sed and
grep (among other throat clearing noises that represent Unix
commands). But for one off stuff like this, it was probably quicker than
trying to do this purely under /bin/sh or even C.
In retrospect, I think I could have written it faster in C.
First off, I write my Perl using “strict”; it's enough that Perl will
gladly accept simple typos (unless in “strict” mode). But that lead to
problem number one: I couldn't figure out how to use
Getopt::Std (Perl code to parse the command line) in “strict”
mode. I had to spend some time figuring out that the correct incantation to
use would be:
use Getopt::Std;
use strict;
use vars qw($opt_h $opt_b $opt_d $opt_t $opt_D);
getopts('h:b:d:t:D');
(in this code, -h is to scan the headers, -b to
scan the body, -d to specify the queue directory and
-t to specify where any email matching should be copied to, and
-D to output debugging messages)
Okay, next problem: it's not finding anything. I grep through the headers, find something like “Subject: AdD Thr33 IncHes 2 ur MOrtgAge” then run the script:
./movequeue.pl -h 'Subject: .*[Mm][0Oo][Rr][Tt\?][Gg][Aa@][Gg][Ee3]' -D'
And nothing shows up.
Debug debug debug.
Turns out I'm a bit overzealous with using “my” (which is used to declare which variables I'm using in “strict” mode). Each instance of “my $variable” causes it create a whole new variable. So of course:
foreach my $entry (@queue)
{
if (my $entry =~ /[dqx](.*)/)
{
# blah blah blah
}
}
isn't going to work, since I effectively get two new variables by the name
of $entry.
Okay, declare all the variables I use in one spot. That fixes that problem, and a whole slew of other problems related to scoped variables.
But at least the code is working.
Why yes, I'm still learning Perl.
And I still don't like it.
![Glasses. Titanium, not steel. [Self-portrait with my new glasses]](https://www.conman.org/people/spc/about/2025/0925.t.jpg)