“Hello, world!” as a CGI Script

Before ending this brief introduction to Perl, I want to show you one more thing: the “Hello, world!” script rewritten to run as a CGI script.

I’ve assumed in this book that you already have a certain amount of web authoring experience, so you may have encountered CGI scripts before, and may even have run a few yourself. In case they’re completely new to you, though, here’s the lowdown: In simplest terms, a CGI script is a separate program that a web server runs in order to produce a customized page to show to a web user. CGI stands for Common Gateway Interface , which is just a description of a set of relatively simple rules for how the communication between the web server and the separate program will be conducted. CGI scripts can be written in any language that can produce the appropriate sort of output, but Perl is by far the most popular choice because of how easy it is to create CGI scripts in Perl.

CGI scripts are something of a gateway drug for Perl use. Many accidental programmers first come to Perl not because they’ve decided to learn Perl programming per se, but because they want to create a CGI script (probably to process the output of a web form), and someone has told them that Perl is the way to go. That’s okay; Perl doesn’t mind. It happily does the task at hand, biding its time until the user is ready for more.

Content-Type Headers

Let’s modify hello.plx so that it will run as a CGI script. Every CGI script needs to output a CGI header ...

Get Perl for Web Site Management now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.