#!/usr/bin/perl use CGI ':standard'; use CGI::Carp 'fatalsToBrowser'; my $tax_rate = .0825; my %cheese_prices = (mozarella => 0, asiago => 300, reggiano => 200, romano => 200,); my @cheeses = keys %cheese_prices; my @toppings = (qw(Pepperoni Sausage Mushrooms Peppers Onions Anchovies Ham Pineapple), 'Artichoke Hearts', 'Extra Cheese', 'Smoked Meat', 'Spinach', 'Asparagus'); my %sizes = (Micro => 8, Small => 10, Medium => 12, Large => 16); my %size_name = reverse %sizes; my @sizes = keys %sizes; my @crusts = ('Regular', 'New York Style', 'Chicago Style', ); sub show_form { my ($message) = @_; if ($message) { print p(font({COLOR => 'red'}, $message)); } print start_form(); print textfield('name'), 'Name', br(); print textfield('address'), 'Address', br(); print password_field('secret'), 'Secret frequent pizza number', br(); print checkbox_group(-name=>'toppings', -values=> \@toppings, -columns => 3, ), br(); print radio_group(-name=>'size', -values=> [sort values %sizes], -labels => \%size_name, -rows => 1, ), br(); print popup_menu(-name=>'crust', -values=> \@crusts, ), br(); print scrolling_list(-name=>'cheese', -values=>\@cheeses, -default => ['Mozarella'], -multiple=>'true'); print hidden(-name => 'version_number', -value => '1'); print submit(); print end_form(); } my $price_per_sq_in = 75; sub compute_price { my $area = $PI/4 * $F::size**2; my $base_cost = $area * $price_per_sq_in; my $cheese_price = 0; my $topping_price = 150 * @F::toppings; my $c; foreach $c (@F::cheeses) { $cheese_price += $cheese_price{$c}; } my $total_price = $base_cost + $cheese_price + $topping_price; $total_price += $total_price * $tax_rate; } sub display_confirmation { my $price = compute_price(); print p( "You ordered a $size_name{$F::size} pizza with", (join ", ", @F::toppings), "and a $F::crust crust with @F::cheeses cheese.\n", ); print p("The total price will be ", sprintf("%.2f", $price/100)); print p($CGI::Q -> dump()); } sub order_makes_sense { my $good = 1; unless ($F::secret =~ /^\d{6}$/ || $F::secret eq '') { $CGI::Q->delete('secret'); $good = 0; } $good = 0 unless $F::name; $good = 0 unless $F::address; if ( grep(/Anchovies/, @F::toppings) && grep(/Pineapple/, @F::toppings)) { # Gack $good = 0; # $CGI::Q -> delete('toppings'); param('toppings', grep (($_ ne 'Anchovies' && $_ ne 'Pineapple'), @F::toppings) ); } return $good; } print header(), start_html(); if (param()) { import_names('F'); if (order_makes_sense()) { display_confirmation(); } else { show_form("Sorry, you made a mistake. Please re-enter the missing information and try again."); } } else { show_form(); } print end_html();