Building a Pizza: Taking the Order
June 14, 1999
sub build_order()
#build the pizza order
{ unless ($newOrderFlag)
{ $greeting="Welcome! Please begin building your pizza order." }
else
{ $greeting="<H3>Build-a-Pizza</H3>".
"You may modify or finalize ".
"your order below." }
$greeting.="<br>Current pizza price: <font color='red'>\$".
sprintf("%.2f",&calcTotal())."</font>.";
print $cgiobject->header;
print $cgiobject->
start_html(-title=>'Build-a-Pizza',-bgcolor=>'white');
print "<H2>$greeting</H2>";
&output_form;
print $cgiobject->end_html;
}
The build_order subroutine controls the
output of the order form page, and therefore is responsible for
displaying the updated order. First, it tests whether
this is the start of a new order or an updated order via
the $newOrderFlag variable (not related to the
seminal 80's synth pop band). In fact, we'll see later that
this flag is actually tied to a hidden form field which
is planted in the output page.
The hidden form field is simply a way of storing a value
on a page, such that the value is returned to the script
when the page is submitted. Hidden form fields are a
convenient way of "faking" a memory on the Web --
much like writing down a phone number on a scrap of
paper and then recalling that paper later to dial the number
-- no actual memorizing was involved.
In this application, when the CGI detects that a new
order is being placed, because no parameters were submitted,
the newOrderFlag hidden form field is not yet
planted in the page. However, once the CGI receives an update
submission (parameters are submitted), the newOrderFlag
is planted to indicate that this is not a new order.
Returning to build_order, an appropriate greeting
is output followed by the current price as calculated
by the calcTotal subroutine. We've used the
formatted output function sprintf() to format the price
into a two-decimal place floating point number, simply
to prevent strange results such as "$10.5" where
we prefer "$10.50".
At its heart, build_order calls the subroutine
output_form which will do most of the work in
constructing the content of the Web page.
sub init()
#define initial values for pizza parameters
{ $name="Your name";
$address="Where to?";
$phone="Your phone";
$deliver="deliver";
$toppings="pepperoni";
$size="large";
}
In case of a new order, the init subroutine
defines the initial values for the variables which determine
the states of the various form fields.
sub get_state_variables()
#grab any parameters which were submitted
{ $name=$cgiobject->param("order_name");
$address=$cgiobject->param("order_address");
$phone=$cgiobject->param("order_phone");
$deliver=$cgiobject->param("order_deliver");
$toppings=$cgiobject->param("order_toppings");
$size=$cgiobject->param("order_size");
$newOrderFlag=$cgiobject->param("newOrderFlag");
$orderComplete=$cgiobject->param("finish")
}
In case of an updated order, the get_state_variables
subroutine employs the param method of the
CGI object to retrieve the value of each form field and
assigns these values to their respective Perl variables.
Single-Session State Maintenance AKA "Building a Pizza" Scenario
The Perl You Need to Know
Building a Pizza: Calculating Cost
|