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