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);
27 use Socket
qw(AF_UNIX);
30 =head3 new(argument => value...)
38 Hashref of arguments for L<IO::Socket> configuration. Use C<Domain> to
39 select a socket class.
43 Debug level (refer to L<Net::REPL::Base>).
51 my $class = ref($proto) || $proto;
59 my $client = $self->_create_socket(
61 'Proto' => 'SOCK_STREAM',
62 %{$self->{'socket_args'}},
65 die("Can't connect client: $!\n");
67 $self->debug("Client PID $$ connected");
69 $self->{'fh'} = $client;
80 $self->{'fh'}->close();
86 Run one iteration of the REPL. Returns true if the socket remains open
87 for further iterations.
89 Blocks until a line of input can be read from the terminal, sent to
90 the server, and the result received and printed.
97 my $input = $self->read();
98 if (not defined($input)) {
101 elsif ($input eq '') {
106 if ($input =~ m{^/(.*)$}) {
107 # $input is a local command
108 my ($cmd, $input) = split(/\s+/, $1);
109 if ($cmd eq 'local_eval') {
110 my @output = $self->formatted_eval($input);
113 elsif ($cmd eq 'exit') {
118 # $input is for remote eval
119 $self->lv_send($input);
120 $output = $self->lv_receive();
121 if (not defined($output)) {
122 # Server has gone away?
127 $self->print($output, "\n");
133 Create the input source for L<read>() and output for L<print>().
140 $self->{'term'} ||= Term
::ReadLine
->new($self->{'prompt'});
141 $self->{'out_fh'} = $self->{'term'}->OUT();
146 Read and return one line of user input.
153 my $prompt = $self->{'prompt'} . '> ';
154 return $self->{'term'}->readline($prompt);
159 Output the result string(s) from one L<interact>() iteration.
164 my ($self, @output) = @_;
166 for my $s (@output) {
167 print { $self->{'out_fh'} } $s;