Problems like this will often be down to the ownership of the file or the permissions of the user owning the file. Remember that when running as a CGI program your code almost certainly runs as the web server user, not as you.
But this is all just guesswork. As moritz says, the only way to be sure is to look in the error log.
Update: I've just remembered - This is a FAQ.