如何将文件句柄传递给Perl Expect的log_file函数? [英] How can I pass a filehandle to Perl Expect's log_file function?

查看:159
本文介绍了如何将文件句柄传递给Perl Expect的log_file函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问这个问题我很愚蠢,但我尝试了几件事,但不确定该如何处理.

I feel stupid for asking this, but I've tried a couple things and I'm not sure where to go with it.

摘录自 Expect.pm文档:

$object->log_file("filename" | $filehandle | \&coderef | undef)

将会话记录到文件.发送或接收的所有字符 产生的进程将被写入文件.

Log session to a file. All characters send to or received from the spawned process are written to the file.

我想将$ filehandle传递给log_file.但是,当我尝试这样做时:

I'd like to pass the $filehandle to log_file. However, when I tried this:

open (LOG, ">>" .$opt{l});
my $sess = Expect->spawn("telnet $ip");
$sess->log_file(LOG)

我在运行脚本的目录中得到一个名为"LOG"的文件.经过一番调查,我尝试了这一点:

I get a file named 'LOG' in the directory that I'm running the script out of. After some investigation, I tried this:

open (LOG, ">>" .$opt{l});
my $sess = Expect->spawn("telnet $ip");
my $fh = *LOG;
$sess->log_file($fh)

现在,我在目录中得到一个名为*main::LOG的文件.我也确实有另一个文件,命名为我在-l选项上指定的名称,但是它只包含我发送到print LOG的行.

Now, I get a file named *main::LOG in the directory. I do have another file as well, named whatever I specified on the -l option, but it only contains the lines that I send to print LOG.

我不确定函数中是否包含文件处理功能,或者我做错了什么.

I'm not sure if the filehandling functionality is hosed in the function, or if I'm doing something wrong.

推荐答案

如果您有一个名为LOG的裸字文件句柄,则可以通过说出\*LOG将其传递给函数(您可以在 perldoc perldata ),但不要那样做.密码文件句柄是一种非常古老的样式,不应再使用.尝试使用词汇文件句柄和open的三个参数版本:

If you have a bareword filehandle named LOG, you can pass it to a function by saying \*LOG (you can read more about this in perldoc perldata), but don't do that. Bareword filehandles are a very old style and should no longer be used. Try using a lexical filehandle and the three argument version of open:

open my $log, ">>", $opt{l}
    or die "could not open $opt{l}: $!";

您可以在过去使用LOG的任何地方使用$log.

you can use $log anywhere you used LOG in the past.

您还应该使用 strict

You should also be using the strict and warnings pragmas.

这篇关于如何将文件句柄传递给Perl Expect的log_file函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