http://qs321.pair.com?node_id=586346


in reply to Re^2: Why am I getting "premature end of script header"?
in thread Why am I getting "premature end of script header"?

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 {}

Replies are listed 'Best First'.
Re^4: Why am I getting "premature end of script header"?
by lokiloki (Beadle) on Nov 27, 2006 at 22:49 UTC
    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?
      BEGIN doesn't need to be at the beginning of the script, but it will make it easier for you to to follow the execution if it is.

      If you want to keep a variable to track whether the module is loaded you should use our, as you should always use strict (and warnings).

      use strict; use warnings; our $lwpavailable = 0; BEGIN { eval (" use LWP::UserAgent; use HTTP::Request::Common; "); if ($@) { $lwpavailable = 0; } else { $lwpavailable = 1; } }
        well, I have a package that has a few separate programs, and I want the variables to be shared by all of them. I use my for local variables, but the variables that i want shared i just dont declare... i tried to use our on them, but it had unintended consequences that I couldn't figure out...
      Yup, that would work great. Just add my $lwpavailable; before BEGIN.
        is there a significant amount of processing that is requires to load these modules? if the script is accessed, say, several 1000 times per day, attempting to load the modules in the beginning might bog things down more than, say, if they were loaded in the middle of the script only when they are needed (which is infrequent)?