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

Hello. I am using Net:Telnet to connect to a remote application. I am able to connect to the application without any problems. The application prompts for an output file path, and if the file already exists, it sends the prompt "The file exists... Do you want to overwrite it?". I would like to conditionally respond to this prompt when it appears. So far I have tried:

$fexists = $t->waitfor(String => '/The file exists\.\.\. Do you want t +o overwrite it?/', errmode => 'return', timeout => 2); if ($fexists == 1) { t->print("Y");

Setting the error mode to return works if the file doesn't exist and it does nothing (expected). If the file does exist, I thought from reading the manual on Net::Telnet that waitfor would return a 1 if successful, but the code does not work. I have tried printing the value of $fexists, and it is null in both cases (if the file exists or not). Any ideas? Thank you

Replies are listed 'Best First'.
Re: Using Conditionals with Net::Telnet
by tilly (Archbishop) on Jan 20, 2011 at 16:17 UTC
    Have you tried Expect? It is used to control external applications, and is good for scripted response flows.

    But based on past experience, I'd strongly suggest modifying the application to have some sort of "batch mode" where it just does what it is told and doesn't try to interact, and then just call that. This will make your life a *lot* easier. And in the long run will be less fragile than trying to script what to do for every unknown prompt.

      Thanks for the reply, tilly. Unfortunately, I have no control over the external application, so I will try using Expect and see where it gets me.