Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to handle Expect::Simple sessions to programs that end normally.

by DrWhy (Chaplain)
on Apr 21, 2010 at 00:18 UTC ( [id://835917]=perlquestion: print w/replies, xml ) Need Help??

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

After several years of being interested in the capabilities of Expect I finally have an occasion to use it (and learn how it works). I'm using Expect::Simple to write test code for a shell-script-based installer that asks the user several questions before installation happens. I've got it working okay in a test environment, but when the install script finishes its work it exits (as it should) causing the Expect::Simple object to get very confused. I can't figure out how with Expect::Simple to catch this normal exit and respond appropriately. As it stands now, when the installer exits, my Expect::Simple script dies with the error:
  Expect::Simple: couldn't find prompt after send
If I catch this die and look at the $simplexpect->expect_error() value, it looks something like this:
  3:Child PID 8093 exited with status 0
Now, I could catch the die and parse the expect_error() message to look for a type 3 error with /status 0$/ in it, but surely there is a 'Simple'r way to catch the successful conclusion of the command that is being controlled by Expect::Simple.

Does anyone know of a better way to do this?

Thanks,

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

  • Comment on How to handle Expect::Simple sessions to programs that end normally.

Replies are listed 'Best First'.
Re: How to handle Expect::Simple sessions to programs that end normally.
by djerius (Beadle) on Apr 21, 2010 at 16:12 UTC
    Expect::Simple was designed to work with programs that return a prompt after performing every action, so it really wants to see a prompt after you send a command to the program.

    I can imagine adding EOF as an acceptable "prompt", but there'd have to be a way of specifying it on the fly, as EOF is typically only acceptable at the end of your communications (else you'd never catch aborted programs).

    If you come up with a reasonable approach I'll add it to the module. As it stands, I think you've hit upon the only reasonable workaround.

      It's somewhat gratifying to know that after all the time I spent looking through documentation and the code for Expect and Expect::Simple that there isn't actually a solution -- the alternative being that I spent all that time and still missed something obvious!

      The fugly hack I outlined in the orig. post has been implemented and it's working fine.

      If I were working on Expect::Simple myself (which I don't have time to do!), I might just wrap that particular hack/solution up in a new method named final_send() or send_and_end() or some such. It would have the same behavior as send(), but instead of looking for a prompt it actually would expect to get a type 3 error with zero exit status, and would throw exceptions if it doesn't find that state. So you would not get that "couldn't find a prompt" error, but instead something like "controlled program didn't end as expected.". You could include information in the error text as to whether it found one of the expected prompts instead or another kind of error was raised by Expect. The advantage of this approach is that the controller also controls when the script is expected to end; if the controlled program ends 'normally' at a point you didn't expect, that still causes an exception to be raised.

      You might also want to modify the DESTROY block so that it doesn't try to end the program if it's already terminated itself.

      NB: Edited for clarity.

      --DrWhy

      "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://835917]
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2025-03-27 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (70 votes). Check out past polls.

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.