Example of a CGI Script
In the picture below you will see an example of a simple CGI script written with Perl Language. Let us now explain what it’s all about in the CGI tutorial below.
1. Line 1 shows the path to the perl compiler or processor in your system. This varies depending on your web server’s configurations. You may need to ask your web hosting provider or administrator for the correct syntax if you are not sure. The usual alternative location on Unix servers is #/usr/local/bin/perl, while the usual location for a windows server is #!c:/perl/bin/perl.exe. You may need to put this line at the top of your CGI scripts on a windows machine depending on your system configuration. Then the – w is for warnings, which you should always have in your scripts to ensure your codes are free of bugs.
2. it’s an empty line that should always come after the call to the perl interpreter.
3.This enables your script to specify variables early and follow other appropriate programming conventions. Applying this equally reduces code error as well. However it is important to note that after every line with a perl command such as echo, you have to end it with a semicolon.
4. This line is also very important in a CGI Code as it helps in debugging the script. It will give you a better description of the error instead of the common “500 Internal Server Error".
5. This line inputs the CGI.pm module which includes pre-specified functions which are vital to CGI programming.
6. This is a function which outputs the HTTP Content-type header, a major requirement in all CGI programs. It’s good practice to have this line near the top of all of your CGI scripts, so that you will not forget to put it in.
7.This uses perl’s in built print functions to dynamically output a string of text in the browser screen for you. You can use either double or single quotes for this.
Finally with a little research there are a lot of places you can easily get free CGI tutorials online as well as free CGI scripts already pre-written for immediate use on your website. So go ahead and add interactivity to your websites as you wish.