Created
March 27, 2014 19:09
-
-
Save ohoushyar/9815623 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use feature qw( say ); | |
use AnyEvent; | |
use AnyEvent::Strict; | |
use Twiggy::Server; | |
my $psgi_app = sub { | |
my $env = shift; | |
return sub { | |
my $responder = shift; | |
my $writer = | |
$responder->( [ 200, [ 'Content-Type' => 'text/plain' ] ] ); | |
$writer->write(scalar localtime . "\n"); | |
}; | |
}; | |
# curl http://localhost:5000/ | |
my $server = Twiggy::Server->new( | |
port => 5000, | |
); | |
$server->register_service($psgi_app); | |
my $cv = AE::cv; | |
say "[$$] Starting ..."; | |
# XXX why I can't use this as cb??? | |
my $hi = sub { | |
my $num = shift; | |
say "[$$] Hi $num!"; | |
}; | |
my $w; $w = AnyEvent->timer( | |
after => 0, | |
interval => 2, | |
cb => sub { say "[$$] Hi 2!" }, | |
); | |
my $w5; $w5 = AnyEvent->timer( | |
after => 0, | |
interval => 5, | |
cb => sub { say "[$$] Hi 5!" }, | |
); | |
my $io; $io = AnyEvent->io( | |
fh => \*STDIN, | |
poll => 'r', | |
cb => sub { | |
chomp (my $input = <STDIN>); | |
say "[$$] Read [$input]"; | |
if ($input eq 'q') { | |
say "[$$] Stopping ..."; | |
$cv->send; | |
undef $io; | |
} elsif ($input eq 'dump') { | |
say "[$$] Here is the dump: blahblah"; | |
} else { | |
# nothing | |
} | |
}, | |
); | |
$cv->recv; | |
say "[$$] Done"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment