Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help in handling user interaction in perl

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
0
0
US
I worte a automatin tool in perl which basically executes a program through calling system() with different arguments passed in. This tool will parse the outputs and compile a report.

Under certain circumstances, when a specific argument passed in, this program would post a question like "do you want to proceeds [default Y]?" and wait for a user input. This is by design so there is nothing wrong. But I don't know how to handle this situation in my perl code.

I'd guess fork() might be able to handle this. But my knowledge on fork() is very limilted. Could someone here kindly offer me some help and better yet, with some sample codes?

Many thanks.
 
I know I probably can use Expect.pm to do the job. However, my tool is normally run on a newly installed server where only core perl modules are available.
 
Why would you think fork() is needed when you are waiting for user input? Is the problem you don't know how to get the user input?

Code:
my $input = <STDIN>;
chomp $input;
if ($input eq 'Y'){
   #proceed
}
else {
   #stop
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you Kevin. And sorry I did not make myself clear.

Sure I know how to get the user input. But that's not my intention.

Let me restate my problem.

My tool is running through a cron job. When the program invoked by my tool is waiting for a user input, idealy, my tool should quit. Unfortunately, my tool is hanging there now, 'cause I don't know how to test if it is waiting for a user input w/o using expect.pm. That's why I was thinking maybe I should start a child process to watch it. I am not sure if I am on the right track. And even if I am, I still don't know how to use fork() properly.

Thanks again for your help.

 
whn said:
Unfortunately, my tool is hanging there now
I'm sorry to hear that [rofl]


But on a more serious note, I guess your tool wants to read this from STDIN, and the rest of its input comes from somewhere else. Can you pipe the response to the tool when you invoke it, so that if it tries to read from STDIN, it will get the response?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Many programs have command line options that allows you to set options, like skip confirmation from the user, that is the first place I would check. Or look into the Expect module.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks, Kevin.

The program I was working on does not have that option. And expect.pm is not my option either, 'cause it is no a core module.

However, I did manage to have this solved by using fork().

When the parent process invoked the program, another child process is watching it. If the programm is asking for user input, the child process kills the parent process. This is OK in my case. My tool is designed only to check whether the program is going to ask for user interaction or not. If it does, the test passed. So the parent process is killed. It is a bold action I guess. But that's all I can think about now.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top