According to the HTTP protocol, an empty line must be sent after all
headers have been sent. This empty line indicates that the actual
response data will start at the next line.[5]
Example 1-2. "Hello user" script
#!/usr/bin/perl
use CGI qw(:standard);
my $username = param('username') || "unknown";
print "Content-type: text/plain\n\n";
print "Hello $username!\n";
Notice that this script is only slightly different from the previous
one. We've pulled in the CGI.pm
module, importing a group of functions called
:standard. We then used its param(
) function to retrieve the value of the
username key. This call will return the name
submitted by any of the three ways described above (a form using
either POST, GET, or a
hardcoded name with GET; the last two are
essentially the same). If no value was supplied in the request,
param( ) returns undef.
my $username = param('username') || "unknown";
The browser will display:
The passed parameters were:
a => foo
b => bar
c => foobar
Now generate this form:
<form action="/cgi-bin/hello_user.pl" method="GET">
<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="submit">
</form>
If we fill in only the firstname field with the
value Doug, the browser will display:
The passed parameters were:
firstname => Doug
lastname =>
If in addition the lastname field is
MacEachern, you will see:
The passed parameters were:
firstname => Doug
lastname => MacEachern
These are just a few of the many functions CGI.pm
offers. Read its manpage for detailed information by typing
perldoc CGI at your command prompt.
We used this long CGI.pm example to demonstrate
how simple basic CGI is. You shouldn't reinvent the
wheel; use standard tools when writing your own scripts, and you will
save a lot of time. Just as with Perl, you can start creating really
cool and powerful code from the very beginning, gaining more advanced
knowledge over time. There is much more to know about the CGI
specification, and you will learn about some of its advanced features
in the course of your web development practice. We will cover the
most commonly used features in this book.
For now, let CGI.pm or an equivalent library
handle the intricacies of the CGI specification, and concentrate your
efforts on the core functionality of your code.