http://www.perlmonks.org?node_id=1017910


in reply to CGI question

By the way, perl allows you to test cgi scripts on the command line. Normally in order to run a cgi script, you need to start your server, then load an html page in your browser, then click a button, which is programmed to call your cgi script. But a cgi script essentially just reads some name/value pairs that the server provides, and then the cgi script executes some perl code and prints something. A cgi script has no idea that there is any such a thing as an html form or even that the internet has been invented. Because of that state of affairs, perl can run a cgi script directly, like a regular perl program, if you specify some name=value pairs like this:
$ perl animal.pl name1=value1 name2=value2

To figure out what name=value pairs your cgi script is expecting, you just need to examine the code. Your cgi script has this in it:

$animal = param('animal');

That line retrieves the value for the name "animal"(which corresponds to the name of an html form element). That means you can run your perl script on the command line if you provide the name animal along with any value that you would have typed into the html form element, e.g.:

$ perl animal.pl animal=tiger

And if you hit return after that, you will get this output:

Content-type: text/html Show me the tiger

In the normal course of things, that text would be sent to a browser, which knows how to interpret the headers (the lines at the beginning of the output that come before two consecutive newlines), and the browser knows how to display the text following the two newlines. In any case, you can quickly find out if the problem you are having is with the cgi script by running it from the command line. Also, running a perl script from the command line is a good way to see exactly what your cgi script is sending to the browser, which may come in handy as your cgi scripts get more complex.

Replies are listed 'Best First'.
Re^2: CGI question
by Anonymous Monk on Feb 09, 2013 at 05:38 UTC

    Excellent often wondered how to do that. Thanks for the tips