xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/CGI/eg/cookie.cgi (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!/usr/local/bin/perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateuse CGI qw(:standard);
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gate@ANIMALS=sort qw/lion tiger bear pig porcupine ferret zebra gnu ostrich
6*0Sstevel@tonic-gate    emu moa goat weasel yak chicken sheep hyena dodo lounge-lizard
7*0Sstevel@tonic-gate    squirrel rat mouse hedgehog racoon baboon kangaroo hippopotamus
8*0Sstevel@tonic-gate    giraffe/;
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gate# Recover the previous animals from the magic cookie.
11*0Sstevel@tonic-gate# The cookie has been formatted as an associative array
12*0Sstevel@tonic-gate# mapping animal name to the number of animals.
13*0Sstevel@tonic-gate%zoo = cookie('animals');
14*0Sstevel@tonic-gate
15*0Sstevel@tonic-gate# Recover the new animal(s) from the parameter 'new_animal'
16*0Sstevel@tonic-gate@new = param('new_animals');
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gate# If the action is 'add', then add new animals to the zoo.  Otherwise
19*0Sstevel@tonic-gate# delete them.
20*0Sstevel@tonic-gateforeach (@new) {
21*0Sstevel@tonic-gate    if (param('action') eq 'Add') {
22*0Sstevel@tonic-gate	$zoo{$_}++;
23*0Sstevel@tonic-gate    } elsif (param('action') eq 'Delete') {
24*0Sstevel@tonic-gate	$zoo{$_}-- if $zoo{$_};
25*0Sstevel@tonic-gate	delete $zoo{$_} unless $zoo{$_};
26*0Sstevel@tonic-gate    }
27*0Sstevel@tonic-gate}
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate# Add new animals to old, and put them in a cookie
30*0Sstevel@tonic-gate$the_cookie = cookie(-name=>'animals',
31*0Sstevel@tonic-gate		     -value=>\%zoo,
32*0Sstevel@tonic-gate		     -expires=>'+1h');
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate# Print the header, incorporating the cookie and the expiration date...
35*0Sstevel@tonic-gateprint header(-cookie=>$the_cookie);
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate# Now we're ready to create our HTML page.
38*0Sstevel@tonic-gateprint start_html('Animal crackers');
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gateprint <<EOF;
41*0Sstevel@tonic-gate<h1>Animal Crackers</h1>
42*0Sstevel@tonic-gateChoose the animals you want to add to the zoo, and click "add".
43*0Sstevel@tonic-gateCome back to this page any time within the next hour and the list of
44*0Sstevel@tonic-gateanimals in the zoo will be resurrected.  You can even quit Netscape
45*0Sstevel@tonic-gatecompletely!
46*0Sstevel@tonic-gate<p>
47*0Sstevel@tonic-gateTry adding the same animal several times to the list.  Does this
48*0Sstevel@tonic-gateremind you vaguely of a shopping cart?
49*0Sstevel@tonic-gate<p>
50*0Sstevel@tonic-gate<em>This script only works with Netscape browsers</em>
51*0Sstevel@tonic-gate<p>
52*0Sstevel@tonic-gate<center>
53*0Sstevel@tonic-gate<table border>
54*0Sstevel@tonic-gate<tr><th>Add/Delete<th>Current Contents
55*0Sstevel@tonic-gateEOF
56*0Sstevel@tonic-gate    ;
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gateprint "<tr><td>",start_form;
59*0Sstevel@tonic-gateprint scrolling_list(-name=>'new_animals',
60*0Sstevel@tonic-gate		     -values=>[@ANIMALS],
61*0Sstevel@tonic-gate		     -multiple=>1,
62*0Sstevel@tonic-gate		     -override=>1,
63*0Sstevel@tonic-gate		     -size=>10),"<br>";
64*0Sstevel@tonic-gateprint submit(-name=>'action',-value=>'Delete'),
65*0Sstevel@tonic-gate    submit(-name=>'action',-value=>'Add');
66*0Sstevel@tonic-gateprint end_form;
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gateprint "<td>";
69*0Sstevel@tonic-gateif (%zoo) {			# make a table
70*0Sstevel@tonic-gate    print "<ul>\n";
71*0Sstevel@tonic-gate    foreach (sort keys %zoo) {
72*0Sstevel@tonic-gate	print "<li>$zoo{$_} $_\n";
73*0Sstevel@tonic-gate    }
74*0Sstevel@tonic-gate    print "</ul>\n";
75*0Sstevel@tonic-gate} else {
76*0Sstevel@tonic-gate    print "<strong>The zoo is empty.</strong>\n";
77*0Sstevel@tonic-gate}
78*0Sstevel@tonic-gateprint "</table></center>";
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gateprint <<EOF;
81*0Sstevel@tonic-gate<hr>
82*0Sstevel@tonic-gate<ADDRESS>Lincoln D. Stein</ADDRESS><BR>
83*0Sstevel@tonic-gate<A HREF="./">More Examples</A>
84*0Sstevel@tonic-gateEOF
85*0Sstevel@tonic-gate    ;
86*0Sstevel@tonic-gateprint end_html;
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate
89