1 |
#!/opt/perl/bin/perl |
2 |
use IO::Socket::INET; |
3 |
use AnyEvent::Util; |
4 |
use AnyEvent::Handle; |
5 |
|
6 |
my $cv = AnyEvent->condvar; |
7 |
|
8 |
my $hdl; |
9 |
|
10 |
# The $watchobj is just a guard that you have to keep |
11 |
# referenced until you are done with the connect. |
12 |
my $watchobj = AnyEvent::Util::tcp_connect ('www.google.com', 80, sub { |
13 |
my ($sock) = @_; |
14 |
|
15 |
$hdl = |
16 |
AnyEvent::Handle->new ( |
17 |
fh => $sock, |
18 |
on_eof => sub { print "received eof\n"; undef $hdl } |
19 |
); |
20 |
|
21 |
$hdl->push_write ("GET / HTTP/1.0\015\012\015\012"); |
22 |
|
23 |
$hdl->push_read_line (sub { |
24 |
my ($hdl, $line) = @_; |
25 |
print "Yay, got line: $line\n"; |
26 |
$cv->broadcast; |
27 |
}); |
28 |
|
29 |
}, sub { |
30 |
warn "Got error on connect: $!\n"; |
31 |
$cv->broadcast; |
32 |
}, 10); |
33 |
|
34 |
$cv->wait; |