#!/usr/bin/perl -T use strict; use warnings; use CGI::Simple; use DBD::SQLite; use HTML::Template; use Storable; $CGI::Simple::POST_MAX = 1024; $CGI::Simple::DISABLE_UPLOADS = 1; #$CGI::Simple::DEBUG = 1; my $q = CGI::Simple->new(); # TODO: if unable to get exclusive lock on $0 display_error() # TODO: Add error handling - duh! $q->param('answer') ? display_answer() : display_question(); sub display_question { my $template = HTML::Template->new(filename => 'question.tmpl'); print $q->header; print $template->output(); exit(0); } sub display_answer { my $template = HTML::Template->new(filename => 'answer.tmpl'); my $input = get_input(); my $len = length($input); my $rev = retrieve('sol.rev'); my $dbh = DBI->connect("dbi:SQLite:dbname=solution.db","","") or die $DBI::errstr; my $sth = $dbh->prepare("SELECT solution FROM solution$len WHERE nform=?") or die $dbh->errstr; $sth->execute($input) or die $dbh->errstr; my @word; while (my @row = $sth->fetchrow_array() ) { push @word, map { {WORD => $rev->{$_}} } split /-/, $row[0]; } $template->param(USER_INPUT => $input); $template->param(WORD_LIST => \@word); print $q->header; print $template->output(); #$sth->finish(); #$dbh->disconnect; exit(0); } sub get_input { my $input = $q->param('question') || ''; $input = lc($input); $input =~ tr/a-z//cd; $input ||= 'abcdefghijklmnopqrstuvwxyz'; my %uniq = map {$_ => undef} split //, $input; return join '', sort keys %uniq; }