Created
May 15, 2014 06:30
-
-
Save shalk/8e6d5c30244b21a95814 to your computer and use it in GitHub Desktop.
perl 使用expect 模块, 问答都放在hash中,类似的修改问答和 命令就行了; perl use expect module ,all Question /Answer in Hash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use 5.010; | |
use strict; | |
use Expect; | |
my $cmd = "perl simulate.pl"; | |
my $timeout = 30; | |
my @question_list = qw/name age home/; | |
my $qa = { | |
"name" => "shalk\n", | |
"age" => "17\n", | |
"home" => "China\n", | |
}; | |
my $exp = Expect->spawn($cmd) or die "can not spawn $cmd"; | |
for my $ask (@question_list){ | |
my $answer = $qa->{$ask}; | |
$exp->expect($timeout, | |
[ qr/$ask/ => sub { my $exp = shift, $exp->send($answer) }, ], | |
); | |
} | |
$exp->soft_close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment