7.5 CGI.pm Introduced

So far, this has been pretty thin gruel. To do more complicated things in an easier fashion, we introduce the Perl/CGI programmer's friend: CGI.pm.

CGI.pm is a Perl module written by Lincoln Stein. Our discussion of CGI.pm is just an introduction. Much more documentation is available. We recommend that you check out Lincoln Stein's Official Guide to Programming with CGI.pm [Stein 98]. You can also check out the CGI.pm documentation with perldoc CGI .

To use CGI.pm, tell Perl that you want to use it with the use pragma:

 use CGI; 

After Perl executes this statement, your program has access to all the methods that CGI.pm provides. The module can be further refined by specifying a more convenient programming style, called the standard style:

 use CGI :standard; 

Here's an example of some simple HTML that creates Figure 7.7:

Figure 7.7. A simple page with CGI.pm


 #!/usr/bin/perl  # simple.cgi  use CGI :standard;  print      header(),      start_html(-title   => A Simple Page,                 -bgcolor => #ffffff,                 -text    => #520063),      h1(Hello, world! again),      hr(),      Time to go now...,      end_html(); 

The fourth line indicates that we want to use the CGI.pm module, passing the string :standard into the use pragma. This ensures that we will use only functions that are appropriate for the widest range of browsers, up to HTML version 3. To use all the HTML there is, we could use CGI :all ; . These are the two most widely used options.

The use of :standard also enables us to call CGI.pm methods such as hr() directly without an object. If we did not use :standard mode, we would have to create a CGI object and execute the methods with it, as in this code:

 $q = new CGI;  print  $q->header(),  .  .  $q->hr(),  .  . 

As you can see, using :standard mode is much nicer ”it looks cleaner and we avoid the difficult-to-type $q-> . Also, using :standard ensures you of the widest possible audience because the HTML generated will support most major browsers.

The rest of the program is all part of the same print() function call. We could have printed each piece separately, but the convention is to combine the multiple print s into a single print() function.

These are the other methods used in this program.

  • header() generates the header "Content-type: text/html\n\n" ; CGI.pm adds the extra newline automatically.

  • start_html() ] generates the HTML for the start of the web page: <HTML><HEAD>... ; within this, note the use of the following:

    - title => A Simple Page sets the title within the title tags: <TITLE>A Simple Page</TITLE> .

    - bgcolor => #ffffff sets the background color within the body tags, used with -text , described next .

    - text => #520063 sets the color of the text within the body tags. Used with the previous -bgcolor , this produces <BODY BGCOLOR="#ffffff" TEXT="#520063"> .

  • h1() generates an <H1> tag, and the argument is placed within the tags; in this example, the HTML produced is <H1>Hello, world! again</H1> .

  • hr() generates <HR> .

  • end_html() generates </BODY></HTML> .

This program can be run from the command line to see the output (enter ctrl-D after the offline mode prompt):

 $  ./simple.cgi  (offline mode: enter name=value pairs on standard input)  ^D  Content-Type: text/html  <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">  <HTML><HEAD><TITLE>A Simple Page</TITLE>  </HEAD><BODY TEXT="#520063" BGCOLOR="#ffffff"><H1>Hello, world!                    again</H1>  <HR>Time to go now...</BODY></HTML> 

Open Source Development with Lamp
Open Source Development with LAMP: Using Linux, Apache, MySQL, Perl, and PHP
ISBN: 020177061X
EAN: 2147483647
Year: 2002
Pages: 136

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net