Listing 3. vote.pl

#!/usr/bin/perl -wT
# vote.pl
use strict;
use diagnostics;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use GIFgraph::pie;
# Create an instance of CGI
my $query = new CGI;
# Define arrays to contain our votes
my @names;
my @votes;
# Open the file, and read the data from it
my $filename = "/tmp/votes.txt";
open (VOTES, $filename) ||
    die "Cannot open \"$filename\"
    "for reading: $! ";
while (<VOTES>)
{
    # Ignore blank lines
    next unless /\w/;
    # Remove trailing newlines
    chomp;
    # Separate the name from the votes
    my ($name, $votes) = split /\t+/;
    push @names, $name;
    push @votes, $votes;
}
close (VOTES);
my @data = (\@names, \@votes);
# Send an appropriate MIME header
print $query->header(-type => "image/gif");
# Create a graph object
my $graph = new GIFgraph::pie;
# Draw the graph
print $graph->plot(\@data);