
良質で美しいブナ材の木目を生かしたシンプルに仕上げられた積み木です。
#!/usr/bin/perl -T
use strict;
use warnings;
use CGI;
use HTML::Template;
my $filename = "test.txt";
my $n = 10;
my $q = new CGI;
my $from = $q->param('f') || 1;
my $next_line = $from + $n;
my $pre_line = ($from - $n > 1) ? $from - $n : 1;
my $key_org = $q->param('key') || "";
my $key = quotemeta $key_org;
$key =~ s/[<>]//g;
my $url = $q->url(-query => 1);
$url =~ s/[;&]f=(\d+)//;
print $q->header(-charset => 'UTF-8');
my $str = "";
if (not $key =~ /^\s*$/) {
if (open(my $fh, "<", $filename)) {
my $count = 0;
while (<$fh>) {
my $line = $_;
next if not $line =~ s|($key)|<font color="red">$1</font>|g;
$count++;
next if $count < $from;
last if $count >= $from + $n;
$str .= "$count: ".$line;
}
close($fh);
$str = "NOT FOUND" if $str eq "";
} else {
$str = "ERROR: Can't open '$filename'";
}
}
my $template = join("", <DATA>);
my $t = HTML::Template->new(scalarref => \$template,
global_vars => 1,
die_on_bad_params => 0);
$t->param(from => $from);
$t->param(str => $str);
$t->param(next_line => $next_line);
$t->param(pre_line => $pre_line);
$t->param(path => $url);
$t->param(key => $key_org);
print $t->output();
__DATA__
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Text Search</title>
</head>
<body>
<form action="" method="get">
<input type="text" name="key" value="<TMPL_VAR name=key>">
<input type="submit">
</form>
<TMPL_IF name=str>
<a href="<TMPL_VAR name=path>&f=<TMPL_VAR name=pre_line>"><<</a>
<a href="<TMPL_VAR name=path>&f=<TMPL_VAR name=next_line>">>></a>
<hr>
<pre><TMPL_VAR name=str></pre>
<hr>
<a href="<TMPL_VAR name=path>&f=<TMPL_VAR name=pre_line>"><<</a>
<a href="<TMPL_VAR name=path>&f=<TMPL_VAR name=next_line>">>></a>
</TMPL_IF>
</body>
</html>