This is my code, have redacted the "use" statements. They all are working as desired.
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;
}
The Dumper in got_response prints the value instantly, but after that I have to wait at least 15 seconds for the return statement after POE::Kernel->run to execute. It returns the correct value but I can't wait that long. If I add exit after the sub get_reponse dumper statement, then no value is returned.
Any help and suggestion will be appreciated.
run()
will not return until every session has ended. This includes sessions that were created while run() was running.
Define the constant POE::Kernel::TRACE_REFCNT
before using the first POE module, and you will receive a dump of which resources are in use throughout your program's lifetime.
#!/usr/bin/perl
use strict;
sub POE::Kernel::TRACE_REFCNT () { 1 }
use POE;
# ...
$poe_kernel->run();
exit 0;