#!/usr/bin/perl -wT
=head1 Name
jumbler.pl - Jumble Solver Perl CGI
=head1 Description
A simple Perl CGI that solves word jumbles.
=head1 Arguments and Options
jumbler.pl expects only one argument and that is the jumble
it is to solve.
=head1 Prerequisites
jumbler.pl uses the CGI CPAN module.
jumbler.pl also needs to call a shell script called spelled
to do spell checking (see below). The script below assumes
your system has aspell installed.
=item Speller shell script
#!/bin/sh
# This file should be named spelled and exist in the same
# directory as jumbler.pl exist.
ASPELL=/usr/bin/aspell
GREP=/usr/bin/grep
EGREP=/usr/bin/egrep
for i in $*
do
correct=`echo $i | $ASPELL -a | $GREP -v '^@' | $GREP -v '^&' | $EGREP -v '^$'`
if [ ! -z "$correct" ]
then
echo "$i"
fi
done
=head1 Example use
=item Command-line
Plankton@Chum-Bucket ~/cgi-bin
$ ./jumbler.pl WORD=enmog
Content-Type: text/html; charset=ISO-8859-1
Jumble solver
enmog
.oOo.oOo.o[gnome]
Oo
=item CGI
Jumbler
=head1 Miscellaneous information
jumbler.pl is an example of Perl CGI that use the CPAN module CGI.
jumbler.pl uses $| and "print" to prevent the browser from timing out.
jumbler.pl is a example of the use of a recursive permution algorithm.
=cut
$ENV{'PATH'} = "";
use strict;
use CGI;
use CGI::Carp qw /fatalsToBrowser/;
my $out = new CGI;
my $word = lc($out->param ( 'WORD' ));
$word =~ s/ //g;
# don't use this anymore! $|++;
# do this ...
$|=1;
# ... instead
# see http://www.perlmonks.com/index.pl?node_id=344772
my $loopCount = 0;
my $charCount = 0;
($word) = $word =~ /^(\w+)$/ or die "Bad User! passing word=[$word]";
my @chars = split //, $word;
my $speller ="spelled";
sub spelled {
my $word = shift;
my $out=`$speller $word`;
chomp($out);
if ( ($loopCount++ % 10) == 0 ) {
print "." if ($charCount % 4) == 0;
print "o" if ($charCount % 4) == 1;
print "O" if ($charCount % 4) == 2;
print "o" if ($charCount % 4) == 3;
$charCount++;
print "
\n" if ($charCount % 29 ) == 0;
}
if ( length($out) > 0 ) {
print "[$out]
\n";
}
}
sub swap {
my $i = shift;
my $j = shift;
my $A = shift; # ref to array
my $tmp = @{$A}[$i];
@{$A}[$i] = @{$A}[$j];
@{$A}[$j] = $tmp;
}
sub permute {
my $i = shift;
my $n = shift;
my $T = shift; #ref to array
if ( $i == $n ) {
my $word = join( "", @{$T} );
spelled( $word );
} else {
for my $j ( $i..$n) {
swap( $i-1, $j-1, $T );
permute( $i+1, $n, $T );
swap( $i-1, $j-1, $T );
}
}
}
print $out->header( 'text/html' );
print $out->start_html( 'Jumble solver' );
print "
$word
\n";
permute (1,$#chars+1,\@chars);
print $out->end_html();