Introduction to Perl and the CGI

What kind of language is Perl, and what is the CGI? How do you write a "CGI script"? These questions, and more, are answered in this introduction!

You've probably heard of CGI scripts. They're what you often see after you've filled in a Web form (e.g. "Thank you for contacting us!"), or when you use a guestbook or a bulletin board such as our Webmaster Forums.

Often you can spot a CGI script in the address bar of your browser, because its name will end in .cgi or .pl, and it will often be in a directory called cgi-bin. For example:

http://www.mysite.com/cgi-bin/formmail.cgi

What is a CGI script?

A CGI script is a small program, or script, that runs on the Web server. It usually produces some sort of dynamically-generated Web page, such as a guestbook page or some information pulled from a database.

CGI stands for Common Gateway Interface. It's simply a mechanism to allow Web servers to run external programs or scripts when requested by a Web browser.

You can write CGI programs or scripts in any programming language you like, as long as the end result is something the server can run. However, CGI scripts are usually written in Perl.

What is Perl?

Perl is a powerful and flexible scripting language. Also, Perl is an interpreted language. This means that you can make changes to a Perl script and see the results immediately (unlike programming in C, where you have to recompile the program each time). This makes Perl highly suitable for CGI scripting.

What does a CGI script actually look like?

Here is a very simple CGI script written in Perl. Don't worry if you don't understand it at this point. We'll explain this script in more detail in a later tutorial!


#!/usr/bin/perl

print "Content-type: text/html\n\n";

print <<END_HTML;
<html>
<head></head>
<body>Hello, World!</body>
</html>
END_HTML

This CGI script simply displays a Web page containing the text "Hello, World!". Click here to see it in action!

How do I run a CGI script on my Web server?

A CGI script is a file, just like a Web page, or a GIF image. The main difference is that CGI scripts are actually run by the Web server, whereas images and Web pages are simply displayed.

Usually you will need to upload your CGI scripts to your Web server, just like you upload your Web pages and images (often using FTP). On most Web servers you will need to upload it to the cgi-bin folder on your site.

Once you've uploaded the script, you usually need to set its permissions. Don't worry - we'll explain all this in later CGI tutorials! We'll also teach you more about how Perl and the CGI work, and help you to write some CGI scripts of your own. Happy scripting! :)

Follow Elated

Related articles

Responses to this article

There are no responses yet.

Post a response

Want to add a comment, or ask a question about this article? Post a response.

To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.

Top of Page