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

Hi All,
I have written a perl cgi script which draws a image using the perl GD module. This scripts works fine when run form the command line. When I run this from browser, I get an internal server error. When I cheked the error logs, it tells me that it cannot locate GD module. Why is this happening. Does it need to set some thing to tell where the modules are located?

perl script simpleImage.cgi
#!/usr/local/bin/perl use GD; use CGI ':standard'; # This creates a Web GIF image on the fly # create a new image $im = new GD::Image(1000,100); # allocate some colors $white = $im ->colorAllocate(255,255,255); $black= $im ->colorAllocate(0,0,0); $red = $im ->colorAllocate(255,100,0); $blue= $im ->colorAllocate(0,150,255); $yellow= $im ->colorAllocate(255,200,0); $pink= $im ->colorAllocate(150,0,150); $im->filledRectangle(10,5,990,15,$pink); $im->arc(10,10,10,10,0,360,$black); $im->fill(10,10,$white); $im->arc(990,10,10,10,270,90,$black); $im->fillToBorder(990,10,$black,$pink); # Convert the image to GIF and print it on standard output #binmode STDOUT; print "Content-type: image/gif\n\n"; print $im->gif;
Address in the browser

http://localhost:8080/cgi-bin/simpleImage.cgi

Error recorded in the log files
[Fri Jul 29 00:25:57 2005] [error] [client 127.0.0.1] Premature end of + script headers: /home/nagesh /apache/cgi-bin/simpleImage.cgi Can't locate GD.pm in @INC (@INC contains: /usr/local/lib/perl5/5.8.6/ +i686-linux /usr/local/lib/per l5/5.8.6 /usr/local/lib/perl5/site_perl/5.8.6/i686-linux /usr/local/li +b/perl5/site_perl/5.8.6 /usr/ local/lib/perl5/site_perl .) at /home/nagesh/apache/cgi-bin/simpleImag +e.cgi line 3. BEGIN failed--compilation aborted at /home/nagesh/apache/cgi-bin/simpl +eImage.cgi line 3.
Any help will be greatly appreciated.
Thanks
Nagesh

fixed formatting by holli

Replies are listed 'Best First'.
Re: Trouble with Perl CGI script
by sparkyichi (Deacon) on Jul 28, 2005 at 14:45 UTC
    I would verify that your web server is using the same perl install that you are using from the command line.

    Another option would be to make a CGI script that will list the your @INC values and compair that to what you get from the command line.

    Sparky
    FMTEYEWTK
      Hi Sparky, Good point. I have actually two version of perl. One in usr/bin and the other is usr/local/bin which is the latest version. When running from the commandline, it uses the latter as the path is set that way. How can I tell the same when running it from the browser. Also how do I get the @INC values both from the brwser and the command line Thanks a lot Nagesh
        To get the values in @INC, simply print @INC;. To find out which perl executable is running your script, print $^X;. My guess is that you will have to make configuration changes to the webserver to tell it which version of perl to use, although you would think it would look at the #! line.
Re: Trouble with Perl CGI script
by Iron (Scribe) on Jul 28, 2005 at 14:35 UTC
    It seems that your script can't find GD module.
    Is it installed?
    Is it in @INC path?
    And why are you use'ing CGI if you actually doesn't use it?
      Hi Iron, Perl GD is definetely installed as when I run the script from the command line, it give me the output I want and does not give the error which I get when I run it from the browser. Any why I am using CGI, this is just a testing script for me to get familar with. I have a bigger plan for its use but wanted to get started with some thing simple. Thanks Nagesh
Re: Trouble with Perl CGI script
by Zucan (Beadle) on Jul 28, 2005 at 20:07 UTC
    I forget if the error I have seen shows up the way you describe it or not. I do not know how your GD module is compile, or if it is a shared library version of libgd... But maybe it is an LD_LIBRARY_PATH issue...

    Where is your libgd library installed? Is it in /usr/local/lib? You could try doing something like the following at the top of your perl script:

    my $LIB = defined $ENV{LD_LIBRARY_PATH} ? $ENV{LD_LIBRARY_PATH} : ""; $ENV{LD_LIBRARY_PATH} = "/usr/local/lib:$LIB";

    If that doesn't work, you can use an old trick to force LD_LIBRARY_PATH to get set:

    #!/bin/sh LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH export LD_LIBRARY_PATH exec /usr/local/bin/perl -x -S $0 ${1+"$@"} # -*-perl-*- #!perl -w use GD; ....

    Anyways, I feel like the "module not found" error is masking the real error that the module *can* be found, but can't be loaded because of other shared libraries needed for the module to work... I think Perl just displays the wrong error.

    The reason it works from the command line is because your shell environment is setup different than the Apache environment. When Apache starts, it is started usually from a startup script as root, which consequently has few environment variables set, meaning LD_LIBRARY_PATH is not likely defined.

    Hope this helps.
    Zucan