Listing 3. check-name-exists.pl

#!/usr/local/bin/perl

use strict;
use diagnostics;
use warnings;

use CGI;
use CGI::Carp;

# Define the usernames that are taken
# (Use a hash for lookup efficiency)
my %usernames = ('abc' => 1,
         'def' => 1,
         'ghi' => 1,
         'jkl' => 1);

# ------------------------------------------------------------
my $query = new CGI;
print $query->header("text/plain");

# Get the POST data
my $postdata = $query->param("POSTDATA");

# Get the username
my ($name, $value) = split /=/, $postdata;

my $username = '';
if ($name eq 'username')
{
    $username = $value;
}

# If this username is defined, say "yes"!
if (exists $usernames{$username})
{
    print "yes";
}

# Otherwise, say "no"!
else
{
    print "no";
}