Listing 3: update-counter.pl, Increment and Display Cookie Value

#!/usr/bin/perl -w
use strict;
use diagnostics;
use CGI;

# Create an instance of CGI
my $query = new CGI;

# Get the "counter" cookie
my $old_value = $query->cookie("counter");

# Increment the counter (if it is new, then
# $old value = 0)
my $new_value = $old_value + 1;

# Create a new cookie with this updated value
my $cookie = $query->cookie(-name => "counter",
                            -value => $new_value);

# Now that we have updated the value, produce some
# HTML output
print $query->header(-type => "text/html",
   -cookie => $cookie);
print $query->start_html(-title =>
   "Counter updated");

print "<P>Counter was $old_value, ";
print "now is $new_value</P>\n";

# Send some HTML as a placeholder
print $query->end_html;