implement repl in takc
[scpubgit/Tak.git] / takc
CommitLineData
36cf3bcb 1use strictures 1;
36cf3bcb 2use Tak::JSONChannel;
3use Tak::Router;
4use IPC::Open2;
5use Tak::Remote;
34a159d9 6use Term::ReadLine;
36cf3bcb 7
8my $pid = open2(my $out, my $in, $^X, qw(-Ilib takd))
9 or die "Couldn't open2 child: $!";
10
11my $channel = Tak::JSONChannel->new(
12 read_fh => $out,
13 write_fh => $in
14);
15
16my $router = Tak::Router->new(
17 channel => $channel,
18);
19
20my $remote = Tak::Remote->new(
21 router => $router,
22 name => 'EVAL'
23);
24
34a159d9 25my $read = Term::ReadLine->new('REPL');
26
27while (1) {
28 my $line = $read->readline('re.pl$ ');
29 exit unless defined $line;
30 next unless length $line;
31 my $reply = [ $remote->blocking_request(eval => $line) ];
32 if ($reply->[0] eq 'MISTAKE') {
33 die "Botch: ".join(': ', @{$reply}[1,2]);
34 }
35 my $ret = $reply->[1];
36 print $ret->{return};
37 if ($ret->{stdout}) {
38 chomp($ret->{stdout});
39 print "STDOUT:\n${\$ret->{stdout}}\n";
40 }
41 if ($ret->{stderr}) {
42 chomp($ret->{stderr});
43 print "STDERR:\n${\$ret->{stderr}}\n";
44 }
45}