如何将文件句柄传递给 Perl Expect 的 log_file 函数?
我觉得问这个问题很愚蠢,但我已经尝试了几件事,但我不知道该去哪里。
来自 Expect.pm 文档:
$object->log_file("文件名" | $filehandle | \&coderef | undef)
将会话记录到文件中。所有发送至或接收自的字符 生成的进程被写入文件。
我想将 $filehandle 传递给 log_file。但是,当我尝试这样做时:
open (LOG, ">>" .$opt{l});
my $sess = Expect->spawn("telnet $ip");
$sess->log_file(LOG)
我在运行脚本的目录中得到一个名为“LOG”的文件。经过一番调查,我尝试了以下操作:
open (LOG, ">>" .$opt{l});
my $sess = Expect->spawn("telnet $ip");
my $fh = *LOG;
$sess->log_file($fh)
现在,我在目录中得到一个名为 *main::LOG 的文件。我还有另一个文件,其名称为我在 -l
选项中指定的名称,但它只包含我发送到 print LOG
的行。
我不确定文件处理功能是否包含在该函数中,或者我是否做错了什么。
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:
$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. 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. 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. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有一个名为
LOG
的裸字文件句柄,则可以通过说\*LOG
将其传递给函数(您可以在perldoc perldata
),但不要这样做。裸字文件句柄是一种非常古老的样式,不应再使用。尝试使用词法文件句柄和 open 的三个参数版本:您可以在过去使用过
LOG
的任何地方使用$log
。您还应该使用
strict
和警告
编译指示。If you have a bareword filehandle named
LOG
, you can pass it to a function by saying\*LOG
(you can read more about this inperldoc 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:you can use
$log
anywhere you usedLOG
in the past.You should also be using the
strict
andwarnings
pragmas.尝试使用词法文件句柄(以及三参数
open
和die
)作为开头:LOG
非常通用,可能会被超越(或胜过)代码中某处的另一个文件句柄。使用词法文件句柄有助于防止混淆。并且您应该始终检查open()
(或use autodie
)的返回状态,以防您实际上无法打开该文件。Try using a lexical filehandle (and the three-argument
open
, anddie
) to begin with:LOG
is incredibly generic and could be getting trumped (or doing the trumping) of another filehandle somewhere in your code. Using a lexical filehandle helps to prevent confusion. And you should always check the return status ofopen()
(oruse autodie
) in case you can't actually open the file.使用
log_file
传递文件名来返回文件句柄可能是一个更好的主意。来自期望文档:
因此,您应该能够使用以下方法实现相同的功能:
现在您的所有会话活动都将记录到文件中。追加模式是默认模式。
It might be a better idea to return a filehandle using
log_file
by passing it the filename instead.From the Expect documentation:
So you should be able to achieve the same functionality using the following:
Now all your session activity will be logged to the file. Append mode is the default.