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/ |