#!/opt/perl/bin/perl use IO::Socket::INET; use AnyEvent::Util; use AnyEvent::Handle; my $cv = AnyEvent->condvar; my $hdl; # The $watchobj is just a guard that you have to keep # referenced until you are done with the connect. my $watchobj = AnyEvent::Util::tcp_connect ('www.google.com', 80, sub { my ($sock) = @_; $hdl = AnyEvent::Handle->new ( fh => $sock, on_eof => sub { print "received eof\n"; undef $hdl } ); $hdl->push_write ("GET / HTTP/1.0\015\012\015\012"); $hdl->push_read_line (sub { my ($hdl, $line) = @_; print "Yay, got line: $line\n"; $cv->broadcast; }); }, sub { warn "Got error on connect: $!\n"; $cv->broadcast; }, 10); $cv->wait;