📄 fig16_11.pl
字号:
#!/usr/bin/perl
# Fig. 16.11: fig16_11.pl
# Add or remove a book from cart and print cart contents
use warnings;
use strict;
use CGI qw( :standard );
my @cart = readCookie();
my $remove = param( "remove" );
my @book;
if ( $remove ) {
my $number = param( "number" );
# The book is removed from the cart array
@book = splice( @cart, 4 * ( $number - 1 ), 4 );
# The new array is written to the cookie
writeCookie( @cart );
print( header() );
print( start_html( "Book removed" ) );
print <<" End_Remove";
<center><p>The book <i>$book[0]</i> has been removed.</p>
<a href = "fig16_11.pl">Return to cart</a>
<br>
<a href = "fig16_13.pl">Sign Out</a>
End_Remove
}
else {
@book = param( "newbook" );
# Add the book the user wants to the cart array
push( @cart, @book );
# If there is nothing in the cart,
# simply show the items for sale
if ( !@cart ) {
print( redirect( "fig16_12.pl" ) );
exit;
}
# Change cookie so it has the new entry
writeCookie( @cart );
print( header() );
print( start_html( "Shopping Cart" ) );
print <<" End_Add";
<center><p>Here is your current order.</p>
<table border = "1" cellpadding = "7">
<tr>
<th>Item</th>
<th>Name</th>
<th>Year</th>
<th>ISBN</th>
<th>Price</th>
<th></th>
</tr>
End_Add
my $counter = 1;
my $total = 0;
my @cartCopy = @cart;
# print out the cart for the user
while ( @book = splice( @cartCopy, 0, 4 ) ) {
print( "<tr><form method = \"post\"" );
print( "action = \"fig16_11.pl\">" );
print( "<td>$counter</td>" );
print( "<td>$book[ 0 ]</td>" );
print( "<td>$book[ 1 ]</td>" );
print( "<td>$book[ 2 ]</td>" );
print( "<td>$book[ 3 ]</td>" );
print( "<td>", submit( "Remove" ), "</td>" );
param( "remove", 1 ); # set "remove" variable to true
param( "number", $counter ); # book number to remove
print( hidden( "remove" ) );
print( hidden( "number" ) );
print( "</form></tr>" );
$book[ 3 ] =~ s/\$//; # remove $ sign
$total += $book[ 3 ]; # add price
$counter++;
}
print( "<tr><th colspan = \"4\">Total Order</th><th>" );
printf( "\$%0.2f", $total ); # print the total
print( "</tr>" );
print( "</table><br>" );
print( "<a href = \"fig16_12.pl\">Buy more books</a>" );
print( br() );
print( "<a href = \"fig16_13.pl\">Sign out</a>" );
}
print( end_html() );
sub writeCookie
{
my $expires = "Monday, 11-JUN-01 16:00:00 GMT";
print( "Set-Cookie: " );
print( "CART=", join( "\t", @_ ), "; expires=$expires\n" );
return;
}
# Read the user's cookies
# Return the information from the CART cookie
sub readCookie
{
my @cookieValues = split( "; ", $ENV{ 'HTTP_COOKIE' } );
my $name;
my $value;
my @data;
foreach ( @cookieValues ) {
( $name, $value ) = split ( "=" );
if ( $name eq "CART" ) {
@data = split ( "\t", $value );
last;
}
}
return @data;
}
###########################################################################
# (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall. #
# All Rights Reserved. #
# #
# DISCLAIMER: The authors and publisher of this book have used their #
# best efforts in preparing the book. These efforts include the #
# development, research, and testing of the theories and programs #
# to determine their effectiveness. The authors and publisher make #
# no warranty of any kind, expressed or implied, with regard to these #
# programs or to the documentation contained in these books. The authors #
# and publisher shall not be liable in any event for incidental or #
# consequential damages in connection with, or arising out of, the #
# furnishing, performance, or use of these programs. #
###########################################################################
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -