dポイントプレゼントキャンペーン実施中!

XML::RSSを使用してRSSのタイトルを取得するPerlスクリプトを作っています。
DBCSの場合、文字化けするので、以下のようにfrom_toを使用してutf8をShiftJISに変換しようとしましたが、エラーが出ます。
SBCSでは問題無いのですが。

Encode::Guessで調べるとutf8が返ってくるので、「from_to( $title, 'utf8','shiftjis' )」でutf8をShift-JISに変換すればよいのかと思ったのですが。

■環境
・perl v5.8.3 built for MSWin32-x86-multi-thread
・Windows2000(SP4)

■エラーメッセージ
Cannot decode string with wide characters at c:/Perl/lib/Encode.pm line 184.

■ソース
#!/usr/bin/perl
BEGIN {
use CGI::Carp qw(carpout fatalsToBrowser);
carpout(STDOUT);
}
use Encode qw/ from_to /;
use LWP::Simple 'get';
use XML::RSS ;

print"Content-type: text/html\n\n";
print"<HTML>\n";
print"<HEAD><meta http-equiv=\"Content-Type\" content=\"text/html;charset=Shift-JIS\"></HEAD>\n";
print"<BODY>\n";

my $rss = new XML::RSS;

eval {
  # $rss->parse( get('http://slashdot.org/slashdot.rdf') );
   $rss->parse( get('http://oshiete1.goo.ne.jp/rss/oshiete_goo.xml') );
};

exit if $@;

foreach my $ref( @{$rss->{items}} ) {
  $title = $ref->{'title'};
  $link = $ref->{'link'};
  from_to( $title, 'utf8','shiftjis' );
  print"[title] $title<br>\n";
  print"[link] <a href=\"$link\" target=\"_blank\">$link</a><br>\n";
  ----------------------------<br><br>\n";
}

print"</BODY></HTML>\n";



RSSの取得方法はここを参考にしました。
http://www.rfs.jp/sb/perl/10/rss01.html

宜しくお願いします。

A 回答 (1件)

スクリプトの先頭に


use encoding 'UTF-8', STDOUT => 'cp932';
として
from_to の行はコメントアウトする。
    • good
    • 0
この回答へのお礼

ありがとうございます。
文字化けすることはく、Shift-JISで表示できました。

お礼日時:2006/09/04 18:08

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!