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

971 Views Asked by At

I'm creating a script with thread so I had to rebuild perl (perl5.20) with threads support. Since I have rebuild perl, I have an error :

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

I've tried to re-install LWP::UserAgent, LWP::Simple, URI, but they are up-to-date(according to cpan).

The faulty code :

#!/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;

And the exact error message : Thread 1 terminated abnormally: Can't locate object method "query_form" via package "LWP::UserAgent" at get_xml.pm line 43.

Code that call the function :

#!/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();

What can I do to make perl find the object method ?

Thank you for your answer.

1

There are 1 best solutions below

3
On

The (reference to the) UA got assigned to $url instead of $ua.

My best guess as to the cause (since you didn't provide the actual code that gives the error): $window, $rows or $username wasn't provided, causing the (reference to the) UA to be the fourth argument.