📄 ch19_15.htm
字号:
<HTML><HEAD><TITLE>Recipe 19.14. Program: chemiserie (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:45:48Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch19_01.htm"TITLE="19. CGI Programming"><LINKREL="prev"HREF="ch19_14.htm"TITLE="19.13. Saving a Form to a File or Mail Pipe"><LINKREL="next"HREF="ch20_01.htm"TITLE="20. Web Automation"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_14.htm"TITLE="19.13. Saving a Form to a File or Mail Pipe"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.13. Saving a Form to a File or Mail Pipe"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch19_01.htm"TITLE="19. CGI Programming"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="chapter"HREF="ch20_01.htm"TITLE="20. Web Automation"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 20. Web Automation"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch19-17893">19.14. Program: chemiserie</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch19-idx-1000005533-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005533-1"></A>The CGI script in <ACLASS="xref"HREF="ch19_15.htm#ch19-42370"TITLE="chemiserie">Example 19.9</A> lets people order t-shirts and sweaters over the Web, using techniques described in <ACLASS="xref"HREF="ch19_13.htm"TITLE="Writing a Multiscreen CGI Script">Recipe 19.12</A>. Its output isn't elegant or beautiful, but illustrating the multiscreen technique in a short program was challenging enough without trying to make it pretty as well.</P><PCLASS="para">The <CODECLASS="literal">shirt</CODE> and <CODECLASS="literal">sweater</CODE> subroutines check their widget values. If the user somehow submits an invalid color or size, the value is reset to the first in the list of allowable colors or sizes.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch19-42370">Example 19.9: chemiserie</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# chemiserie - simple CGI shopping for shirts and sweatersuse strict;use CGI qw(:standard);use CGI::Carp qw(fatalsToBrowser);my %States; # state table mapping pages to functionsmy $Current_Screen; # the current screen# Hash of pages and functions.%States = ( 'Default' => \&front_page, 'Shirt' => \&shirt, 'Sweater' => \&sweater, 'Checkout' => \&checkout, 'Card' => \&credit_card, 'Order' => \&order, 'Cancel' => \&front_page,);$Current_Screen = param(".State") || "Default";die "No screen for $Current_Screen" unless $States{$Current_Screen};# Generate the current page.standard_header();while (my($screen_name, $function) = each %States) { $function->($screen_name eq $Current_Screen);}standard_footer();exit;################################# header, footer, menu functions################################sub standard_header { print header(), start_html(-Title => "Shirts", -BGCOLOR=>"White"); print start_form(); # start_multipart_form() if file upload}sub standard_footer { print end_form(), end_html() }sub shop_menu { print p(defaults("Empty My Shopping Cart"), to_page("Shirt"), to_page("Sweater"), to_page("Checkout"));}############################## subroutines for each screen############################## The default page.sub front_page { my $active = shift; return unless $active; print "<H1>Hi!</H1>\n"; print "Welcome to our Shirt Shop! Please make your selection from "; print "the menu below.\n"; shop_menu();}# Page to order a shirt from.sub shirt { my $active = shift; my @sizes = qw(XL L M S); my @colors = qw(Black White); my ($size, $color, $count) = (param("shirt_size"), param("shirt_color"), param("shirt_count")); # sanity check if ($count) { $color = $colors[0] unless grep { $_ eq $color } @colors; $size = $sizes[0] unless grep { $_ eq $size } @sizes; param("shirt_color", $color); param("shirt_size", $size); } unless ($active) { print hidden("shirt_size") if $size; print hidden("shirt_color") if $color; print hidden("shirt_count") if $count; return; } print h1("T-Shirt"); print p("What a shirt! This baby is decked out with all the options.", "It comes with full luxury interior, cotton trim, and a collar", "to make your eyes water! Unit price: \$33.00"); print h2("Options"); print p("How Many?", textfield("shirt_count")); print p("Size?", popup_menu("shirt_size", \@sizes ), "Color?", popup_menu("shirt_color", \@colors)); shop_menu();}# Page to order a sweater from.sub sweater { my $active = shift; my @sizes = qw(XL L M); my @colors = qw(Chartreuse Puce Lavender); my ($size, $color, $count) = (param("sweater_size"), param("sweater_color"), param("sweater_count")); # sanity check if ($count) { $color = $colors[0] unless grep { $_ eq $color } @colors; $size = $sizes[0] unless grep { $_ eq $size } @sizes; param("sweater_color", $color); param("sweater_size", $size); } unless ($active) { print hidden("sweater_size") if $size; print hidden("sweater_color") if $color; print hidden("sweater_count") if $count; return; } print h1("Sweater"); print p("Nothing implies preppy elegance more than this fine", "sweater. Made by peasant workers from black market silk,", "it slides onto your lean form and cries out ``Take me,", "for I am a god!''. Unit price: \$49.99."); print h2("Options"); print p("How Many?", textfield("sweater_count")); print p("Size?", popup_menu("sweater_size", \@sizes)); print p("Color?", popup_menu("sweater_color", \@colors)); shop_menu();}# Page to display current order for confirmation.sub checkout { my $active = shift; return unless $active; print h1("Order Confirmation"); print p("You ordered the following:"); print order_text(); print p("Is this right? Select 'Card' to pay for the items", "or 'Shirt' or 'Sweater' to continue shopping."); print p(to_page("Card"), to_page("Shirt"), to_page("Sweater"));}# Page to gather credit-card information.sub credit_card { my $active = shift; my @widgets = qw(Name Address1 Address2 City Zip State Phone Card Expiry); unless ($active) { print map { hidden($_) } @widgets; return; } print pre(p("Name: ", textfield("Name")), p("Address: ", textfield("Address1")), p(" ", textfield("Address2")), p("City: ", textfield("City")), p("Zip: ", textfield("Zip")), p("State: ", textfield("State")), p("Phone: ", textfield("Phone")), p("Credit Card #: ", textfield("Card")), p("Expiry: ", textfield("Expiry"))); print p("Click on 'Order' to order the items. Click on 'Cancel' to return shopping."); print p(to_page("Order"), to_page("Cancel"));}# Page to complete an order.sub order { my $active = shift; unless ($active) { return; } # you'd check credit card values here print h1("Ordered!"); print p("You have ordered the following toppings:"); print order_text(); print p(defaults("Begin Again"));}# Returns HTML for the current order ("You have ordered ...")sub order_text { my $html = ''; if (param("shirt_count")) { $html .= p("You have ordered ", param("shirt_count"), " shirts of size ", param("shirt_size"), " and color ", param("shirt_color"), "."); } if (param("sweater_count")) { $html .= p("You have ordered ", param("sweater_count"), " sweaters of size ", param("sweater_size"), " and color ", param("sweater_color"), "."); } $html = p("Nothing!") unless $html; $html .= p("For a total cost of ", calculate_price()); return $html;}sub calculate_price { my $shirts = param("shirt_count") || 0; my $sweaters = param("sweater_count") || 0; return sprintf("\$%.2f", $shirts*33 + $sweaters * 49.99);}<ACLASS="indexterm"NAME="ch19-idx-1000005538-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005538-1"></A>sub to_page { submit(-NAME => ".State", -VALUE => shift) }<ACLASS="indexterm"NAME="ch19-idx-1000005330-0"></A></PRE></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_14.htm"TITLE="19.13. Saving a Form to a File or Mail Pipe"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.13. Saving a Form to a File or Mail Pipe"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="chapter"HREF="ch20_01.htm"TITLE="20. Web Automation"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 20. Web Automation"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">19.13. Saving a Form to a File or Mail Pipe</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">20. Web Automation</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -