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

Hi All,
I have been trying hard to make a very simple Perl CGI script generate an image when opened in a browser but without success. I had written a perl cgi script (simpleImage.cgi) which generates an image (using the GD module). I have saved it in my working directory (not in cgi-bin) and tried to open it in the browser. What is surprising me is that the browser shows the code rather than the image which it was suppose to generate. I have made the simpleImage.cgi as executable but that dint serve the problem. Can any one please help me how to get the image by running this script from the browser (I have wasted more than a days time trying to figure out myself). I am including the code below. Am I doing any thing wrong.

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;

Many thanks
Nagesh

20050727 Cleaned up by Corion: Added formatting

Replies are listed 'Best First'.
Re: Perl CGI problem. Help please!!!!
by davorg (Chancellor) on Jul 27, 2005 at 13:57 UTC
    I have saved it in my working directory (not in cgi-bin) and tried to open it in the browser. What is surprising me is that the browser shows the code rather than the image which it was suppose to generate.

    Your web server needs to be configured to know which URLs are CGI programs. CGI programs are usually identified by location (i.e. all files in /cgi-bin) or by name (i.e. all files with a .cgi extension). Sounds like your web server isn't configured to recognise .cgi files as CGI programs.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Perl CGI problem. Help please!!!!
by jbrugger (Parson) on Jul 27, 2005 at 13:57 UTC
    Try a simple script:
    #!/usr/local/bin/perl -w use strict; use CGI; my $cgi = new CGI; print $cgi->header(-type => 'text/html'); print "Please use <CODE>your code here</CODE> tags";
    chmod 755 the script. If it does the same, check your webserrvers configuration.

    update ps. your script works fine for me, it's probabluy your webservers configuration.

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
      Hi There, Thanks a lot for the prompt reply. I tired your script with your instruction but again I get the code on the browser. Do I have to do chmod 755 to the directories in which this script is in. Thanks Nagesh
Re: Perl CGI problem. Help please!!!!
by Fletch (Bishop) on Jul 27, 2005 at 14:00 UTC

    If you're getting the code instead of it running it means you have a webserver configuration problem, not a Perl problem. Bug your sysadmin and/or consult the documentation for whatever webserver. See also the somewhat dated but still useful The Idiot's Guide to Solving Perl CGI Problems.

    --
    We're looking for people in ATL

      Thank you davorg and Fletch I think what you pointed out about the webserver setting seems to be right. I have now moved the script to cgi-bin folder (file:///usr/local/apache/cgi-bin/simpleImage.cgi) and repeated chmod 755. It still does the same (showing the code). Does this mean that just putting the code in cgi-bin does not solve the purpose. The major issue is that we donot have a sys admin which means I should learn the hard way :( Thanks Nagesh

        Its not the directory that matters. If you're using Apache, it has to be told that the directory (cgi-bin) is allowed to execute code. It should look something like this in the httpd.conf file:

        <Directory /path/to/your/cgi-bin> Options ExecCGI Order allow,deny Allow from all </Directory>

        If you have IIS I'm not sure of the options

        Silly question.
        Did you use this url: file:///usr/local/apache/cgi-bin/simpleImage.cgi?
        I have now moved the script to cgi-bin folder (file:///usr/local/apache/cgi-bin/simpleImage.cgi) and repeated chmod 755. It still does the same (showing the code). Does this mean that just putting the code in cgi-bin does not solve the purpose.

        Sounds that way to me.

        Do you have _any_ CGI programs working on this web server? It looks like your server isn't configured to run CGI programs at all. If you told us which web server you were using, someone might be able to help you fix that.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg