So I've been having a difficult time with my foray into event driven programming. Most of it due to still thinking sequentially but I'm having a hard time understanding how anyone synchronizes their code when using LWP::Protocol::AnyEvent::http and am looking for some help in understanding. Here is the smallest program I can create that demonstrates my basic lack of understanding:
use strict;
use warnings;
use 5.10.0;
use LWP::Protocol::AnyEvent::http;
use WWW::Mechanize;
use Coro qw(async);
my $url = "http://feedproxy.google.com/~r/PerlNews/~3/kqUb_rpU5dE/";
my $mech = WWW::Mechanize->new;
$mech->get($url);
my @cs;
foreach my $link ($mech->links) {
my $c = async {
say "Getting " . $link->url;
my $ua = WWW::Mechanize->new;
$ua->get($link->url);
};
push(@cs, $c);
}
$_->join for (@cs);
How do I make sure the ->get has succeeded before going into the foreach
loop? the ->get
will return immediately since it doesn't block when using the LWP::Protocol::AnyEvent::http
module. So there are no ->links
and the program just exits. Removing LWP::Protocol::AnyEvent::http
obviously makes the program return links, like a regular sequential program, and slow like one too.
Thanks for any insight.