#!/usr/bin/perl use 5.14.1; use EV; use Coro; use Coro::Handle; use AnyEvent::Socket; use Guard; my $channel = Coro::Channel->new(); sub handle_server { my $fh = shift; # handle input. async_pool { while (defined $fh and defined (my $line = $fh->readline())) { chomp $line; given($line) { when('a') { $channel->put("message A"); # this will print second $fh->print("you said 'a'\n"); # this will print first } when ('b') { $fh->print("you said 'b'\n"); } when ('exit') { $fh->print("Bye!\n"); $channel->shutdown(); $fh = undef; last; } default { $fh->print("What does '$line' mean?"); } } } }; while (my $msg = $channel->get()) { last unless $fh->print("$msg\n"); } } unlink '/tmp/c.sock'; # just in case AnyEvent::Socket::tcp_server 'unix/', '/tmp/c.sock', sub { my $fh = shift; $fh = unblock $fh; async_pool { handle_server($fh); }; return; }; # just some data... putting in the data even when there's no listeners? # not good - but good enough for a demo. my $f = do { my $i = 0; EV::periodic 0, 1, 0, sub { $channel->put(++$i); } }; EV::loop;