【发布时间】:2014-07-11 14:28:54
【问题描述】:
这是我的代码,已经编辑了“使用”语句。他们都按预期工作。
my $json_data;
sub request {
my ( $method, $container, $params, $content_type) = @_;
#get the complete URL, works fine
my $full_url = get_url( $method, $container, $params, $content_type);
POE::Component::Client::HTTP->spawn(Alias => 'ua');
# Start a session that will drive the resolver.
# Callbacks are named functions in the "main" package.
POE::Session->create(
inline_states => {
_start => \&_start,
got_response => \&got_response,
}
);
POE::Kernel->run();
return $json_data;
}
sub _start {
my $kernel = $_[KERNEL];
$kernel->post("ua" => "request", "got_response", $full_url);
}
sub got_response {
my ($response_packet) = $_[ARG1];
my $http_response = $response_packet->[0];
my $response_string = $http_response->decoded_content();
$json_data = decode_json( $response_string);
print Dumper $json_data;
}
got_response 中的 Dumper 会立即打印该值,但之后我必须等待至少 15 秒才能在 POE::Kernel->run 之后等待返回语句执行。它返回正确的值,但我不能等那么久。 如果我在 sub get_reponse dumper 语句之后添加 exit,则不会返回任何值。
任何帮助和建议将不胜感激。
【问题讨论】:
标签: perl httpresponse poe