#!/usr/bin/perl -T use strict; use warnings; use Encode; use CGI; use LWP::Simple; use XML::Simple; use HTML::Template; my $q = new CGI; my $key = $q->param('key') || ""; Encode::from_to($key, "utf8", "euc-jp"); $key =~ s/([^0-9A-Za-z_])/'%'.unpack('H2',$1)/ge; $key =~ s/ /+/g; my $url = "http://search.chiebukuro.yahoo.co.jp/search/search.php?p=$key&rss=1"; my $r = get_cresults($url); my $template = join("", <DATA>); my $t = HTML::Template->new(scalarref => \$template, associate => $q, die_on_bad_params => 0); $t->param(results => $r); print $q->header(-charset => 'UTF-8'), $t->output(); sub get_cresults { my ($url) = @_; my $rss = get($url); my $xmlsimple = XML::Simple->new(); my $xml = $xmlsimple->XMLin($rss); if (ref($xml->{channel}->{item}) eq "ARRAY") { return $xml->{channel}->{item}; } else { return [$xml->{channel}->{item}]; } return []; } __DATA__ <html lang="ja"><head><title>search.chiebukuro (RSS)</title></head><body> <h1>search.chiebukuro (RSS)</h1> <form method="get"> <input type="text" name="key" value="<TMPL_VAR name=key>" size="20"> <input type="submit" value="search"> </form> <h2>Search Results</h2> <TMPL_LOOP name=results> <h3><a href="<TMPL_VAR name=link>"><TMPL_VAR name=title></a><br> <TMPL_VAR name=category></h3><p><TMPL_VAR name=description></p> </TMPL_LOOP> </body></html>