• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Matt Doyle | Elated Communications

Web and WordPress Development

  • About Me
  • Blog
    • Design & Multimedia
      • Photoshop
      • Paint Shop Pro
      • Video & Audio
    • Online Marketing
      • E-Commerce
      • Social Media
    • Running a Website
      • WordPress
      • Apache
      • UNIX and Linux
      • Using FTP
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • PHP
      • Perl and CGI Scripting
  • Portfolio
  • Contact Me
  • Hire Me
Home / Blog / Web Development / Perl and CGI Scripting / Introduction to Perl and the CGI

Introduction to Perl and the CGI

13 February 2003 / Leave a Comment

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.

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! 🙂

Filed Under: Perl and CGI Scripting Tagged With: basics, cgi-bin, code, intro, programming, server-side

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

To include a block of code in your comment, surround it with <pre> ... </pre> tags. You can include smaller code snippets inside some normal text by surrounding them with <code> ... </code> tags.

Allowed tags in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre> .

Primary Sidebar

Hire Matt!

Matt Doyle headshot

Need a little help with your website? I have over 20 years of web development experience under my belt. Let’s chat!

Matt Doyle - Codeable Expert Certificate

Hire Me Today

Call Me: +61 2 8006 0622

Stay in Touch!

Subscribe to get a quick email whenever I add new articles, free goodies, or special offers. I won’t spam you.

Subscribe

Recent Posts

  • Make a Rotatable 3D Product Boxshot with Three.js
  • Speed Up Your WordPress Website: 11 Simple Steps to a Faster Site
  • Reboot!
  • Wordfence Tutorial: How to Keep Your WordPress Site Safe from Hackers
  • How to Make Awesome-Looking Images for Your Website

Footer

Contact Matt

  • Email Me
  • Call Me: +61 2 8006 0622

Follow Matt

  • E-mail
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

Copyright © 1996-2023 Elated Communications. All rights reserved.
Affiliate Disclaimer | Privacy Policy | Terms of Use | Service T&C | Credits