in reply to Premature end of script headers
You could try moving the print closer to the start of the program, but even better would be
use CGI::Carp qw/fatalsToBrowser/; [download]