评估perl中的期望

时间:2010-07-16 12:39:39

标签: perl expect

我在perl中使用Expect连接远程机器并执行某些功能。示例代码就像

$outfile="ls -lrt";
$outfile1="output";

$exp->expect(30,-re,".*bash-.*" => sub{$exp->send("$outfile2 >$outfile \r")});
$exp->expect(60,-re,".*bash-.*" => sub{$exp->send("$shayam > $Ram \r")});

即使第一个表达式失败,它也会等待60秒并执行第二个语句。我只是想检查一下,如果只有第一个语句通过它就应该继续。

1 个答案:

答案 0 :(得分:4)

我猜您正在使用the Expect.pm module documented here。如上所述:

  

如果在数组上下文中调用expect()   将返回   ($ matched_pa​​ttern_position,$ error,   $ successfully_matching_string,   $ before_match和$ after_match)。

因此,您可能希望在数组上下文中调用它,以便在正则表达式失败和发送失败时都可以收到错误。

my ($matched_pattern_position, $error,
  $successfully_matching_string,
  $before_match, $after_match) =
  $exp->expect(30
  , -re,".*bash-.*" =>
    sub{$exp->send("$outfile2 >$outfile \r")}
);

$exp->expect(60
  ,-re,".*bash-.*" =>
    sub{$exp->send("$shayam > $Ram \r")}
) if !defined $error;