这是我的代码,已经编辑了“使用”语句。他们都按要求工作。
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之后的 return 语句。它返回正确的值,但我不能等那么久。如果我在sub get_reponse dumper语句之后添加 exit,则不会返回任何值。
任何帮助和建议将不胜感激。