http://qs321.pair.com?node_id=903487

steve has asked for the wisdom of the Perl Monks concerning the following question:

I would like to write a wrapper script for a *nix-based command line utility. Said utility while running is almost constantly printing information, and accepts input regularly regarding this information. What I would like to do is write a script that meets the following criteria:

  1. Opens some sort of connection with the target program and prints to STDOUT the same information that it receives.
  2. Accepts input from STDIN and prints the command directly to the same bidirectional connection.
  3. Can use the information from the program output to determine automated input based on a predefined set of rules (like Expect).

The first two items here amount to straight passthrough as far as I am concerned. This is essentially the same user experience as running the program by itself outside of perl. The last item is essentially an expect script.

A good test case for this (although practically less useful) would be semi-automated control of some process like top.

Pipe Opens is documented as insufficient to meet this set of criteria. Bidirectional Communication with Another Process has some great information, but cautions that working with buffering and pseudo-ttys will ruin my day. Expect is suggested as the best solution for compatible environments.

With that wonderful documentation it seems that the solution lies with Expect as I had presupposed, but I am having some difficulty formulating a mental map of how this works logically. I have used Expect in the past for many things, but only to automate entire workflows, and not in an environment where user input may happen at any time, and the expect-controlled target never ceases to return results (does not wait for input).

$process->expect(undef); # Forever until EOF

Will always emulate the target until EOF, but does not afford any interactivity.

I fully Expect that the interconnect or interact method will be the solution to address these criteria simultaneously, but the documentation for such is rather sparse. Additionally I am finding the search for information on the Expect module to be very difficult due to the case-insensitive nature of most searching algorithms and the ubiquitous usage of the word expect in entirely unrelated topics.

While I am working on this I would like to ask if anyone else has dealt with this, and if they have any advice, other reference points, or sample code I could review.