Perl 期望模块无法理解

Perl expect module not able to understand

我是 Perl 新手。我正在使用 Perl Expect 模块来自动化一个简单的程序,但输出不匹配。 这是我想要自动化的 Perl 代码

#!/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";

这是我的 Expectfile 代码..

#!/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();

这是我得到的输出

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$

下一行又出现了我的名字,输入的年龄根本没有显示。

新更新-->现在当我使用 soft_close 而不是 hard_close 时,我的年龄输入即将到来。但是,我的名字输入还是来了两次。 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$

以下适用于我(Ubuntu 19.04,预期版本 1.35,perl 版本 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();