Introduction To CGI

 

System Requirements:

1)Most operating systems will work with Perl/CGI

2)Your web server must be configured for CGI.  You may need to talk to your systems administrator or web hosting provider about this.

3)You must have perl installed.  Most *nix (unix, freebsd, linux, etc.) systems come with perl pre-installed.  If not, you can get it from cpan.org.  If you’re running windows, get perl from activestate.com.

 

Writing your first script:

 

Create a new text file in a plain text editor such as Microsoft Wordpad.  I use Textpad, a shareware program. Name it first.cgi.  Make sure to save this file in plain text only.  Do not save it with any special formatting or it won’t work properly.

 

Now, type the following to begin your script.  DO NOT type in the line numbers.  They exist solely to be referenced further below!

 

 

Here is an explanation of what’s going on above:

 

1. This is the path to the perl interpreter on your system.  It varies depending on your web server computer’s configuration.  Ask your system administrator or web hosting provider if you’re not sure.  The most common alternate location on *nix systems is #!/usr/local/bin/perl .  The most common location on windows systems is #!c:/perl/bin/perl.exe .  Depending on your system configuration, you may not need to put this line at the top of your CGI scripts on windows machines.  The –wflag enables warnings, which you should always use so that your code is free of bugs.

2. empty line should always follow the call to the perl interpreter.

3. this requires your code to pre-define variables and use other good programming practices.  Using it helps reduce bugs as well.  An important note here is that after every line where you give perl a command such as print, you need to put a semicolon to end the command.

4. this line should always be in a CGI script, as it greatly increases debugging.  It will give you a descriptive error message instead of a “500 Internal Server Error”.

5. this line loads the CGI.pm module which includes pre-defined functions which are essential to cgi programming.

6. This is a function which prints the HTTP Content-type header, which is required in all cgi scripts.  It’s good to put this line near the top of all your cgi scripts, so you don’t forget to include it.

7. This line uses perl’s built-in print function to output a string of text to <STDOUT> <STDOUT> in the case of a cgi script just means ‘print it to the browser’, or you can think of it as ‘when you tell perl to print something, it dynamically generates html text in the browser screen for you’.  You can use either single or double quotes for this, which does make a difference.  See below in the section on scalars for more information on this difference.

 

Now, upload this file to your webpswitch.com/">http://ipswitch.com). 

 

 

Next, chmodthe script to 755.  In WS_FTP Lite, you do this by clicking on the uploaded file, and right clicking it.  Next, you click the ‘chmod’ button. 

 

 

There are 3 settings for user, group, and all.  Set user to read/write/execute, group to read/execute, and all to read/execute.

 

 

Now, go to your web browser such as internet explorer and type in http://yourdomainname.com/cgi-bin/first.cgi

 

You should see the text ‘Hello, World’ appear on the screen:

 

 

Functions in perl:

 

print in perl is called a function.  Perl has many, many built-in functions.  The typical syntax for calling a function is:

 

print(“my value to pass to function”);

 

or

 

my $string_length = length($some_string);

 

You can pass functions variables to work with.  In the above example, we passed the length function a string (see below for definition).  In this case, the function “returned” a value, which means the variable$string_length now contains the return value of the function length, meaning that $string length now contains a number denoting how many characters were in the string $some_string.  (see below for definitions of strings and variables).

 

Perl lets you do a neat trick by allowing you to separate a single print function call using commas.  So you can type:

 

 

This results in:

 

 


<-- Previous: CGI Tutorial Home | Next: Basic Perl Syntax Guide - Variables: scalars, arrays, hashes -->


Please rate this cgi tutorial on cgi-resources.com:

CGI-Resources Rating:


Copy the Shrug Emoji
Ecommerce Shopping Cart Software
ShopCMS Paypal Shopping Cart
ShopCMS Paypal Shopping Cart
Free CGI Scripts
CGI Tutorial
Software Engineering Consultant
Search Engine Optimization Tips
How To Choose Quality Web Hosting
Free Search Engine Ranking Software
HTTP Compression
Install CGI Scripts
Tell A Friend Script
LittleFTP Free FTP Client For Windows


Link To
This Page!


Copy the following code and paste it into your html file.
Ecommerce Shopping Cart Software | ShopCMS Paypal Shopping Cart | Software Engineering Consultant | Free Search Engine Ranking Software | HTTP Compression | Install CGI Scripts | Search Engine Optimization Tips | CGI Tutorial | CGI Scripts | How To Choose Quality Web Hosting | Tell A Friend Script