Stylish

Fri, 21 Jan 2005

If you tried to submit a comment a couple of hours ago, you probably noticed that the script formerly known as mt-comments.cgi was producing nice fat 500 server errors. Yup, that was me, tweaking (as ever) without having any clue what I was doing. Great learning experience, bad customer service. Bad Laurabelle.

(Note to self: Make better testing environment ASAP.)

What was I so clumsily tweaking? I was trying another tactic on the same problem I had trouble with earlier. The task was to translate a certain script from PHP to Perl, and it was harder than I had thought it would be; or rather, the parts I thought would be hard were easy, and parts I thought would be easy were very difficult.

The code in question is what I use for determining what style my site will use on any given occasion. The algorithm is fairly simple. If today is a holiday for which I have defined a stylesheet, use that style. Otherwise, if the browser has a cookie for the visitor's favorite style, use that one. If neither of those pans out, then use a seasonal style. (By the way, the form for setting a cookie is on the front page, at the bottom of the left sidebar.)

It turns out that taking my Perl code out of the MT template and sticking it in a plugin doesn't make it any more correct, but it did sort of make it easier to debug. That is, until I stopped getting reasonably comprehensible parsing errors and started getting server errors.

The log entry for these errors looked like this:

[Fri Jan 21 20:23:39 2005] [error] [client xxx.xxx.xxx.xxx] malformed header from script. Bad header=<!-- Script and navigation hea: LBM-comments.cgi, referer: http://www.niceperson.org

I had a beast of a time figuring out what the problem was, because to put it mildly, the MT plugin interface is not terribly well documented, at least for people who don't want to pay for it. So I bumbled along on my own and eventually worked it out. For those of you who find yourselves in my situation and have found my site by googling malformed header from script, I had to fix two things that were not obvious to me. First, make sure there is a line:

1;

at the end of your file. I don't really have a clue what this does, but it's important. Secondly, don't try to print your output; return it.

Once I got that bit worked out, the page loaded, but without styles. If you came along at that point, a plain white page was all you saw. First it was completely unstyled, and then it was merely albino, and then I finally cracked it. I lost count of how many bugs there were in my initial code, but there were a lot of them.

In fact, the cookies still don't work, but I'm not too surprised about that because my cookie fu is not very strong. It works in PHP (except my cookie-deletion code never worked, I don't know why) but not in Perl, but I'm not going to worry terribly much about it, because I suspect that nobody uses that feature anyway, and does it matter so much if the comments pages are a different style from the rest? (However, if someone with more plugin-fu and cookie-fu and, I dare say, Perl-fu would care to look at some code that almost-works, I won't refuse.)

I call my plugin MTStylish. If anyone thinks s/he would find it useful or interesting, I would be glad to share.