ensure, better scripting infrastructure
[scpubgit/Tak.git] / lib / Tak / STDIOSetup.pm
CommitLineData
77bf1d9b 1package Tak::STDIOSetup;
2
e99bc141 3BEGIN { require MRO::Compat; }
4
77bf1d9b 5use Tak::ConnectionService;
6use Tak::Router;
7use Tak;
8use strictures 1;
9
10sub run {
11 open my $stdin, '<&', \*STDIN;
12 open my $stdout, '>&', \*STDOUT;
2791fd73 13 # if we don't re-open them then 0 and 1 get re-used - which is not
14 # only potentially bloody confusing but results in warnings like:
15 # "Filehandle STDOUT reopened as STDIN only for input"
16 close STDIN; open STDIN, '<', '/dev/null';
17 close STDOUT; open STDOUT, '>', '/dev/null';
986f5290 18 my $done;
77bf1d9b 19 my $connection = Tak::ConnectionService->new(
20 read_fh => $stdin, write_fh => $stdout,
986f5290 21 listening_service => Tak::Router->new,
22 on_close => sub { $done = 1 }
77bf1d9b 23 );
2791fd73 24 $connection->receiver->service->register_weak(remote => $connection);
25 if ($0 eq '-') {
26 $0 = 'tak-stdio-node';
27 }
986f5290 28 Tak->loop_until($done);
77bf1d9b 29}
30
311;