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

Hello, I have just started trying to write cgi-scripts and although I can run my perl programs from the command line, when I try to load a webpage, all perl is visible on browser, including the initial lines
#! /usr/bin/perl print "Content-type: text/html\n\n"
Thanks

Edit: Added <code> tags. larsen

Replies are listed 'Best First'.
Re: browser not executing perl
by Beatnik (Parson) on Jun 19, 2002 at 22:23 UTC
    You need
    1. A webserver to put the scripts on (check Apache)
    2. CGI configurred on that webserver
    If you already have a webserver, then step 2 is regulary the problem...
    Small note: Your browser doesnt execute the scripts, the server does :)

    Greetz
    Beatnik
    Perl is like sex: if you're doing it wrong, there's no fun to it.
      Adding to Beatnik's list, check to make sure that the file is chmod'ed correctly on a linux system. I don't know if this could have any effect but maybe the path to Perl is wrong? Let us know what was wrong.

      -Stormgard
        I've never gotten the source for an incorrectly chmod'd file, just a 403 Forbidden (though that's not to say a very misconfigured server wouldn't dump the source).

        Makeshifts last the longest.

      That note should be more than a small one. :)

      Makeshifts last the longest.

Re: browser not executing perl
by emcs (Scribe) on Jun 19, 2002 at 23:39 UTC
    If you are using a Windows machine this link may help.

    Indigo perl is a free Perl distribution, that comes packaged with "Apache" web server; it is very easy to set up and install.

    It is available at:

    indigoperl.htm

    emcs

    The dogs bark; but the caravan rolls on.

Re: browser not executing perl
by stormgard (Novice) on Jun 20, 2002 at 00:30 UTC
    I just remembered. It has to do with what you have setup for the web server. Assuming it is on apache, tell the admin to add this line (it should already be there, just needs to add the .pl or .cgi extension (maybe even uncomment it)):

    AddHandler cgi-script .cgi .pl

    As long as the server is setup to allow execution of Perl CGI scripts, it will pickup .cgi and .pl extensions as a cgi script.

    -Stormgard
Re: browser not executing perl
by Aristotle (Chancellor) on Jun 19, 2002 at 23:46 UTC
    You need a webserver, as Beatnik already noted. Apache can be daunting to set up at first (update: or maybe the package emcs++ mentions makes it not so), so you might want to start out with Ritlab's TinyWeb server. If you do, I would advise you to upgrade to Apache sometime anyway, because TinyWeb's CGI support is rather limited in some ways. But it's enough to make first steps with, and it's quick to set up so you can go about learning Perl for the time being and forget about the more intricate details of running a webserver.

    Makeshifts last the longest.

      Or just roll your own webserver with HTTP::Daemon. It's suprisingly easy for a one-shot script. Multiple functions cause minor headaches.

      Update: I dispute that Apache is hard to setup on Linux. Many distributions will install it unless restrained at install time. To use scripts with it, you copy your script into your cgi-bin directory, name it script.cgi, chmod a=rx script.cgi and you are off and running. Even microsoft couldn't make it any easier.

      ____________________
      Jeremy
      I didn't believe in evil until I dated it.

        While HTTP::Daemon may be easy to use, it is not advisable practice. For one, if you need to write a script to run on YourAverageHost.Com, it will invariably be a CGI script. Even when that is untrue and the script in question is going to run on your own machine, running a HTTP daemon for every single script you may or may not need at one time or other is simply an unnecessary waste of resources (memory, CPU, socket handles) and presents a box with a million open ports to anyone who happens to scan you. And without meaning to be condescending to her, if danielle is asking such a question I doubt she has the experience to have a grasp of all of the security concerns that play a role in writing a daemon (although admittedly CGI is only marginally friendlier an environment).

        In specific scenarios, HTTP::Daemon may be the tool to use. For the common tasks, it's not. Use the right tool for the right job..

        Makeshifts last the longest.

Re: browser not executing perl
by George_Sherston (Vicar) on Jun 20, 2002 at 09:31 UTC
    If you're just starting out you may find some of the above excellent advice about setting up Apache on your own computer a bit daunting. One way to get your feet wet with CGI is to run the scripts on your webhost's server. Presumably you have or will have an ISP who can let you run your CGI scripts. So when you are sure the scripts run (tested at the command line), just upload them into the cgi-enabled directory on your web space (your ISP will tell you what you have to do to make this work). There are many ISPs that allow this, and offer it free or for very little. For example, cubesoft is American and good, uklinux is British and ditto.

    § George Sherston