Listing 4.
 
Lincoln D. Stein

Client-Server Applications
The Perl Journal, Fall 1999
 
 
  0  package Chatbot::Eliza::Server;
  1  use Chatbot::Eliza;
    
  2  @ISA = 'Chatbot::Eliza';
    
  3  sub command_interface {
  4    my ($self,$in,$out) = @_;
  5    die "usage: Chatbot::Eliza::Server->new(\$input_handle,\$output_handle)"
  6        unless $in && $out;
  7    my ($user_input, $previous_user_input, $reply);
      
  8    $self->botprompt($self->name . ":\t");  # Set Eliza's prompt 
  9    $self->userprompt("you:\t");           # Set user's prompt
    
 10    # Print an initial greeting
 11    $out->print ($self->botprompt,
 12                 $self->{initial}->[ int rand scalar @{ $self->{initial} } ],
 13                 "\n");
    
 14    while (1) {
 15      $out->print ($self->userprompt);
 16      $previous_user_input = $user_input;
 17      chomp( $user_input = $in->getline ); 
    
 18      # If the user wants to quit,
 19      # print out a farewell and quit.
 20      if ($self->_testquit($user_input) ) {
 21          $reply = $self->{final}->[ int rand scalar @{ $prompt->{final} } ];
 22          $out->print ($self->botprompt,$reply,"\n");
 23          last;
 24      } 
    
 25      # Invoke the transform method
 26      # to generate a reply.
 27      $reply = $self->transform( $user_input );
    
 28      # Print the actual reply
 29      $out->print ($self->botprompt,$reply,"\n");
 30    }
 31  }
    
 32  1;