我在模拟一段涉及perl中的Expect::expect函数的代码时被卡住了
它是这样的:
my $result = 0;
my $exp = new Expect;
$exp->raw_pty(1);
$exp->log_stdout(0);
$exp->spawn("$some_command");
$exp->expect(undef,
[ qr/some text/i,
sub { my $self = shift;
$self->send("$a_param\n");
exp_continue;
} ],
[ qr/more text/,
sub { my $self = shift;
$self->send("$another_param");
sleep 5;
exp_continue;
} ],
[ qr/some more text/ ,
sub { my $self = shift;
$ld_info = "$something";
$self->clear_accum();
$self->send($ld_info);
sleep 5;
exp_continue;
} ],
[ qr/further more text/,
sub { my $self = shift;
$result = 1;
} ],
);
return $result;}$result的值是最终的返回值。在模拟时,我打算将result的值设置为1,而不实际运行命令,然后尝试匹配模式。有没有办法做到这一点?
发布于 2014-11-14 00:47:02
您可以通过使用
local *实现将代码替换为您自己的代码。这实际上取决于函数是如何调用的,但我认为你可以这样做:
local *Expect::expect = sub
{
return 1;
}https://stackoverflow.com/questions/26882329
复制相似问题