I learned a long time ago that the key to debugging applications -- esp. CGI applications -- is to log information about the state of the program to a file. Using an in house logger with the ability to change verbosity (preferably from the program, i.e. so you can have somebody e-mail you the logs if there are problems) allows looking at a script and seeing exactly what is going on. It can be a pain setting it up, but there is nothing sweeter then saying "there's been 2 emails, and we see from the log that function send_email was called twice"
Want to support the EFF and FSF by buying cool stuff? Click here