create world object, repl script
[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 { shift->world->remote_for('EVAL') }
11
12 sub 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 }