title: How can I show all input and output with Expect.pm (Perl)?
body: <p>Here is a little Perl server.  It displays (1), accepts a line of input, then displays (2), etc.  If you type "error" or "commit", it gives a custom message.  If you type "exit", it quits.  Otherwise, it just endlessly takes lines of input.</p>
  
  <pre><code>use strict;
  use warnings;
  $|++;
  
  my $counter = 1;
  print "($counter) ";
  
  while (&lt;STDIN&gt;) {
    chomp;
    if ($_ eq "error")  {print "Error on command #$counter\n";}
    if ($_ eq "commit") {print "Committing data\n";}
    if ($_ eq "exit")   {print "Exiting program...\n"; exit;}
  
    $counter++;
    print "($counter) ";
  }
  </code></pre>
  
  <p>Now, here is an Expect.pm client script to interact with the server script by typing in various lines.</p>
  
  <pre><code>use strict;
  use warnings;
  use Expect;
  $|++;
  
  my $exp = new Expect;
  $exp-&gt;raw_pty(1);
  $exp-&gt;log_file("/tmp/expect.out");
  $exp-&gt;log_stdout(1);
  
  my @commands = (
    "This is the first command",
    "Here is the second command",
    "error",
    "commit",
    "This is the last command",
    "exit",
  );
  
  
  $exp-&gt;spawn("./expecttest_server.pl");
  foreach my $command (@commands) {
    print "$command\n";
    $exp-&gt;send("$command\n");
    $exp-&gt;expect(1, '-re','\(\d+\)');
  }
  $exp-&gt;soft_close();
  </code></pre>
  
  <p>What I want is to be able to store the entire session from start to finish, including everything the server script generated, and everything the Expect.pm script sent.</p>
  
  <p>That is, I <strong>want</strong> my client script to be able to return output like this, which is what you would see if you ran and interacted with the server script manually:</p>
  
  <pre>
  (1) This is the first command
  (2) Here is the second command
  (3) error
  Error on command #3
  (4) commit
  Committing data
  (5) This is the last command
  (6) exit
  Exiting program...
  </pre>
  
  <p>But the STDOUT display that comes from running the client script looks like this:</p>
  
  <pre>
  This is the first command
  (1) (2) Here is the second command
  error
  (3) Error on command #3
  (4) commit
  This is the last command
  Committing data
  (5) (6) exit
  Exiting program...
  </pre>
  
  <p>and the file specified by $exp->log_file (tmp/expect.out) shows this:</p>
  
  <pre>
  (1) (2) (3) Error on command #3
  (4) Committing data
  (5) (6) Exiting program...
  </pre>
  
  <p>I've tried experimenting by logging various combinations of the command itself + the <strong>before_match</strong> and <strong>after_match</strong> variables returned by $exp->expect().  But so far I haven't gotten the right combination.  And it seems like an awfully clunky way to get what I'm looking for.</p>
  
  <p>So, what's the best practice for capturing the entirety of an Expect.pm session?</p>
  
  <p>Thanks to anyone who can help!</p>