CGI using Perl is straightforward. If you can write a Perl program to print "hello, world!" you're halfway there. So, to be consistent with previous first examples (and because we haven't chanted the mantra in a while), let's start with "hello, world!". Change to the proper directory that contains the server's CGI programs: $ cd /var/www/cgi-bin Create the file hello.cgi , which contains the following text: [2]
#!/usr/bin/perl # hello.cgi print "Content-type: text/plain\n"; print "\n"; print "hello, world!"; The program must be executable by all users: [3]
$ chmod a+rx hello.cgi Point your browser to one of these URLs, depending on whether you are following the bouncing ball or not: http://localhost/cgi-bin/hello.cgi or www.opensourcewebbook.com/cgi-bin/hello.cgi. In either of these, you should end up seeing something very similar to Figure 7.2. Figure 7.2. hello, world!
The first line of this "hello, world!" CGI script is the usual shell directive telling the shell to run Perl. Because this is a Perl script, the second line is a comment, prefaced with # just as in regular Perl scripts. The rest of this CGI program is simply a series of print() function calls. Recall from the previous discussion that the CGI program must print the content type followed by a blank line, and the first two print() functions do that. The first prints information that will be passed to the browser in the header . It tells the browser that the following information is to be interpreted as text/plain , or, as you might say out loud (until you've been doing it for a while and become a nerd), plain text. The next statement, print "\n"; is the all-important, gotta -have-it, don't-forget-it-or-you-will-get-a-server-error print-the-newline character ” \n ”which is equivalent to printing a blank line. This blank line terminates the header so that the browser knows that the header is finished and anything following is to be displayed in the browser. The next line is of course where the action is, displaying your cheerful and by now almost mantralike greeting to any observer. In general, CGI programs aren't used to print plain text but to create HTML for the client browser. As an example, create a new program named hello2.cgi : #!/usr/bin/perl # hello2.cgi print "Content-type: text/plain\n"; print "\n"; print "<b>hello</b>, <i>world</i>!"; This HTML made "hello" bold and "world" italicized . Load this code into the browser via one of these URLs ” http://localhost/cgi-bin/hello2.cgi or www.opensourcewebbook.com/cgi-bin/hello2.cgi ”and you should see Figure 7.3. Figure 7.3. hello, world! take 2
Oops. Something's wrong with this picture! Oh, it's because we set the MIME [4] type to text/plain . It should be text/html . This change is made in hello3.cgi :
#!/usr/bin/perl # hello3.cgi print "Content-type: text/html\n"; print "\n"; print "<b>hello</b>, <i>world</i>!"; Go to one of these URLs ” http://localhost/cgi-bin/hello3.cgi or www.opensourcewebbook.com/cgi-bin/hello3.cgi ”and you should see what's shown in Figure 7.4. Figure 7.4. hello, world! take 3
Usually, we want CGI programs to do more than simply print "hello, world". [5] So let's see an example that displays the date, time, and hostname on a web page:
#!/usr/bin/perl # info.cgi use strict; my($host, $date); # get the info from the system chomp($host = `/bin/hostname`); chomp($date = `/bin/date`); # Pass info about the following text in the header print "Content-type: text/html\n"; print "\n"; # Print the information in HTML on the web page print "<html>\n"; print "<head>\n"; print "<title>System Information</title>\n"; print "</head>\n"; print "<body bgcolor=\"#520063\" text=\"#ffffff\">\n"; print "<h1>Hello from $host!</h1>\n"; print "The current time is now: $date\n"; print "</body>\n"; print "</html>\n"; If you point your browser to either http://localhost/cgi-bin/info.cgi or www.opensourcewebbook.com/cgi-bin/info.cgi, you will see something very similar to Figure 7.5. The hostname and time will likely be different. Figure 7.5. System information
The first thing that happens in this script is the execution of two system commands, /bin/hostname and /bin/date , the results of which are placed into appropriately named variables . Note that the commands are enclosed within backticks that execute the command and capture the standard output, and are chomp ed to get rid of the returned newline. As discussed before, the content type is sent with the print() function, and the header is ended by the newline , which generates a blank line. Last, the HTML to generate the web page is generated via print() functions, including the hostname and date stored in $host and $date , respectively. There is one big problem with this program: It's hard to read with all those print statements ”and this with only a few lines of text. Imagine if your program printed a whole bunch of HTML. Having that many print() statements would make it difficult to read, not to mention debug. There is, of course, another way (TMTOWTDI, remember?) ”instead of multiline prints, use a here document: #!/usr/bin/perl # info2.cgi use strict; my($host, $date); chomp($host = `/bin/hostname`); chomp($date = `/bin/date`); print <<EOHTML; Content-type: text/html <html> <head> <title>System Information</title> </head> <body bgcolor="#520063" text="#ffffff"> <h1>Hello from $host!</h1> The current time is now: $date </body> </html> EOHTML That generates the same web page as shown in Figure 7.5. In the second program, the here document begins with the syntax <<EOHTML . You don't have to use the particular string EOHTML ”the text after " << " can be virtually any string of characters, including no characters . EOHTML stands for the End Of HTML. We could have used EOS ”End Of Stuff, for instance. The here document, and hence the text that is to be printed, continues until the marker, EOHTML , is on a line by itself ”that means it starts in the first column, and nothing else follows it. This last bit is important. It can't be in the second column, and nothing can follow it, not even whitespace. Another advantage of the here format, besides the text being more readable, is that the double quotes no longer need be escaped, as shown in the <body> tag. |