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

Can someone tell me why I'm getting premature end of script header warnings while running this?
#/usr/bin/perl -w use warnings; use strict; use lib qw(/home/public_html/lib/); use CGI::Carp 'fatalsToBrowser'; use Net::DNS; use POSIX; use CGI; use CGI qw(:standard); print header, start_html; my $rr; my $res = Net::DNS::Resolver->new; my $query = $res->query("http://www.yahoo.com", "NS"); if ($query) { foreach $rr ($query->answer) { next unless $rr->type eq "NS"; print $rr->nsdname, "\n"; } } else { print "query failed: ", $res->errorstring, "\n"; }

Replies are listed 'Best First'.
Re: Premature End of Script Headers
by hv (Prior) on Apr 30, 2003 at 03:35 UTC

    When I try and run it here, the only thing that stops error messages getting to the browser is the missing "!" in the first line. Try changing it to:

    #!/usr/bin/perl -w

    Hugo
Re: Premature End of Script Headers
by jkahn (Friar) on Apr 30, 2003 at 03:39 UTC
    well, when I run it, the two $rr tokens that come back from the $query->answer call are both of type CNAME, not NS. So nothing comes out of the script except the header, since you never hit another print statement.

    DB<1> c 21 DB<2> x $query->answer() 0 Net::DNS::RR::CNAME=HASH(0x8755fc8) 'class' => 'IN' 'cname' => 'rc.yahoo.com' 'name' => 'http://www.yahoo.com' 'rdata' => "\cBrcÀ\cW" 'rdlength' => 5 'ttl' => 1759 'type' => 'CNAME' 1 Net::DNS::RR::CNAME=HASH(0x8755f98) 'class' => 'IN' 'cname' => 'rc.yahoo.akadns.net' 'name' => 'rc.yahoo.com' 'rdata' => "\cBrc\cEyahoo\cFakadns\cCnet\c@" 'rdlength' => 21 'ttl' => 1759 'type' => 'CNAME'

    But I'm just running it from the commandline; haven't tested it in CGI environment proper.

    Update: hv is right, the shebang line is funky too. I was running it with explicit call to perl cgi-test.pl from within emacs, so I didn't notice.

Re: Premature End of Script Headers
by hardburn (Abbot) on Apr 30, 2003 at 04:09 UTC

    The most obvious thing to me is that you have quite a lot of redundancy in there. use warnings does everything -w does, plus some (I recall hearing that some people consider -w to be broken, but I don't remember the reasons behind this). Also, you don't need to put two use CGI lines in there--all you need is the one that has the qw(:standard).

    Not sure if this will actually fix your problem, but its a start.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Premature End of Script Headers
by ScooterQ (Pilgrim) on Apr 30, 2003 at 13:19 UTC
    In an answer only *slightly* related to what you're asking about, you should be able to gleen some sort of clue as to what is happening form your web server error logs, assuming you have access to them. The logs should show you the output that the script produced prior to the headers that resulted in the "premature end of script headers" message.