1 package Net
::REPL
::Client
;
9 Net::REPL::Client - ReadLine + Socket to Eval Server = interactive development
13 The client object connects L<Term::ReadLine> to a socket that speaks
14 the L<Net::REPL::Server> protocol. Perl code is sent over the socket
15 to be eval'd in the server process, which sends back a result for the
18 Like the Server, this class can be used as-is or superclassed to
19 provide additional capabilities.
25 use base
qw(Net::REPL::Base);
30 =head3 new(argument => value...)
38 Hashref of arguments for L<IO::Socket::INET>.
42 Debug level (refer to L<Net::REPL::Base>).
50 my $class = ref($proto) || $proto;
58 my $client = IO
::Socket
::INET
->new(
60 %{$self->{'socket_args'}},
63 die("Can't connect client: $!\n");
65 $self->debug("Client PID $$ connected");
67 $self->{'fh'} = $client;
78 $self->{'fh'}->close();
84 Run one iteration of the REPL. Returns true if the socket remains open
85 for further iterations.
87 Blocks until a line of input can be read from the terminal, sent to
88 the server, and the result received and printed.
95 my $input = $self->read();
96 if (not defined($input)) {
99 elsif ($input eq '') {
104 if ($input =~ m{^/(.*)$}) {
105 # $input is a local command
106 my ($cmd, $input) = split(/\s+/, $1);
107 if ($cmd eq 'local_eval') {
108 my @output = $self->formatted_eval($input);
111 elsif ($cmd eq 'exit') {
116 # $input is for remote eval
117 $self->lv_send($input);
118 $output = $self->lv_receive();
119 if (not defined($output)) {
120 # Server has gone away?
125 $self->print($output, "\n");
131 Create the input source for L<read>() and output for L<print>().
138 $self->{'term'} ||= Term
::ReadLine
->new($self->{'prompt'});
139 $self->{'out_fh'} = $self->{'term'}->OUT();
144 Read and return one line of user input.
151 my $prompt = $self->{'prompt'} . '> ';
152 return $self->{'term'}->readline($prompt);
157 Output the result string(s) from one L<interact>() iteration.
162 my ($self, @output) = @_;
164 for my $s (@output) {
165 print { $self->{'out_fh'} } $s;