#!/usr/bin/env perl use strict; use warnings; use CGI qw(:standard :netscape); use CGI::Carp qw/fatalsToBrowser warningsToBrowser/; warningsToBrowser(1); sub do_form; if (param('name') || cookie('perls10-name')){ #if the form was submitted #Get the number from a cookie, if it exists. my $num = cookie('perls10-num'); #create the new Number cookie $num++; my $numcookie = cookie(-name=>'perls10-num', -value=>$num, -expires=>'+30m'); my $name; #if the user just entered a new name... if (param('name')){ #create it my $namecookie = cookie(-name=>'perls10-name', -value=>param('name'), -expires=>'+30m'); #and set it (along with the number cookie) print header(-cookie=>[$namecookie,$numcookie]); #get the name from the textbox $name = param('name'); } else { #else, set the number cookie, and get the name from the name cookie print header(-cookie=>$numcookie); $name = cookie('perls10-name'); } #Welcome the user back and print out how many times he's been here print start_html("Welcome back $name!"); print "Hello $name. You've been here $num times before
\n"; #print out the form do_form(); } else { #else, user never been here. print header; print start_html("Welcome, new user"); do_form(); } sub do_form{ print start_form; print "Enter your name: \n"; print textfield(-name=>'name', -default=>'', -override=>1); print submit(-name=>'submit', -value=>'Tell me your name!'); print end_form; print end_html; }