Program Listings

Listings 15-1 to 15-12 contain the complete an uninterrupted code for the programs and templates used to create the Web-based shopping cart.

Listing 15-1: catalog.cgi

start example
01: #!/usr/bin/perl -wT 02: use strict; 03: use SOAP::Lite; 04: use CGI qw(:standard); 05: use lib qw(.); 06: use BasicSession; 07: our $sess; 08: my  %cart; 09: $sess = Get_Session(); 10: tie %cart, ‘BasicSession’; 11: my $add_to_cart = param(‘add_to_cart’); 12: ++$cart{$add_to_cart} if $add_to_cart; 13: my $remove= param(‘remove’); 14: delete $cart{$remove} if $remove; 15: my $products = SOAP::Lite 16:   -> uri(‘http://goliath.perlguy.net/Catalog’) 17:   -> proxy(‘http://goliath.perlguy.net/cgi-                 bin/soap_server19.cgi’) 18:   -> Get_Product_List() 19:   -> result; 20: Print_Page("./templates", "header.tmpl", \%cart); 21: Print_Page("./templates", "catalog_header.tmpl", \%cart); 22: for my $p (@$products){ 23:     print qq( 24:       <tr><td> 25:  [<a href="catalog.cgi?add_to_cart=$p->[1]">Add Item</a>] 26:       </td><td> 27:  <a href="item_details.cgi?item_num=$p->[1]">$p->[1]</a> 28:       </td><td>$p->[0] 29:       </td><td>&nbsp;$p->[2] 30:       </td><td align="center">&nbsp;$item{$p->[1]} 31:       </td><td>[<a              href="catalog.cgi?remove=$p->[1]">Delete</a>] 32:       </td></tr>\n); 33: } 34: Print_Page("./templates", "catalog_footer.tmpl", \%cart); 
end example

Listing 15-2: item_details.cgi

start example
01: #!/usr/bin/perl -wT 02: use strict; 03: use CGI qw(:standard); 04: use SOAP::Lite; 05: use XML::Simple; 06: use lib qw(.); 07: use BasicSession; 08: our $sess; 09: my  %session; 10: $sess = Get_Session(); 11: tie %session, ‘BasicSession’; 12: my $input = param(‘item_num’); 13: my $data = SOAP::Lite 14:   -> uri(‘http://goliath.perlguy.net/Catalog’) 15:   -> proxy(‘http://goliath.perlguy.net/cgi-                 bin/soap_server.cgi’) 16:   -> Get_Product_Data($input) 17:   -> result; 18: $data =~ s/\&amp;|\&/\&amp;/g; 19: my $item = XMLin($data); 20: ++$session{$item->{isbn}} if(param(‘add_to_cart’)); 21: my $cart_qty = $session{$item->{isbn}}; 22: $cart_qty    = 0 unless $cart_qty; 23: $item->{‘cart_qty’} = $cart_qty; 24: Print_Page("./templates", "header.tmpl"      , $item); 25: Print_Page("./templates", "item_details.tmpl", $item);
end example

Listing 15-3: cart.cgi

start example
01: #!/usr/bin/perl -wT 02: # cart.cgi 03: use strict; 04: use SOAP::Lite; 05: use CGI qw(:standard); 06: use lib qw(.); 07: use BasicSession; 08: our $sess; 09: my  %session; 10: $sess = Get_Session(); 11: tie %session, ‘BasicSession’; 12: my $add_to_cart = param(‘add_to_cart’); 13: ++$session{$add_to_cart} if $add_to_cart; 14: my $remove= param(‘remove’); 15: delete $session{$remove};  16: my $products = SOAP::Lite 17:   -> uri(‘http://goliath.perlguy.net/Catalog’) 18:   -> proxy(‘http://goliath.perlguy.net/cgi-                 bin/soap_server19.cgi’) 19:   -> Get_Product_List() 20:   -> result; 21: Print_Page("./templates", "header.tmpl", \%session); 22: Print_Page("./templates", "catalog_header.tmpl", \%session); 23: delete($session{‘cart_qty_total’});   24: delete($session{‘cart_price_total’}); 25: for my $prod (@$products) { 26:     next unless ($session{$prod->[1]}); 27:     $session{‘cart_qty_total’}   += $session{$prod->[1]}; 28:     $session{‘cart_price_total’} +=              Calc_Price($session{$prod->[1]} , $prod->[2]); 29:     print qq( 30:       <tr><td> 31:   [<a href="cart.cgi?add_to_cart=$prod->[1]">Add Item</a>] 32:       </td><td> 33:   <a href="item_details.cgi?item_num=$prod->[1]">$prod->[1]</a> 34:       </td><td> 35:         $prod->[0] 36:       </td><td align="right"> 37:         &nbsp;$prod->[2] 38:       </td><td align="center"> 39:         &nbsp;$session{$prod->[1]} 40:       </td><td> 41:         [<a href="cart.cgi?remove=$prod->[1]">Delete</a>] 42:       </td></tr>\n 43:     ); 44: } 45: $session{‘cart_price_total’} =          sprintf("%.2f", $session{‘cart_price_total’}); 46: Print_Page("./templates", "cart_total.tmpl"    , \%session); 47: Print_Page("./templates", "catalog_footer.tmpl", \%session); 48: sub Calc_Price { 49:     my($qty, $price) = @_; 50:     $price =~ s/[ \$]//g; 51:     return($qty * $price); 52: }
end example

