プロが教えるわが家の防犯対策術!

Perlのプログラムのエラーです。ローカルでは問題なく動きました。サーバはさくらサーバで文字コードはEUC-JPです。
CGIから文章をMECAPIという形態素解析APIに送って、品詞ごとにファイルに書き込みます。

Status: 500 Content-type: text/html
Software error:
Can't locate object method "content" via package "HTTP::Response=HASH(0x86e9250)" (perhaps you forgot to load "HTTP::Response=HASH(0x86e9250)"?) at test9.cgi line 49.

#!/usr/local/bin/perl

use strict;
use warnings;
use LWP::UserAgent;
use Encode qw/encode decode/;
use utf8;
use CGI;
use CGI::Carp qw( fatalsToBrowser);

my $str;
my $i;
my $j;

&main;

sub main{
my $q = new CGI();

$str = $q->param( "words" );
$str = decode('euc-jp', $str);

my $title = encode('euc-jp', "MeCab APIを呼出す");
my $h1 = encode('euc-jp', $str . "の品詞");

print $q->header( -type =>"text/html", -charset => "euc-jp" );
print "<meta http-equiv=\"refresh\" content=\"5;URL=./test2.html\">";
print $q->start_html( $title ),
$q->h1( $h1 ),
$q->start_ul;

my $esc = CGI::escape($str);
Encode::from_to($esc, "euc-jp", "utf8" );
my $ua = LWP::UserAgent->new;
$ua->agent('PerlSample/0.1');
$ua->timeout(5);

my $response = $ua->get('http://mimitako.net/api/mecapi.cgi?sentence=' . $esc . '&response=surface,feature');
Encode::from_to($response, "utf8", "euc-jp" );
my @res = split(/<word>/, $response->content);

foreach $i (@res){
$i = decode('utf8', $i);

my @wc = ("名詞", "動詞", "形容詞", "助動詞", "助詞" , "接頭詞" , "接続詞" , "記号" );
my @fn = ("N" , "V" , "A" , "MV" , "JOSHI","SETTOUSHI", "SETSUZOKUSHI", "KIGOU");
for($j=0; $j<@wc; $j++){
if($i =~ /<surface>(.+)<\/surface><feature>$wc[$j]/){
$str = encode('utf8', $1);#utf8
my $filename = encode('euc-jp', $fn[$j]);
open(OUT, ">>./dir/$filename.txt");
Encode::from_to($fn[$j], "utf8", "euc-jp" );
print $fn[$j].$str." ";
$str = encode('euc-jp', $1);
Encode::from_to($str." ", "utf8", "euc-jp" );
print OUT "$str\n";
close(OUT);
last;
}
}
}
}

A 回答 (5件)

> Encode::from_to($response, "utf8", "euc-jp" );



response オブジェクトそのものを文字コード変換するのは
おかしくありませんか。

my $content = $responce->content;
Encode::from_to($content, "utf8", "euc-jp" );
my @res = split(/<word>/, $content);

とかじゃないかと。

この回答への補足

Perlのオブジェクトというのがよく分かりませんでした。
教えていただいたとおりに書き直してみます。

補足日時:2010/11/06 17:05
    • good
    • 0

エラーそのままの意味だとHTTP::Responseがないと言っているみたいですが、そこはどうですか?

この回答への補足

つまり、HTTPリクエストを送って、なにも返答が帰ってきてないということですね。
ということは、ソースの段階できちんとAPIに命令を送れてないということなんでしょうか?

補足日時:2010/11/06 17:03
    • good
    • 0

> つまり、HTTPリクエストを送って、なにも返答が帰ってきてないということですね。



違いますよ。モジュールがちゃんと入っているかという意味です。

content メソッドは HTTP::Response が 継承している HTTP::Message で実装している
ようなので、HTTP::Message が正しく入っていないかもしれません。

use HTTP::Response;

if (HTTP::Response->can('content')) {
print 'HTTP::Response can content', $/;
}
else {
print 'HTTP::Response can not content', $/;
}

if (HTTP::Response->can('content_dummy')) {
print 'HTTP::Response can content_dummy', $/;
}
else {
print 'HTTP::Response can not content_dummy', $/;
}

