我是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$
答案 0 :(得分:0)
以下对我有用(Ubuntu 19.04,Expect版本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();