Skip to content

Commit

Permalink
simplify non-blocking Mojo::UserAgent recipe some more
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 8, 2012
1 parent af0d38b commit 249d8ba
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -938,25 +938,23 @@ can keep many parallel connections active at the same time.
sub crawl {

# Dequeue or wait 2 seconds for more URLs
return Mojo::IOLoop->timer(2 => sub { crawl() })
unless my $url = shift @urls;
return Mojo::IOLoop->timer(2 => \&crawl) unless my $url = shift @urls;

# Fetch non-blocking just by adding a callback
$ua->get($url => sub {
my ($ua, $tx) = @_;

# Extract URLs
say $url;
$tx->res->dom('a[href]')->each(sub {
my $e = shift;
for my $e ($tx->res->dom('a[href]')->each) {

# Build absolute URL
my $url = Mojo::URL->new($e->{href})->to_abs($tx->req->url);
say " -> $url";

# Enqueue
push @urls, $url;
});
};

# Next
crawl();
Expand Down

0 comments on commit 249d8ba

Please sign in to comment.