Here is a little Perl server script. 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.

use strict; use warnings; $|++; my $counter = 1; print "($counter) "; while (<STDIN>) { 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) "; }

Now, here is an Expect.pm client script to interact with the server script by typing in various lines.

use strict; use warnings; use Expect; $|++; my $exp = new Expect; $exp->raw_pty(1); $exp->log_file("/tmp/expect.out"); $exp->log_stdout(1); my @commands = ( "This is the first command", "Here is the second command", "error", "commit", "This is the last command", "exit", ); $exp->spawn("./expecttest_server.pl"); foreach my $command (@commands) { print "$command\n"; $exp->send("$command\n"); $exp->expect(1, '-re','\(\d+\)'); } $exp->soft_close();

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.

It should look like this:

(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...

But the STDOUT display that comes from running the client script looks like this:

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...

and the file specified by $exp->log_file (tmp/expect.out) shows this:

(1) (2) (3) Error on command #3
(4) Committing data
(5) (6) Exiting program...

I've tried experimenting by logging various combinations of the command itself + the before_match and after_match 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.

So, what's the best practice for capturing the entirety of an Expect.pm session?

Thanks to anyone who can help!


In reply to How can I show all input and output with Expect.pm by GeorgeAdams

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.