Listing All Items

Next, we cover the catalog program. This program creates a Web page that contains a listing of all of the products, along with their part numbers. The products for this example are a list of books, and the part numbers are actually the ISBNs of these books. In reality, the parts and part numbers can be for any type of product. I just happen to have a database of books already populated, so it's easy for me to create an example based upon already existing data.

The catalog page this program creates is not designed to look pretty; it is designed to be a simple example to work with. For a real-world application, you would most likely want to make the user interface look much nicer for your customers. Figure 12-1 shows an example of what this program produces.

click to expand
Figure 12-1: Output from catalog.cgi

01: #!/usr/bin/perl -wT 02: use strict; 03: use SOAP::Lite; 04: use CGI qw(:standard); 

Line 1 tells the system where to find Perl and turns on warnings and taint checking. Since this is a CGI program, turning taint checking on is a good idea for safety sake. By turning taint checking on, if I try to perform a system command with data read in by the program, this will warn me that it may be unsafe.

Line 2 loads the strict module. strict should be used for all programs so that you can avoid common errors and also so that you are forced to program carefully.

Line 3 loads the SOAP::Lite module. We use this module to access the functions on the SOAP server.

Line 4 loads the CGI module and its standard features.

05: my $rval = SOAP::Lite 06:   -> uri(‘') 07:   -> proxy(‘     bin/soap_server.cgi') 08:   -> Get_Product_List() 09:   -> result;

Line 5 declares a scalar variable named $rval and assigns it to the value that the call to the SOAP::Lite module returns. Since the Get_Products_List SOAP method returns a reference to an array of references, a reference will be stored in $rval.

Lines 5-9 are actually several methods from the SOAP::Lite module concatenated. These commands make up a SOAP client that retrieves data from the SOAP server.

Line 6 tells the SOAP client the URI, or namespace, of the service we are calling.

Line 7 is the address of the SOAP server we are contacting.

Line 8 is the remote function we are calling on the SOAP sever.

Line 9 calls the result method, which returns the results of the function we have called.

10: print header;

Line 10 prints the data returned by a call to the header function. The header function is part of the standard set of functions in the CGI module. This simply prints the HTTP header information.

11: print<<HTML; 12: <html> 13:  <head><title>Product Catalog</title></head> 14:  <body> 15:   <table border="1" cellspacing="0" align="center"> 16:    <tr> 17:     <td colspan="2" align="center"> 18:      <font size="6"> 19:       Product Catalog 20:      </font> 21:     </td> 22:    </tr> 23:    <tr> 24:     <td align="center"> 25:      <b>Part #</b> 26:     </td> 27:     <td align="center"> 28:      <b>Item</b> 29:     </td> 30:    </tr> 31: HTML

Lines 11-31 are a here document that prints the beginning HTML needed to generate the top of the page that displays all of the items.

32: for (@$rval){ 33:     print qq(   <tr><td>); 34:     print qq(<a href="/cgi-bin/item_details.cgi?item_num=           $_->[1]">$_->[1]</a>); 35:     print qq(    </td><td>$_->[0]); 36:     print qq(   </td></tr>\n); 37: }

Line 32 begins a for loop that iterates through each of the items in the array @$rval.

Line 33 prints a table row and table-column tag.

Line 34 prints a link to the item_details.cgi program as well as the item number (ISBN).

Line 35 prints a closing table-column tag and begins a new column containing the item name.

Line 36 prints a closing table column and closing table row tag.

Line 37 ends the for loop that begins on line 32.

38: print<<HTML; 39:   </table> 40:  </body> 41: </html> 42: HTML

Lines 38-42 are a here document that prints the remaining HTML needed for the product display page.

We now have our SOAP server and the program that calls one of the SOAP functions. Next we'll take a look at the program that displays the detailed information about an item that has been clicked.

Perl Database Programming
Perl Database Programming
ISBN: 0764549561
EAN: 2147483647
Year: 2001
Pages: 175

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: