Angharad has asked for the wisdom of the Perl Monks concerning the following question:

Hi there
I'm having some trouble with a cgi script. I'm getting the 'Premature End of Script Headers' error message and it's getting to be very frustrating.
This is a test script at present. The input_file variable will eventually be passed to the script via another one ... but for the moment its being invoked via redirection from a html page.
Basically, it runs an executable (a C program) using the input_file as input. It results in the generation of a number of text files.
When run locally from the command line, the executable program does print to STDOUT so I was wondering whether this may be causing my error. Anyway, this is the script as is (which by the way has been developed with the kind help from a perl monk already).
#!/usr/bin/perl use strict; use warnings; use CGI qw/:standard/; # Upload directory for files my $upload_dir = "uploadpath"; # Path to processing program my $program = "path/processprogram"; my $input_file = "inputfile.txt"; die "Usage: $0 INPUT_FILENAME\n" unless $input_file; die "Can't process '$upload_dir/$input_file': File does not exist!\n" unless -f "$upload_dir/$input_file"; # Create web page... print header, start_html("Processing file '$input_file' ..."), $/, h1("Processing file '$input_file' ..."), $/; chdir $upload_dir; open (PROG, "| $program") || die "Can't run '$program': $!\n"; print PROG "$input_file\n"; close(PROG); print p("OK"), end_html();
Any comments, suggestions much appreciated

Replies are listed 'Best First'.
Re: Premature end of script header error
by holli (Abbot) on Feb 15, 2006 at 12:46 UTC
    How about using Super Search with the phrase you used for you title? That question has really been answered about thousand times.


    holli, /regexed monk/
Re: Premature end of script header error
by marto (Cardinal) on Feb 15, 2006 at 12:49 UTC
Re: Premature end of script header error
by wfsp (Abbot) on Feb 15, 2006 at 12:45 UTC
    Are the line endings correct? The shebang line pointing to the right place? Have you tried adding
    use CGI::Carp qw( fatalsToBrowser );
    to the top of the script?

    What happens if you comment out the

    open... print... close...
    lines?

    You'll have at least eliminated a few possiblilites!

    Hope this helps.

    Update

    This will help. It mentions checking the scripts permissions (which I forgot).

Re: Premature end of script header error
by derby (Abbot) on Feb 15, 2006 at 13:12 UTC

    Check your server error logs (or use CGI::Carp's fatalsToBrowser if you don't have easy access to the logs). I would assume you're die'ing (permission issues) and hence not seeing the error.

    -derby
Re: Premature end of script header error
by CountZero (Bishop) on Feb 15, 2006 at 14:15 UTC
    A 'Premature End of Script Headers' error message is typically seen when your CGI-script prints something before the usual HTML-headers were issued. Many times (but not always) this is due to some error occurring and the error-message send out instead of the usual headers.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Premature end of script header error
by olus (Curate) on Feb 15, 2006 at 14:30 UTC
    Try adding the following line to your program before outputing any html.
    print "Content-type: text/html\n\n";
Re: Premature end of script header error
by Angharad (Pilgrim) on Feb 15, 2006 at 12:52 UTC
    Thanks for your comments so far. Yes, I've already done the super search thing on here and googled for possible answers elsewhere. But I'll look again anyway.