简体   繁体   English

如何将文件句柄传递给Perl Expect的log_file函数?

[英]How can I pass a filehandle to Perl Expect's log_file function?

I feel stupid for asking this, but I've tried a couple things and I'm not sure where to go with it. 我问这个问题感到很愚蠢,但是我已经尝试了几件事,但不确定如何处理。

From the Expect.pm documentation : 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. 发送到生成的进程或从生成的进程接收的所有字符都被写入文件。

I'd like to pass the $filehandle to log_file. 我想将$ filehandle传递给log_file。 However, when I tried this: 但是,当我尝试这样做时:

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

I get a file named 'LOG' in the directory that I'm running the script out of. 我在运行脚本的目录中得到一个名为“ LOG”的文件。 After some investigation, I tried this: 经过一番调查,我尝试了一下:

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

Now, I get a file named *main::LOG in the directory. 现在,我在目录中得到一个名为*main::LOG的文件。 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 . 我也确实有另一个文件,命名为我在-l选项上指定的名称,但是它仅包含我发送给print LOG

I'm not sure if the filehandling functionality is hosed in the function, or if I'm doing something wrong. 我不确定函数中是否包含文件处理功能,或者我做错了什么。

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. 如果您有一个名为LOG的裸字文件句柄,则可以通过说\\*LOG (可以在perldoc perldata阅读有关此内容的更多信息)将其传递给函数,但不要这样做。 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的三个参数版本:

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

you can use $log anywhere you used LOG in the past. 您可以在过去使用LOG任何地方使用$log

You should also be using the strict and warnings pragmas. 您还应该使用strictwarnings编译指示。

Try using a lexical filehandle (and the three-argument open , and die ) to begin with: 尝试使用词汇文件句柄(以及三个参数opendie )开始:

open my $logfh, ">>", $opt{l} or die "Could not open log file $opt{l}: $!\n";
$sess->log_file( $logfh );

LOG is incredibly generic and could be getting trumped (or doing the trumping) of another filehandle somewhere in your code. LOG是非常通用的,可能在代码中的某个位置被另一个文件句柄(或做得很重要)。 Using a lexical filehandle helps to prevent confusion. 使用词汇文件句柄有助于防止混淆。 And you should always check the return status of open() (or use autodie ) in case you can't actually open the file. 而且,您应该始终检查open()的返回状态(或use autodie ),以防无法实际打开文件。

It might be a better idea to return a filehandle using log_file by passing it the filename instead. 通过向log_file传递文件名来返回一个文件句柄可能是一个更好的主意。


From the Expect documentation : Expect文档中

$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. 发送到生成的进程或从生成的进程接收的所有字符都被写入文件。 Normally appends to the logfile, but you can pass an additional mode of "w" to truncate the file upon open() : 通常会追加到日志文件,但是可以在open()时传递附加模式“ w”截断文件:

$object->log_file("filename", "w");

Returns the logfilehandle . 返回logfilehandle

So you should be able to achieve the same functionality using the following: 因此,您应该能够使用以下方法实现相同的功能:

my $sess = Expect->spawn("telnet $ip");
$sess->log_file($opt{l});               # Or my $fh = $sess->log_file...
                                        # if that filehandle is needed

Now all your session activity will be logged to the file. 现在,您的所有会话活动都将记录到文件中。 Append mode is the default. 默认为追加模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何覆盖Perl的open()函数,但使用相同的文件句柄进行测试? - How can I override Perl's open() function but use the same filehandle for testing? 如何确定Perl中输入文件句柄的缓冲区中是否有数据? - How can I tell if there is data in an input filehandle's buffer in Perl? 如何将Perl系统()的输出重定向到文件句柄? - How can I redirect the output of Perl's system() to a filehandle? 在perl中,我如何打印到在运行时解析其文件句柄的文件? - In perl how can I print to a file whose filehandle is resolved at the runtime? 如何判断Perl中的文件句柄是否为空? - How can I tell if a filehandle is empty in Perl? 在Perl中,如果可能,如何将通用菱形(&lt;&gt;)文件句柄传递给子例程? - In Perl, if possible, how do I pass a generic diamond (<>) filehandle to a subroutine? 如何获得Perl程序输出的文件句柄? - How can get a filehandle for a Perl program's output? 如何使用Perl检测Windows上是否删除了打开文件句柄的文件? - How can I detect if the file for an open filehandle has been deleted on Windows using Perl? Perl的打印功能:打印到最后一个文件句柄? - Perl's print function: print to last filehandle? 如何从Perl中返回文件句柄的方法中读取? - How can I read from a method that returns a filehandle in Perl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM