#!/usr/bin/perl -T use strict; use warnings; use CGI; use SUFARY; use Encode; use URI::Escape; use HTML::Template; use utf8; binmode STDOUT, ":utf8"; my $fn = "test-dic.txt"; my $sa = SUFARY->new($fn); my $q = new CGI; my $key = $q->param('key'); my $start = $q->param('start') || 1; my $num = $q->param('num') || 10; my $r_ref = search($sa, $key); my $template = join("", <DATA>); my $t = HTML::Template->new(scalarref => \$template, global_vars => 1, die_on_bad_params => 0); $t->param(key => $key); $t->param(ekey => URI::Escape::uri_escape($key)); $t->param(results => $r_ref->{cont}) if %$r_ref; $t->param(pre => $r_ref->{pre}) if %$r_ref; $t->param(nex => $r_ref->{nex}) if %$r_ref; print $q->header(-charset => 'UTF-8'), decode('utf-8', $t->output()); sub search { my ($sa, $key) = @_; return {} if $key eq ""; my ($left, $right) = $sa->range_search($key); return {} if not defined $left and not defined $right; my $n = $right - $left + 1; my $from = $left + $start - 1; return {} if $right < $from; my $to = $from + $num - 1; $to = $right if $to > $right; my @rv; for (my $k = $from; $k <= $to; $k++) { my $pos = $sa->get_position($k); my $str = $sa->get_line($pos); push @rv, {line => $str}; } my $pre = ($start - $num > 0) ? $start - $num : 0; my $nex = ($start + $num <= $n) ? $start + $num : 0; return {cont => \@rv, pre => $pre, nex => $nex}; } __DATA__ <html lang="ja"> <head> <meta http-equiv="Content-Type" contet="text/html; charset=UTF-8"> <title></title> </head> <body> <h1></h1> <form> <input type="input" name="key" size="30" value="<TMPL_VAR name=key>"> <input type="submit"> </form> <TMPL_IF name=results> <TMPL_LOOP name=results> <TMPL_VAR name=line><br> </TMPL_LOOP> </TMPL_IF> <TMPL_IF name=pre> <a href="?key=<TMPL_VAR name=ekey>&start=<TMPL_VAR name=pre>"><<</a> </TMPL_IF> <TMPL_IF name=nex> <a href="?key=<TMPL_VAR name=ekey>&start=<TMPL_VAR name=nex>">>></a> </TMPL_IF> </body> </html>
「-l」オプションで行頭にインデックスを張ります。% tail -5 test-dic.txt 龍尾神社 龍滕 LONG TENG(赤坂) 1万円入りま〜す 1日なのでお休みです Tシャツ・ラブ・サミットでTシャツを買ってきた! % mkary -l test-dic.txt