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