use strict; use warnings; use CGI qw(:standard); use Template; use JSON::XS; # Don't use CGI::Carp in production, it's here to make your life # easier while you experiment with this. use CGI::Carp qw(fatalsToBrowser); my $default = "splash"; my %actions = ( $default => \&show_form, ajax_lookup => \&ajax_lookup, ); my $action = param("x") || $default; my $executable = $actions{$action} || \&no_such_action; $executable->(); # Subroutines #--------------------------------------------------------------------- sub show_form { print header(); my $tt2 = Template->new(); $tt2->process(\*DATA, { self_uri => CGI::url(), usernames => [ map { $_->{username} } @{_dummy_db()} ], }) or die $tt2->error; } sub ajax_lookup { my $data = _dummy_db(); my $query = param("username"); my $result = {}; for my $row ( @{$data} ) { $result = $row if lc($query) eq lc($row->{username}); } print header("application/json"), encode_json($result); } sub no_such_action { print header(), h1("Nopers! Don't know how to do that!"); } sub _dummy_db { return [ { username => "paco", fish => "Sunfish", }, { username => "YourUncle", fish => "Coelacanth", }, { username => "hiragana", fish => "Monkfish", }, { username => "MosaicNL", fish => "Sixgill Shark", }, ]; } __DATA__ OH, HAI! I CAN HAS AJAKS?
Simplistic Ajax Example

Usernames you can search for: [% usernames.join(", ") %].