0

im stucked with the following:

I try to pipe a "$ua->get($url)" in a forked process to the parent process, like this:

use LWP::UserAgent;
use IO::Handle;

pipe my $ua_reader, my $ua_writer;
my $url = "http://some-sample-html-file";

my $ua = LWP::UserAgent->new;
$ua->timeout(1);

my $ua_process = fork();
if( $ua_process == 0 ) {
    close $ua_reader;
    print $ua_writer $ua->get($url);
    exit 0;
}

do {
    $ua_process = waitpid(-1, 0);
} while ($ua_process >= 0);
close $ua_writer;
my $response = $ua_reader->getline;
close $ua_reader;

print $response->decoded_content;

I get this error:

Can't locate object method "decoded_content" via package "HTTP::Response=HASH(0x19045f0)" (perhaps you forgot to load "HTTP::Response=HASH(0x19045f0)"?) at ./uafork.pl line 26.

the $ua object definitively has a "decoded_content" method, but its not callable after it gets through the pipe (other methods, such as "is_success" also dont work...).

Whats a possible solution here? Any help would be very appreciated.

4

1 に答える 1

2

最初は、HTTP::Response をストリームに直接書き込むことはできません。エラーは、「HTTP::Response...」と書かれた文字列がメソッドを持っていないことを意味しますdecoded_content。decoded_content から取得した文字列を記述し、文字列として読み取る必要があります。

use LWP::UserAgent;
use IO::Handle;
use Encode;

pipe my $ua_reader, my $ua_writer;
my $url = "http://some-sample-html-file";

my $ua = LWP::UserAgent->new;
$ua->timeout(1);

my $ua_process = fork();
if( $ua_process == 0 ) {
    close $ua_reader;
    print $ua_writer encode_utf8 $ua->get($url)->decoded_content;
    exit 0;
}

do {
    $ua_process = waitpid(-1, 1);
} while ($ua_process != 0);
close $ua_writer;
my $response = $ua_reader->getline;
close $ua_reader;

print $response;
于 2013-06-28T09:02:11.773 に答える