Listing 15-4: invoice.cgi

start example
01: #!/usr/bin/perl -wT 02: use strict; 03: use SOAP::Lite; 04: use CGI qw(:standard); 05: use lib qw(.); 06: use BasicSession; 07: our $sess; 08: my %session; 09: $sess = Get_Session(); 10: tie %session, ‘BasicSession’; 11: my $products = SOAP::Lite 12:   -> uri(‘http://goliath.perlguy.net/Catalog’) 13:   -> proxy(‘http://goliath.perlguy.net/cgi-                 bin/soap_server19.cgi’) 14:   -> Get_Product_List() 15:   -> result; 16: Print_Page("./templates", "header.tmpl", \%session); 17: Print_Page("./templates", "invoice_header.tmpl", \%session); 18: delete($session{‘cart_qty_total’}); 19: delete($session{‘cart_price_total’}); 20: for my $prod (@$products){ 21:     next unless ($session{$prod->[1]}); 22:     $session{‘cart_qty_total’}   += $session{$prod->[1]}; 23:     $session{‘cart_price_total’} +=              Calc_Price($item{$prod->[1]} , $prod->[2]); 24:     print qq( 25:       <tr> 26:        <td align="center" valign="top"> 27:         $prod->[1] 28:        </td> 29:        <td align="left"> 30:         &nbsp;&nbsp;$prod->[0] 31:        </td> 32:        <td align="center" valign="top"> 33:         $session{$prod->[1]} 34:        </td> 35:        <td align="right" valign="top"> 36:         $prod->[2] 37:        </td> 38:        <td align="right" valign="top"> 39:     ); 40:   printf("\$%.2f", Calc_Price($session{$prod->[1]}, $prod->[2]) ); 41:     print qq( 42:        </td> 43:       </tr>\n 44:     ); 45: } 46: $session{‘cart_price_total’} =          sprintf("%.2f", $session{‘cart_price_total’}); 47: Print_Page("./templates", "invoice_footer.tmpl", \%session); 48: sub Calc_Price { 49:     my($qty, $price) = @_; 50:     $price =~ s/[ \$]//g; 51:     return($qty * $price); 52: }
end example

Listing 15-5: cart_total.tmpl

start example
01:    <tr> 02:     <td align="right" colspan="3"> 03:      Total in cart: 04:     </td> 05:     <td align="right"> 06:      <b>$%%cart_price_total%%</b> 07:     </td> 08:     <td align="center"> 09:      <b>%%cart_qty_total%%</b> 10:     </td> 11:     <td align="center"> 12:     &nbsp; 13:     </td> 14:    </tr>
end example

Listing 15-6: catalog_footer.tmpl

start example
01:    <tr>  02:     <td align="center" colspan="6"> 03:      [ <a href="catalog.cgi">View Catalog</a> ] 04:      [ <a href="cart.cgi">View Cart</a> ] 05:      [ <a href="invoice.cgi">View Invoice</a> ] 06:     </td> 07:    </tr>  08:   </table> 09:  </body> 10: </html> 
end example

Listing 15-7: catalog_header.tmpl

start example
01:  <body> 02:   <table border="1" cellspacing="0" align="center"> 03:    <tr> 04:     <td colspan="6" align="center"> 05:      <font > 06:       Book Catalog 07:      </font> 08:     </td> 09:    </tr> 10:    <tr> 11:     <td align="center"> 12:      <b>Purchase</b> 13:     </td> 14:     <td align="center"> 15:      <b>ISBN #</b> 16:     </td> 17:     <td align="center"> 18:      <b>Item</b> 19:     </td> 20:     <td align="center"> 21:      <b>Price</b> 22:     </td> 23:     <td align="center"> 24:      <b>Cart</b> 25:     </td> 26:     <td align="center"> 27:      <b>Remove</b> 28:     </td> 29:    </tr>
end example

Listing 15-8: header.tmpl

