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

发布于 2024-09-19 16:43:05 字数 850 浏览 4 评论 0原文

我觉得问这个问题很愚蠢,但我已经尝试了几件事,但我不知道该去哪里。

来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美人迟暮 2024-09-26 16:43:06

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

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

您可以在过去使用过 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 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}: $!";

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

You should also be using the strict and warnings pragmas.

じее 2024-09-26 16:43:06

尝试使用词法文件句柄(以及三参数 opendie)作为开头:

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

LOG 非常通用,可能会被超越(或胜过)代码中某处的另一个文件句柄。使用词法文件句柄有助于防止混淆。并且您应该始终检查 open() (或 use autodie)的返回状态,以防您实际上无法打开该文件。

Try using a lexical filehandle (and the three-argument open, and die) to begin with:

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. 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.

秉烛思 2024-09-26 16:43:06

使用 log_file 传递文件名来返回文件句柄可能是一个更好的主意。


来自期望文档

$object->log_file("文件名" | $filehandle | \&coderef | undef)

将会话记录到文件中。所有角色
发送到生成的或从生成的接收的
进程被写入文件。
通常附加到日志文件中,但是
你可以传递一个附加模式“w”
open() 时截断文件:

$object->log_file("文件名", "w");

返回日志文件句柄

因此,您应该能够使用以下方法实现相同的功能:

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

现在您的所有会话活动都将记录到文件中。追加模式是默认模式。

It might be a better idea to return a filehandle using log_file by passing it the filename instead.


From the Expect 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.
Normally appends to the logfile, but
you can pass an additional mode of "w"
to truncate the file upon open():

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

Returns the 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文