ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
19.7 Other Form ElementsNow that you know how to create simple text fields in your form and respond to them, you're probably wondering how to make the other kinds of widgets you've seen, like buttons, checkboxes, and menus. Here's a more elaborate version of our program. We've thrown in some new widgets: popup menus, a submit button (named "order"), and a button to reset the entire form, erasing all user input. Popup menus are pretty much just what they say they are, but the arguments given to #!/usr/bin/perl -w # cgi-bin/ice_cream: program to answer and generate ice cream # order form (version 4) use strict; # enforce variable declarations and quoting use CGI qw(:standard); print header, start_html("Ice Cream Stand"), h1("Ice Cream Stand"); if (param()) { # the form has already been filled out my $who = param("name"); my $flavor = param("flavor"); my $scoops = param("scoops"); my $taxrate = 1.0743; my $cost = sprintf("%.2f", $taxrate * (1.00 + $scoops * 0.25)); print p("Ok, $who, have $scoops scoops of $flavor for \$$cost."); } else { # first time through, so present clean form print hr(); # draw a horizontal rule before the form print start_form(); print p("What's your name? ", textfield("name")); # FOR EXPLANATION OF FOLLOWING TWO LINES, SEE NEXT SECTION print p("What flavor: ", popup_menu("flavor", ['mint','cherry','mocha'])); print p("How many scoops? ", popup_menu("scoops", [ 1..3 ])); print p(submit("order"), reset("clear")); print end_form(), hr(); } print end_html; Figure 19.4 shows the initial screen it generates. Figure 19.4: A slightly more elaborate fill-out formAs you'll recall, the 19.7.1 ReferencesYou may have noticed that the popup_menu() functions in the previous example both have a strange kind of argument. Just what are @choices = ('mint','cherry','mocha'); print p("What flavor: ", popup_menu("flavor", \@choices)); works just as well as this: print p("What flavor: ", popup_menu("flavor", ['mint','cherry','mocha'])); References behave somewhat as pointers do in other languages, but with less danger of error. They're values that refer to other values (or variables). Perl references are very strongly typed (and uncastable), and they can never cause core dumps. Even better, the memory storage pointed to by references is automatically reclaimed when it's no longer used. References play a central role in object-oriented programming. They're also used in traditional programming, forming the basis for data structures more complex than simple one-dimensional arrays and hashes. Perl supports references to both named and anonymous scalars, arrays, hashes, and functions. Just as you can create references to named arrays with { key1, value1, key2, value2, ... }
You can learn more about references in Chapter 4 of Programming Perl, or the perlref (1) manpage. 19.7.2 Fancier Calling SequencesWe'll round out the discussion of form widgets by creating a really fancy widget - one that allows the user to select any number of its items. The To add a scrolling list to a form, here's all you need to do: print scrolling_list( -NAME => "flavors", -VALUES => [ qw(mint chocolate cherry vanilla peach) ], -LABELS => { mint => "Mighty Mint", chocolate => "Cherished Chocolate", cherry => "Cheery Cherry", vanilla => "Very Vanilla", peach => "Perfectly Peachy", }, -SIZE => 3, -MULTIPLE => 1, # 1 for true, 0 for false ); The parameter values have meanings as follows:
When you've set @choices = param("flavors"); Here's another way to create the same scrolling list, passing a reference to an existing hash instead of creating one on the fly: %flavors = ( mint => "Mighty Mint", chocolate => "Cherished Chocolate", cherry => "Cheery Cherry", vanilla => "Very Vanilla", peach => "Perfectly Peachy", ); print scrolling_list( -NAME => "flavors", -LABELS => \%flavors, -VALUES => [ keys %flavors ], -SIZE => 3, -MULTIPLE => 1, # 1 for true, 0 for false ); This time we send in values computed from the keys of the |