Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Why am I getting "premature end of script header"?

by ikegami (Patriarch)
on Nov 27, 2006 at 22:19 UTC ( #586342=note: print w/replies, xml ) Need Help??


in reply to Why am I getting "premature end of script header"?

That error means something (such as an error message) was printed before the header was printed. In this case, an error is printed before compilation of the script is done, and the header is only outputed after the script is done compiling.

The content of eval BLOCK is compiled when the rest of the script is compiled, so the use in the eval BLOCK (and anything in the used file) will be executed before you send the header.

You could send the header sooner (using BEGIN), or you could use eval EXPR instead of eval BLOCK.

The following executes everything in the same order as if the error checking wasn't there (i.e. modules are loaded before CHECK is called), but dies gracefully if a module can't be loaded.

#!/usr/bin/perl BEGIN { eval (" use LWP::UserAgent; use HTTP::Request::Common; "); if ($@) { print "500 Internal Server Error\n"; print "Content-type: text/plain\n"; print "\n"; print $@ die($@); # For the log file. } } print "Content-type: text/html\n\n"; my $ua = LWP::UserAgent->new; my %post = (...); my $response = $ua->request(POST "http://www.example.com/", [ %post ]) +; print $response->as_string;

By the way, it's $@, not @$.

Replies are listed 'Best First'.
Re^2: Why am I getting "premature end of script header"?
by lokiloki (Beadle) on Nov 27, 2006 at 22:27 UTC
    Thanks very much... I appreciate your help. So the following would be a fine way to test if a module is available and either use it or not use it accordingly?
    print "Content-type: text/html\n\n"; print "here we go again..."; &a; sub a { BEGIN { eval ("use fakemodule;"); if ($@) { print "error happened $@"; } else { print "error didnt happen, do some stuff"; } } }

      My solution already does exactly that. Just replace the body of if ($@) with whatever you want.

      What you have there is the same as

      eval ("use fakemodule;"); if ($@) { print "error happened $@"; } else { print "error didnt happen, do some stuff"; } print "Content-type: text/html\n\n"; print "here we go again..."; &a; sub a {}
        thanks... so the BEGIN blocks should be at the beginning of the script... since i am using lots of calls to lwp, etc, i suppose something like the following would be a good idea:
        BEGIN { eval (" use LWP::UserAgent; use HTTP::Request::Common; "); if ($@) { $lwpavailable = 0; } else { $lwpavailable = 1; } }
        and then when I want to use it just test for that variable?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://586342]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2022-08-08 05:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?