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

I have been asked to write a CGI script to do the following.

So, I started taking notes on CGI scripts. As all kids do, I did startup with a "Hello World" program.

Script is at /test012/home/PikK45/CGI/1.pl. Note that test012 is the server name. (Server is a AIX server)

Then, I wanted to view the output of my first script. But don't know how to do that :(

Replies are listed 'Best First'.
Re: run a CGI script
by davido (Cardinal) on Dec 27, 2013 at 16:20 UTC

    Hit the appropriate URL with your browser. Only the person who configured your webserver can tell you what the appropriate URL is.

    If you're wise enough to be using CGI, you should also be able to simply run your script and see some output:

    your_command_prompt$ perl /test012/home/PikK45/CGI/1.pl

    Whether or not your web server actually invokes the CGI script at the path where you put it is dependent on how the server is configured, and that's probably something you will have to ask the administrator about (or possibly read the instructions in your assignment again). This isn't a Perl question. It's a webserver question.


    Dave

      thats my bad.. I will have to do more work on that and get back :)

Re: run a CGI script
by berends (Novice) on Dec 28, 2013 at 11:09 UTC

    A typical CGI script outputs some HTTP header lines, a blank line and then your HTML document. You can usually run the simple scripts, redirecting the output to a temporary file. Edit the temporary file to remove the HTTP header lines, save the remainder and then view it in a web browser. It's inconvenient but barely usable in restricted environments.

    If you are forced to keep working that way you'll devise some scripting automation (sed, awk or perl) to trim off the HTTP header lines. With a bit more effort you could create your own web server in Perl with modules such as HTTP::Server::Simple to send the CGI output directly to the browser. Even better would be to swap CGI for PSGI as used for example Dancer, but that might be forbidden on your AIX computer.

Re: run a CGI script
by marto (Cardinal) on Dec 29, 2013 at 11:49 UTC
Re: run a CGI script
by flexvault (Monsignor) on Dec 28, 2013 at 16:17 UTC

    Hi PikK45,

    The nice thing about the CGI script is that STDIN and STDOUT are the same for Perl and the web-server. So without using your AIX web-server you can test any CGI script setting up a input file and then do the following(untested):

    cat inputfile | perl script.cgi > outputfile

    By adjusting the inputfile and the script.cgi you can see differences in the outputfile. Getting it to work on the AIX server is another thing, but it'll get you started.

    Good Luck...Ed

    "Well done is better than well said." - Benjamin Franklin

Re: run a CGI script
by Preceptor (Deacon) on Dec 29, 2013 at 11:33 UTC

    CGI scripts run in exactly the same way, so you can 'test' them by running them. The key difference is, that each web page _must_ start with a 'content type', and without that your script looks like an error

    #!/usr/bin/perl use strict; use warnings; print "Content-Type: Text/HTML\n\n"; print "<H1>Your CGI script is working</H1>\n";

    That's the very basics - you don't _have_ to use the 'CGI' module, but ... well, it's there to support CGI scripting, so it's really sensible.

    So to get this script to 'work':

    • Copy the script to a directory that you've configured in your web server. If you're using 'Apache' as your web server, you'll be wanting to look up 'ScriptAlias'.
    • Set it executable, and ensure it's readable by your web server user.
    • You can then 'run' it by pointing a web brower at that path - e.g. http://yourserver/cgi-bin/your_script
    • STDOUT will go through the web server, to the browser. STDERR will go into the error log on your web server. STDIN is present if someone uses as web form to POST data to your script.
    • You will also want to look into web forms - the way you 'ask' a script something, is (typically) that way. A basic form (static HTML) looks like
    • <HTML> <HEAD><TITLE>Web Form</TITLE></HEAD> <BODY> <FORM ACTION="http://yourserver/cgi-bin/your_script" METHOD=POST> Text Here: <INPUT TYPE="TEXT" NAME="my-text" /><BR/> <INPUT TYPE="SUBMIT" VALUE="Send" /> </FORM> </BODY>

      This form will send the contents of your text box to you script on 'STDIN' (because you used 'METHOD=POST'

    And there you have a very basic pair of scripts - but seriously, use CGI as it streamlines a lot of things. Key feature are parameter handling, 'on the fly' form generation, and sending errors to your browser (look at CGI::Carp)

    One thing I will say - be extremely careful to sanitise any inputs to your script. Web forms/CGI scripts are VERY good places to end up with security bugs, because they can be exploited remotely, and your CGI script runs as the web server. That means don't trust _anything_ you get from that form.

    XKCD says it quite well: http://xkcd.com/327/