perl无法通过包LWP :: UserAgent找到对象方法query_form

时间:2015-03-23 14:58:59

标签: multithreading perl libwww-perl

我正在创建一个带有线程的脚本,因此我必须使用线程支持重建perl(perl5.20)。 由于我重建了perl,我有一个错误:

Can't locate object method "query_form" via package "LWP::UserAgent"

我尝试重新安装LWP :: UserAgent,LWP :: Simple,URI,但它们是最新的(根据cpan)。

错误的代码:

#!/usr/bin/env perl

package get_xml;

use strict;
use warnings;
use Curses;
use LWP::Simple;
use LWP::UserAgent;
use MIME::Base64;
use URI;
use URI::http;
use HTTP::Request::Common;
use parse_xml;

# ...

sub write_conv_thread{
    my ($window, $rows, $username, $url, $ua) = @_;
    while(1){
        $$url->query_form(  # line 43
            "heartbeat" => '0',
            "conv"      => 0,
            "username"  => "$username",
            "active"    => 0
        );
        my $xml = $$ua->get($url);
        my @conv = get_conv($xml);
        print_all_lines($window, $rows, @conv);
        $$window->refresh();
        sleep(5);
    }
}

1;

确切的错误消息:Thread 1 terminated abnormally: Can't locate object method "query_form" via package "LWP::UserAgent" at get_xml.pm line 43.

调用函数的代码:

#!/usr/bin/env perl

use strict;
use warnings;
use Curses;
use LWP::Simple;
use LWP::UserAgent;
use MIME::Base64;
use URI;
use threads;
use get_xml;
use post_xml;

# ... initialization of Curses windows ...
# $chat_win is a curse, $row is a number

my $server_endpoint = "...";
my $ua = LWP::UserAgent->new;
my $url = URI->new( "$server_endpoint/index.php" );
my $thread = threads->new(\&get_xml::write_conv_thread, \$chat_win, $row-4,"...", \$url, \$ua);
$thread->detach();

如何让perl找到对象方法?

感谢您的回答。

1 个答案:

答案 0 :(得分:2)

(参考)UA已分配到$url而不是$ua

我对原因的最佳猜测(因为您没有提供给出错误的实际代码):未提供$window$rows$username,导致(参考)UA是第四个参数。