start example
01: <html><head><title>Product Catalog</title> 02: <style type="text/css"> 03: <!--  04: td   { background:  #e0e0e0;  05:        color:       #000000;  06:        font-family: Lucida, Verdana, Helvetica, Arial;  07:        font-size:   12pt} 08: a:link    { color: #4444ff } 09: a:visited { color: #333377 } 10: a:active  { color: #0000dd } 11: b  12: { font-family: Lucida, Verdana, Helvetica, Arial;  13:   font-size:   10pt;  14:   color:       %%text_color%% } 15: .small 16: { font-family: Lucida, Verdana, Helvetica, Arial;  17:   font-size:   10pt;  18:   color:       %%text_color%% } 19: .medium 20: { font-family: Lucida, Verdana, Helvetica, Arial;  21:   font-size:   12pt;  22:   color:       %%text_color%% } 23: .big_error 24: { font-family: Lucida, Verdana, Helvetica, Arial;  25:   font-size:   14pt;  26:   font-weight: bold; 27:   color:       #ff0000 } 28: .big  29: { font-family: Lucida, Verdana, Helvetica, Arial;  30:   font-size:   14pt;  31:   color:       %%text_color%% } 32: .large  33: { font-family: Lucida, Verdana, Helvetica, Arial;  34:   font-size:   20pt;  35:   color:       %%text_color%% } 36: .big2 37: { font-family: Lucida, Verdana, Helvetica, Arial;  38:   font-size:   24pt;  39:   color:       %%text_color%% } 40: --> 41: </style> 42: </head> 43: <body>
end example

Listing 15-9: invoice_body.tmpl

start example
01:  <tr> 02:   <td align="center" valign="top"> 03:    <b>Item #</b> 04:   </td> 05:   <td align="center"> 06:    <b>Item Description</b> 07:   </td> 08:   <td align="center" valign="top"> 09:    <b>Qty</b> 10:   </td> 11:   <td align="center" valign="top"> 12:    <b>Price</b> 13:   </td> 14:   <td align="center" valign="top"> 15:    <b>Total</b> 16:   </td> 17:  </tr>
end example

Listing 15-10: invoice_footer.tmpl

start example
01:    <tr>  02:     <td align="center" colspan="2"> 03:     </td> 04:     <td align="center"><b> 05:      %%cart_qty_total%%  06:     </b></td> 07:     <td align="right"> 08:      <b>Grand Total:</b> 09:     </td> 10:     <td align="right"><b> 11:      $%%cart_price_total%% 12:     </b></td> 13:    </tr>  14:   </table> 15:  </body> 16: </html>
end example

Listing 15-11: invoice_header.tmpl

start example
01: <table border="1" width="100%" align="center" cellspacing="0"> 02:  <tr> 03:   <td colspan="5" align="center"> 04:    <font >Order Invoice</font> 05:   </td> 06:  </tr> 07:  <tr> 08:   <td align="left" width="10%" valign="top"> 09:    <b>Date:</b> <font >04/29/2002</font> 10:   </td> 11:   <td align="center" width="80%" colspan="3"> 12:   <font >Perlguy, Inc.</font><br /> 13:   4 Brae Burn Court<br /> 14:   St. Charles, MO 63303<br /> 15:   (314) 555-1212<br /> 16:   brent@perlguy.net 17:   <br /><br /> 18:   </td> 19:   <td align="right" width="10%" valign="top"> 20:    <b>Invoice&nbsp;#:</b> <font >9328409</font> 21:   </td> 22:  </tr> 23:  <tr> 24:   <td align="center" valign="top"> 25:    <b>Item #</b> 26:   </td> 27:   <td align="center"> 28:    <b>Item Description</b> 29:   </td> 30:   <td align="center" valign="top"> 31:    <b>Qty</b> 32:   </td> 33:   <td align="center" valign="top"> 34:    <b>Price</b> 35:   </td> 36:   <td align="center" valign="top"> 37:    <b>Total</b> 38:   </td> 39:  </tr>
end example

Listing 15-12: item_details.tmpl

start example
01: <table border="1" width="100%" align="center" cellspacing="0"> 02:  <tr> 03:   <td colspan="5" align="center"> 04:    <font >Order Invoice</font> 05:   </td> 06:  </tr> 07:  <tr> 08:   <td align="left" width="10%" valign="top"> 09:    <b>Date:</b> <font >04/29/2002</font> 10:   </td> 11:   <td align="center" width="80%" colspan="3"> 12:   <font >Perlguy, Inc.</font><br /> 13:   4 Brae Burn Court<br /> 14:   St. Charles, MO 63303<br /> 15:   (314) 555-1212<br /> 16:   brent@perlguy.net 17:   <br /><br /> 18:   </td> 19:   <td align="right" width="10%" valign="top"> 20:    <b>Invoice&nbsp;#:</b> <font >9328409</font> 21:   </td> 22:  </tr> 23:  <tr> 24:   <td align="center" valign="top"> 25:    <b>Item #</b> 26:   </td> 27:   <td align="center"> 28:    <b>Item Description</b> 29:   </td> 30:   <td align="center" valign="top"> 31:    <b>Qty</b> 32:   </td> 33:   <td align="center" valign="top"> 34:    <b>Price</b> 35:   </td> 36:   <td align="center" valign="top"> 37:    <b>Total</b> 38:   </td> 39:  </tr>
end example



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

Similar book on Amazon

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