Get Simon & Matt's Photoshop Book!

Photoshop CS3 Layers Bible book cover

Sharpen your Photoshop skills today!

PageKits.com Featured PageKits

Rush Hour PageKit
Rush Hour ($14.99)


Photoshoot PageKit
Photoshoot ($39.99)


See more! > >

 

Introduction to Perl and the CGI

Tutorial by Matt Doyle. Level: Beginner. Published on 13 February 2003 in Perl and CGI Scripting.

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! :)

Responses to this article

There are no repsonses 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 or two. Sign up now.

Keep in touch

Subscribe to our feed or follow us on Twitter for news of our latest articles and other fun stuff.

Also, don't forget the free ELATED Extra Newsletter, where you can get more great Web-building articles and tips sent straight to your inbox!

If you need help with a Web-building issue, check out our online Webmaster Forums, where you can get assistance from members of Elated and other webmasters.

Link to this page

Feel free to link to this article in your own Web pages. Click one of the boxes below to select the text, then copy and paste it into your page:



Top of Page

Free email newsletter

The popular ELATED Extra twice-a-month newsletter. Site updates, web tips, and more. Free bonus template when you sign up!

We won't give away your email address. Unsubscribe any time. Privacy
Feed icon Follow us on Twitter

Current forum topics:

Got a question about making a website? Ask it in the forums — we'd love to help you. All questions answered!