From 1fabe20fd9ddee533a8adb49da2a1feac3104aee Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 19 Feb 2010 22:49:07 +0000 Subject: [PATCH] Net::REPL::Client: Allow read/print portions to be overridden separately. --- perl/lib/Net/REPL/Client.pm | 51 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/perl/lib/Net/REPL/Client.pm b/perl/lib/Net/REPL/Client.pm index 596b5f2..ce1031a 100644 --- a/perl/lib/Net/REPL/Client.pm +++ b/perl/lib/Net/REPL/Client.pm @@ -66,7 +66,7 @@ sub new { $self->{'fh'} = $client; - $self->{'term'} ||= Term::ReadLine->new($self->{'prompt'}); + $self->setup_io(); return $self; } @@ -92,8 +92,7 @@ the server, and the result received and printed. sub interact { my ($self) = @_; - my $prompt = $self->{'prompt'} . '> '; - my $input = $self->{'term'}->readline($prompt); + my $input = $self->read(); if (not defined($input)) { return 0; } @@ -123,10 +122,50 @@ sub interact { } } - my $out_fh = $self->{'term'}->OUT(); - print $out_fh $output; - print $out_fh "\n"; + $self->print($output, "\n"); return 1; } +=head3 setup_io() + +Create the input source for L() and output for L(). + +=cut + +sub setup_io { + my ($self) = @_; + + $self->{'term'} ||= Term::ReadLine->new($self->{'prompt'}); + $self->{'out_fh'} = $self->{'term'}->OUT(); +} + +=head3 read() + +Read and return one line of user input. + +=cut + +sub read { + my ($self) = @_; + + my $prompt = $self->{'prompt'} . '> '; + return $self->{'term'}->readline($prompt); +} + +=head3 print() + +Output the result string(s) from one L() iteration. + +=cut + +sub print { + my ($self, @output) = @_; + + for my $s (@output) { + print { $self->{'out_fh'} } $s; + } +} + + + 1; -- 2.11.4.GIT