977c5b6a77612d8e4f2b5c9a9a2d3b7fb9e7b0bc
[scpubgit/Tak.git] / lib / Tak / REPL.pm
1 package Tak::REPL;
2
3 use Term::ReadLine;
4 use Moo;
5
6 has world => (is => 'ro', required => 1);
7
8 has remote => (is => 'lazy');
9
10 sub _build_remote {
11   my ($self) = @_;
12   my $world = $self->world;
13   $world->remote_for('meta')->blocking_request(
14     register => eval => 'Tak::EvalService'
15   );
16   $world->remote_for('eval')
17 }
18
19 sub run {
20   my $remote = $_[0]->remote;
21   my $read = Term::ReadLine->new('REPL');
22
23   while (1) {
24     my $line = $read->readline('re.pl$ ');
25     last unless defined $line;
26     next unless length $line;
27     my @reply = $remote->blocking_request(eval => $line);
28     if ($reply[0] eq 'MISTAKE') {
29       die "Botch: ".join(': ', @reply[1,2]);
30     }
31     my $ret = $reply[1];
32     print $ret->{return};
33     if ($ret->{stdout}) {
34       chomp($ret->{stdout});
35       print "STDOUT:\n${\$ret->{stdout}}\n";
36     }
37     if ($ret->{stderr}) {
38       chomp($ret->{stderr});
39       print "STDERR:\n${\$ret->{stderr}}\n";
40     }
41   }
42 }