⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch19_15.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 19.14. Program: chemiserie (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; 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'     =&gt; \&amp;front_page,    'Shirt'       =&gt; \&amp;shirt,    'Sweater'     =&gt; \&amp;sweater,    'Checkout'    =&gt; \&amp;checkout,    'Card'        =&gt; \&amp;credit_card,    'Order'       =&gt; \&amp;order,    'Cancel'      =&gt; \&amp;front_page,);$Current_Screen = param(&quot;.State&quot;) || &quot;Default&quot;;die &quot;No screen for $Current_Screen&quot; unless $States{$Current_Screen};# Generate the current page.standard_header();while (my($screen_name, $function) = each %States) {    $function-&gt;($screen_name eq $Current_Screen);}standard_footer();exit;################################# header, footer, menu functions################################sub standard_header {    print header(), start_html(-Title =&gt; &quot;Shirts&quot;, -BGCOLOR=&gt;&quot;White&quot;);    print start_form(); # start_multipart_form() if file upload}sub standard_footer { print end_form(), end_html() }sub shop_menu {    print p(defaults(&quot;Empty My Shopping Cart&quot;),        to_page(&quot;Shirt&quot;),        to_page(&quot;Sweater&quot;),        to_page(&quot;Checkout&quot;));}############################## subroutines for each screen############################## The default page.sub front_page {    my $active = shift;    return unless $active;    print &quot;&lt;H1&gt;Hi!&lt;/H1&gt;\n&quot;;    print &quot;Welcome to our Shirt Shop!  Please make your selection from &quot;;    print &quot;the menu below.\n&quot;;    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(&quot;shirt_size&quot;), param(&quot;shirt_color&quot;), param(&quot;shirt_count&quot;));    # sanity check    if ($count) {        $color = $colors[0] unless grep { $_ eq $color } @colors;        $size  = $sizes[0]  unless grep { $_ eq $size  } @sizes;        param(&quot;shirt_color&quot;, $color);        param(&quot;shirt_size&quot;,  $size);    }    unless ($active) {        print hidden(&quot;shirt_size&quot;)  if $size;        print hidden(&quot;shirt_color&quot;) if $color;        print hidden(&quot;shirt_count&quot;) if $count;        return;    }    print h1(&quot;T-Shirt&quot;);    print p(&quot;What a shirt!  This baby is decked out with all the options.&quot;,        &quot;It comes with full luxury interior, cotton trim, and a collar&quot;,        &quot;to make your eyes water!  Unit price: \$33.00&quot;);    print h2(&quot;Options&quot;);    print p(&quot;How Many?&quot;, textfield(&quot;shirt_count&quot;));    print p(&quot;Size?&quot;,  popup_menu(&quot;shirt_size&quot;,  \@sizes ),        &quot;Color?&quot;, popup_menu(&quot;shirt_color&quot;, \@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(&quot;sweater_size&quot;), param(&quot;sweater_color&quot;), param(&quot;sweater_count"));    # sanity check    if ($count) {        $color = $colors[0] unless grep { $_ eq $color } @colors;        $size  = $sizes[0]  unless grep { $_ eq $size  } @sizes;        param(&quot;sweater_color&quot;, $color);        param(&quot;sweater_size&quot;,  $size);    }    unless ($active) {        print hidden(&quot;sweater_size&quot;)  if $size;        print hidden(&quot;sweater_color&quot;) if $color;        print hidden(&quot;sweater_count&quot;) if $count;        return;    }    print h1(&quot;Sweater&quot;);    print p(&quot;Nothing implies preppy elegance more than this fine&quot;,        &quot;sweater.  Made by peasant workers from black market silk,&quot;,        &quot;it slides onto your lean form and cries out ``Take me,&quot;,        &quot;for I am a god!''.  Unit price: \$49.99.&quot;);    print h2(&quot;Options&quot;);    print p(&quot;How Many?&quot;, textfield(&quot;sweater_count&quot;));    print p(&quot;Size?&quot;,  popup_menu(&quot;sweater_size&quot;,  \@sizes));    print p(&quot;Color?&quot;, popup_menu(&quot;sweater_color&quot;, \@colors));    shop_menu();}# Page to display current order for confirmation.sub checkout {    my $active = shift;    return unless $active;    print h1(&quot;Order Confirmation&quot;);    print p(&quot;You ordered the following:&quot;);    print order_text();    print p(&quot;Is this right?  Select 'Card' to pay for the items&quot;,        &quot;or 'Shirt' or 'Sweater' to continue shopping.&quot;);    print p(to_page(&quot;Card&quot;),        to_page(&quot;Shirt&quot;),         to_page(&quot;Sweater&quot;));}# 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(&quot;Name:          &quot;, textfield(&quot;Name&quot;)),      p(&quot;Address:       &quot;, textfield(&quot;Address1&quot;)),      p(&quot;               &quot;, textfield(&quot;Address2&quot;)),      p(&quot;City:          &quot;, textfield(&quot;City&quot;)),      p(&quot;Zip:           &quot;, textfield(&quot;Zip&quot;)),      p(&quot;State:         &quot;, textfield(&quot;State&quot;)),      p(&quot;Phone:         &quot;, textfield(&quot;Phone&quot;)),      p(&quot;Credit Card #: &quot;, textfield(&quot;Card&quot;)),      p(&quot;Expiry:        &quot;, textfield(&quot;Expiry&quot;)));    print p(&quot;Click on 'Order' to order the items.  Click on 'Cancel' to return shopping.");    print p(to_page(&quot;Order&quot;), to_page(&quot;Cancel&quot;));}# Page to complete an order.sub order {    my $active = shift;    unless ($active) {        return;    }    # you'd check credit card values here    print h1(&quot;Ordered!&quot;);    print p(&quot;You have ordered the following toppings:&quot;);    print order_text();    print p(defaults(&quot;Begin Again&quot;));}# Returns HTML for the current order (&quot;You have ordered ...&quot;)sub order_text {    my $html = '';    if (param(&quot;shirt_count&quot;)) {        $html .= p(&quot;You have ordered &quot;, param(&quot;shirt_count&quot;),           &quot; shirts of size &quot;,  param(&quot;shirt_size&quot;),           &quot; and color &quot;, param(&quot;shirt_color&quot;), &quot;.&quot;);    }    if (param(&quot;sweater_count&quot;)) {        $html .= p(&quot;You have ordered &quot;,  param(&quot;sweater_count&quot;),           &quot; sweaters of size &quot;, param(&quot;sweater_size&quot;),           &quot; and color &quot;, param(&quot;sweater_color&quot;), &quot;.&quot;);    }    $html = p(&quot;Nothing!&quot;) unless $html;    $html .= p(&quot;For a total cost of &quot;, calculate_price());    return $html;}sub calculate_price {    my $shirts   = param(&quot;shirt_count&quot;)   || 0;    my $sweaters = param(&quot;sweater_count&quot;) || 0;    return sprintf(&quot;\$%.2f&quot;, $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 =&gt; &quot;.State&quot;, -VALUE =&gt; 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 &copy; 2002</a> O'Reilly &amp; 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 + -