OK, I'm a newbie trying to do things in perl that are smarter than I am.  I'm working 
on a CGI clock-in program for where I work, and I want it to be one CGI script with 
multiple pages, each a step in the clocking in process (gathering different bits of 
info and confirming).  I got the idea of doing it this way when I saw the example in 
the Perl Cookbook, at the end of Chapter 19.  I looked it over, and started trying to 
generate the basics of my own program using the other as a model (read: 'borrowing' 
bits of that code).  

What I'm having trouble understanding is how, when the script is run, only one page 
comes up in the browser.  When I did my example, using pretty much the same code, I 
got all my pages dumped on one page together in the browser.  The part of code that 
worked in the example (and mine is basically identical) is:
(All this code is from the Perl Cookbook by Tom Christiansen & Nathan Torkington, and 
this code is theirs not mine)

my %States;             # state table mapping pages to functions
my $Current_Screen;     # the current screen

# Hash of pages and functions.

%States = (
    'Default'     => \&front_page,
    'Shirt'       => \&shirt,
    'Sweater'     => \&sweater,
    'Checkout'    => \&checkout,
    'Card'        => \&credit_card,
    'Order'       => \&order,
    'Cancel'      => \&front_page,
);

$Current_Screen = param(".State") || "Default";
die "No screen for $Current_Screen" unless $States{$Current_Screen};

# Generate the current page.

standard_header();

while (my($screen_name, $function) = each %States) {
    $function->($screen_name eq $Current_Screen);
}
standard_footer();
exit;

Now, I think I know what's going on here basically, making references to the 
subroutines where the pages are detailed.  My program just spits out all the pages at 
once.  The example doesn't, it does it correctly.  Here's one of the page subroutines:

# 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("shirt_size"), param("shirt_color"), param("shirt_count"));

    # sanity check
    if ($count) {
        $color = $colors[0] unless grep { $_ eq $color } @colors;
        $size  = $sizes[0]  unless grep { $_ eq $size  } @sizes;
        param("shirt_color", $color);
        param("shirt_size",  $size);
    }

    unless ($active) {
        print hidden("shirt_size")  if $size;
        print hidden("shirt_color") if $color;
        print hidden("shirt_count") if $count;
        return;
    }

    print h1("T-Shirt");
    print p("What a shirt!  This baby is decked out with all the options.",
        "It comes with full luxury interior, cotton trim, and a collar",
        "to make your eyes water!  Unit price: \$33.00");

    print h2("Options");
    print p("How Many?", textfield("shirt_count"));
    print p("Size?",  popup_menu("shirt_size",  \@sizes ),
        "Color?", popup_menu("shirt_color", \@colors));

    shop_menu();
}

Now, I'm guessing the $active variable has something to do with my problem.  Can 
someone explain what's happening here?  I'm totally confused.  

Justin

Reply via email to