Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 50 lines (39 sloc) 0.91 kB
5f75559 @rcaputo This is a simpler version version of eg-22-multiple-http-requests.pl.
authored
1 #!/usr/bin/env perl
2 # vim: ts=2 sw=2 noexpandtab
3
4 use warnings;
5 use strict;
6 use lib qw(../lib);
7
8 {
9 package ConcurrentHttpClient;
10 use Moose;
11 extends 'HttpClient';
12
13 has pending => (
14 is => 'rw',
15 isa => 'Int',
16 default => 0,
17 );
18
19 after request => sub {
20 my ($self) = @_;
21 $self->pending( $self->pending() + 1 );
22 };
23
24 after internal_http_response => sub {
25 my ($self) = @_;
26 return if $self->pending( $self->pending() - 1 );
27 $self->emit( -name => "empty" );
28 };
29 }
30
31 ### Main usage.
32
33 use HTTP::Request;
34
35 my $client = ConcurrentHttpClient->new();
36 $client->request( HTTP::Request->new( GET => $_ ) ) foreach (
37 'http://poe.perl.org',
38 'http://duckduckgo.com/',
39 'http://metacpan.org/',
40 'http://perl.org/',
41 'http://twitter.com/',
42 );
43
44 while (my $event = $client->next()) {
45 last if $event->_name() eq 'empty';
46 print $event->response()->code(), " = ", $event->request()->uri(), "\n";
47 }
48
49 exit;
Something went wrong with that request. Please try again.