Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by lokiloki (Beadle)
on Nov 27, 2006 at 22:49 UTC ( #586351=note: print w/replies, xml ) Need Help??


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

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?

Replies are listed 'Best First'.
Re^5: Why am I getting "premature end of script header"?
by imp (Priest) on Nov 27, 2006 at 22:54 UTC
    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...
        I also tried to use use vars qw($name) but that didnt work either...
Re^5: Why am I getting "premature end of script header"?
by ikegami (Patriarch) on Nov 27, 2006 at 22:50 UTC
    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)?

        I ran

        perl -le "use Time::HiRes qw( time ); $t1=time; eval 'use LWP::UserAge +nt; use HTTP::Request::Common;'; $t2=time; print $t2-$t1"
        a couple of times, and got
        0.0728998184204102 0.0660429000854492 0.0670039653778076 0.0661859512329102

        That means you'd save at most 70ms every time the script is run. I've waited longer than the time you'd save per day for a single PerlMonks page to load :) If you're that desperate for speed, you should switch to mod_perl, FastCGI, or similar

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2023-11-29 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?