#!/usr/bin/perl use strict; $|=1; my $MAXHITS = 25; # constant: number of hits per page my $TMP = "/tmp/more."; # constant: location of session files use lib "/home/merlyn/Lwp"; use HTML::Entities qw(encode); use CGI; my $session; # global: session-ID my $search; # global: search string my @found; # global: array of valid hits my $query = new CGI; print $query->header; print $query->start_html( 'So you want more???', 'merlyn@stonehenge.com' ); print "

Query the dictionary

\n"; if ($session = $query->param('session')) { ## we are in the midst of a session &load_session(); # sets $search, @found &display($query->param('start')); } elsif ($search = $query->param('search')) { ## we are beginning the query ## perform the query, and set up for session if necessary open WORDS,"/usr/dict/words"; chomp(@found = grep /$search/o, ); close WORDS; $session = unpack("H*", pack("Nn", time, $$)); # 12 hex chars &save_session(); &display(0); } else { ## we are being invoked initially ## print the basic search form &search_form(); } print $query->end_html; exit 0; sub load_session { open TMP, "<$TMP$session" or die "missing session file $TMP$session: $!"; chop(($search, @found) = ); close TMP; } sub save_session { open TMP,">$TMP$session" or die "Cannot create $TMP$session: $!"; print TMP map "$_\n", $search, @found; close TMP; } sub display { my $start = shift; # where to start (undef/0 if beginning) print "You are searching for: ", encode($search), "\n"; my $low = $start; ## sanity checking... won't happen unless user fakes us out $low = 0 if ($low || 0) <= 0; $low = $#found if $low > $#found; my $high = $low + $MAXHITS - 1; $high = $#found if $high > $#found; print "
Hits ", $low + 1, "..", $high + 1, " (of ".@found.") hits:\n", "
\n",
    (map { encode($_)."\n" } @found[$low..$high]),
    "
\n", "
\n"; if ($high < $#found) { print "
script_name, "?session=$session&start=", $low + $MAXHITS, "\">", "See next $MAXHITS hits..."; } if ($low > 0) { print "
script_name, "?session=$session&start=", $low - $MAXHITS, "\">", "See previous $MAXHITS hits..."; } } sub search_form { print <<_FORM_;
@{[$query->startform('POST',$query->script_name)]}

Search for: @{[$query->textfield('search')]}

@{[$query->submit('Search')]} @{[$query->endform]}


_FORM_ }