入っていれば以下のようになります。

HTTP::Response can content
HTTP::Response can not content_dummy

でも、 HTTP レスポンスが帰ってきてない可能性もありますね。レスポンスをチェック
するのもよいでしょう。
if ( $response->is_success ) {
print "HTTP response success\n";
}_
else {
print "HTTP response fail\n";
}

この回答への補足

HTTP response success HTTP::Response can content HTTP::Response can not content_dummy

と表示されました。

どこが問題なのでしょうか。

補足日時:2010/11/07 03:20
    • good
    • 0

ちょっと試したところ再現しました。

一番最初に回答された方が指摘した問題が原因のようです。修正しても直りませんでしたか?

use strict;
use warnings;
use LWP::UserAgent;
use Encode qw/encode decode/;
use CGI;

my $str = "test string";
my $esc = CGI::escape($str);
my $ua = LWP::UserAgent->new;
$ua->agent('PerlSample/0.1');
$ua->timeout(5);

my $response
= $ua->get( 'http://mimitako.net/api/mecapi.cgi?sentence='
. $esc
. '&response=surface,feature' );

# Can't locate object method "content" via package ... と出力し、エラーになる
Encode::from_to( $response, "utf8", "euc-jp" );
my @res = split( /<word>/, $response->content );

# 成功する
# my $content = $response->content;
# Encode::from_to( $content, "utf8", "euc-jp" );
# my @res = split( /<word>/, $content );

この回答への補足

すみません、どうしてもエラーがでます。
以下はローカルのエラーで、サーバ環境では500番で全く動きません。

--- 実行開始 [>perl untitled3.pl]
[Mon Nov 8 05:34:31 2010] untitled3.pl: Use of uninitialized value $str in concatenation (.) or string at untitled3.pl line 24.
[Mon Nov 8 05:34:31 2010] untitled3.pl: Use of uninitialized value $esc in concatenation (.) or string at untitled3.pl line 38.
Content-Type: text/html; charset=utf-8

<meta http-equiv="refresh" content="5;URL=./test2.html"><!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitio …
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>MeCab API繧貞他蜃コ縺・/title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>縺ョ蜩∬ゥ・/h1><ul>
--- 実行終了 (0)

補足日時:2010/11/08 05:39
    • good
    • 0

元の質問にあったエラーと話がそれて来て混乱しているのですが、とりあえずなんか書いてみました。

パスとか修正して下さい。あと空白2文字を全角空白にしてます。

#!/usr/bin/perl -T
use strict;
use warnings;
use utf8;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use CGI::Pretty;
use Encode;
use LWP::UserAgent;

my $q  = CGI->new();
my $text = $q->param("text");

if ( defined $text ) {
  my $utf8  = Encode::is_utf8($text) ? $text : Encode::decode_utf8($text);
  my $escaped = CGI::escape($utf8);
  my $ua   = LWP::UserAgent->new;
  $ua->agent('PerlSample/0.1');
  $ua->timeout(5);

  my $res
    = $ua->get( 'http://mimitako.net/api/mecapi.cgi?sentence='
      . $escaped
      . '&response=surface,feature' );
  print $q->header( -type => 'text/plain', -charset => 'UTF-8' );
  print $res->status_line, "\n";
  print $res->message,   "\n";
  print $res->code,    "\n";
  if ( $res->is_success ) {
    binmode STDOUT, ":encoding(utf8)";
    print $res->content, "\n";
  }
  else {
    print $res->error_as_HTML, "\n";
  }

}
else {
  print $q->header( -charset => 'UTF-8' ),
    $q->start_html( -title => "test", -lang => 'ja', ),
    $q->start_form( -method => 'GET', -action => $q->script_name ),
    $q->textfield( -name => 'text', -default => 'Input text' ),
    $q->submit("Send"),
    $q->end_form,
    $q->end_html;
}

この回答への補足

ありがとうございます。
なんとかなりそうなのですが、XMLページを表示できませんとなってしまいます。
クロームだと表示できるのですが、IEでは表示できません。

補足日時:2010/11/16 05:53
    • good
    • 0

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