[英]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. 您还应该使用
strict
和warnings
编译指示。
Try using a lexical filehandle (and the three-argument open
, and die
) to begin with: 尝试使用词汇文件句柄(以及三个参数
open
和die
)开始:
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.