0

I am new to Perl. I am using Perl Expect module to automate a simple prog but the output is mismatched. This is my Perl code which I wanted to be automated

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;
use Path::Tiny qw(path);
use IO::Prompter;


my $username= prompt "What is your name";
my $hry= prompt "How are you";
my $age= prompt "How old are you";

and this is my Expectfile code..

#!/usr/bin/perl
use strict;
use warnings;

use Expect;
my $cmd='perl';
my @param=qw(Welcome.pl);
my $exp = Expect->spawn($cmd,@param) or die "Cannot spawn $cmd: $!\n";

$exp->expect (1,"What is your name");
$exp->send("Alen\r");

$exp->expect (1,"Alen\rHow are you");
$exp->send("Fine\r");

$exp->expect (1,"Fine\rHow old are you");
$exp->send("21\r");
$exp->hard_close();

This is the output I'm getting

admin3@admin3-VirtualBox:~/Desktop$ perl WelcomeExpect.pl
 What is your name Alen
 Alen
 How are you Fine
 How old are you admin3@admin3-VirtualBox:~/Desktop$

My name is appearing again on the next line and the age input is not at all showing.

New Update-->Now when I'm using soft_close instead of hard_close, my age input is coming. But still, my name input is coming two times. admin3@admin3-VirtualBox:~/Desktop$ perl WelcomeExpect.pl

What is your name Alen
Alen
How are you Fine
How old are you 21
admin3@admin3-VirtualBox:~/Desktop$
pynexj
  • 19,215
  • 5
  • 38
  • 56
luffy008
  • 37
  • 7

1 Answers1

1

The following works for me (Ubuntu 19.04, Expect version 1.35, perl version 5.28.1):

use strict;
use warnings;
use Expect;

my $cmd='perl';
my @param=qw(Welcome.pl);
my $exp = Expect->new();
$exp->spawn($cmd, @param) or die "Cannot spawn $cmd: $!\n";

$exp->expect (1, "What is your name ");
$exp->send("Alen\n");
$exp->expect (1, "Alen");
$exp->send("Fine\n");
$exp->expect (1, "\nHow are you Fine");
$exp->send("21\n");
$exp->expect (1, "\nHow old are you 21");
$exp->soft_close();
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • Thanks. But still, the output is the same as earlier even after trying your code. – luffy008 Jul 18 '19 at 20:01
  • So it is still echoing twice? What terminal emulator are you using? Maybe you have to turn off echo? – Håkon Hægland Jul 19 '19 at 11:22
  • I'm running it on a Ubuntu VM terminal only. And thanks for your code. After adding a new line character '\n' after ''What is your name " like `$exp->expect (1, "What is your name\n");` it's working fine. But still, I'm not able to understand the flow of the program like why we are sending input(for fine and 21) before expecting a string.Shouldn't we send it like after expecting some expected string . – luffy008 Jul 19 '19 at 13:51
  • *"...why we are sending input(for fine and 21) before expecting a string.."* I am not sure I understand the question, but we need to send input, since the program is waiting for input. If we did not send anything, the program will never end. – Håkon Hægland Jul 19 '19 at 13:56
  • What I was trying to ask is that when my program starts, we first EXPECT the string `What is your name`, then only we SEND it the string `Alen`.Then my main program prompts `How are you` which we didn't EXPECT in our expect code. We first provided the string `Fine`, then we are EXPECTING the string `How are you Fine`(same with `How old are you 21`). Can't we first EXPECT `How are you` then SEND it the string `Fine`.I don't know if my question made any sense or not – luffy008 Jul 19 '19 at 15:17
  • Yes that makes sense, and seems to be a more appropriate approach. So you can try with: `$exp->expect (1, "Alen"); $exp->(1, "How are you");` and then do a `send()`. Please try and see if that works. – Håkon Hægland Jul 19 '19 at 15:33