Showing Item Details

This next program does a couple different things to show you how even a small program can do several different things and become part of a fairly sophisticated system.

The item_details.cgi program for this example is run on one server, connects to a SOAP server that fetches the product information from a totally different server, and returns the results as an XML document. The SOAP server also fetches a Web page from Amazon.com and parses the page so that the only thing we keep is the URL to an image of the product, or book.

In this small program, we connect to a SOAP service that connects to two other servers, and we pull all of this data into a single page-totally seamless to the user!

Figure 12-2 shows what a detailed product page looks like.

click to expand
Figure 12-2: Detailed product page

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

Line 1 tells the system where to find Perl and turns on warnings and taint checking.

Line 2 loads the strict module.

Line 3 loads the CGI module and its standard functions.

Line 4 loads the SOAP::Lite module.

Line 5 loads the XML::Simple module.

06: print header; 07: my $input = param(‘item_num');

Line 6 prints the data that a call to the header function returns. The header function is part of the CGI module.

Line 7 declares a new variable named $input and sets it to the value that a call to the param function returns. The param function in this case is looking for the value passed in the URL as an HTML GET.

08: my $data = SOAP::Lite 09:   -> uri(‘http://goliath.perlguy.net/Catalog') 10:   -> proxy(‘http://goliath.perlguy.net/cgi-     bin/soap_server.cgi') 11:   -> Get_Product_Data($input) 12:   -> result;

Line 8 declares a scalar variable named $data and assigns it to the value that the call to the SOAP::Lite module returns. The value that Get_Product_Data returns is a string that makes up an XML document.

Lines 8-12 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 9 tells the SOAP client the URI, or namespace, of the service we are calling.

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

Line 11 is the remote function we are calling on the SOAP server; we pass the value in $input to the Get_Product_Data function.

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

13: $data =~ s/\&|\&/\&/g; 14: my $item = XMLin($data);

Line 13 replaces any ampersands HTML markups (&) or lone ampersands (&) in the XML document that was returned, with their equivalent HTML markup (&). A plain ampersand in an XML document causes an error when the document is parsed. We included & in our search to ensure that we don't change an already marked-up ampersands from & to &.

Line 14 declares a scalar variable named $item. This new scalar variable is set to the value that a call to the XMLin function returns. XMLin is part of the XML::Simple module, and it returns a reference to a hash containing the parsed XML document.

15: print<<HTML; 16: <html> 17:  <head><title>Product $item->{title}</title></head> 18:  <body> 19:   <table border="1" align="center"> 20:    <tr bgcolor="#c0c0c0"> 21:     <td colspan="3" align="center"> 22:      <font size="5">Item Details</font> 23:     </td> 24:    </tr> 25:    <tr> 26:     <td rowspan="6" bgcolor="#e0e0e0"><img           src="/books/2/889/1/html/2/$item->{image}"></td> 27:     <td bgcolor="#e0e0e0"><b>Item Name:</b></td> 28:     <td>$item->{title}</td> 29:    </tr> 30:    <tr> 31:     <td bgcolor="#e0e0e0"><b>Publisher:</b></td> 32:     <td>$item->{publisher}</td> 33:    </tr> 34:    <tr> 35:     <td bgcolor="#e0e0e0"><b>Format:</b></td> 36:     <td>$item->{format}</td> 37:    </tr> 38:    <tr> 39:     <td bgcolor="#e0e0e0"><b>Author:</b></td> 40:     <td>$item->{author}</td> 41:    </tr> 42:    <tr> 43:     <td bgcolor="#e0e0e0"><b>Publish Date:</b></td> 44:     <td>$item->{pubdate}</td> 45:    </tr> 46:    <tr> 47:     <td bgcolor="#e0e0e0"><b>Price:</b></td> 48:     <td>$item->{price}</td> 49:    </tr> 50:    <tr bgcolor="#e0e0e0"> 51:     <td colspan="3" align="center"> 52:      <a href="/cgi-bin/catalog.cgi">Back to Product                 Listing</a> 53:     </td> 54:    </tr> 55:   </table> 56:  </body> 57: </html> 58: HTML

Lines 15-58 are a here document that prints the HTML that generates the detailed item-listing page.

Each item from the XML document is simply referenced as a hash element in the hash that the $item variable points to.

In just 14 lines of code (the code before the here document), we connect to a SOAP server, connect to a remote database, and also connect to the servers at Amazon.com, pulling all of this data back and forming a nice Web page from it!

I find it just amazing that so much is going on and that we are able to pull it all together with such small programs. Because of this power and simplicity, Web services should really take off. You will no longer have to write all of the code for each application yourself-instead, you will just need to know where the Web services are that you need to connect to and put the pieces together to make your new, larger, application work.



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

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