Perl 打印缓冲刷新

发布于 2024-07-29 08:31:55 字数 795 浏览 3 评论 0原文

我有以下 Perl 代码:

STDOUT->autoflush(1);

foreach(...)
{
    ...

    foreach(...)
    {
        print("Processing $folder");
        $|=1;
        process($folder);
    }
    ...
}

但 print 语句仅在循环的第一次迭代中起作用,之后不会打印任何内容。 知道为什么吗?

编辑:我找到了原因并将其添加到答案中。 解决方案是:

我在里面添加了以下行 循环并且有效:

选择标准输出;

我认为process()函数中的代码 应该是修改默认的 输出缓冲区。 这是一段代码写的 别人写的!

我不确定这是否是一个问题 Perl 允许这样做或 没有改回来的开发者 为默认值。

最终的代码如下所示:

<前><代码>foreach(...) { ... foreach(...) { 选择标准输出; print("正在处理$文件夹"); $|=1; 进程($文件夹); } ... }

谢谢大家...

I have the following Perl code:

STDOUT->autoflush(1);

foreach(...)
{
    ...

    foreach(...)
    {
        print("Processing $folder");
        $|=1;
        process($folder);
    }
    ...
}

but the print statement works only in the first iteration of the loop and does not print anything after that. Any idea why?

EDIT: I found the reason and have added it in the answer also.
The solution was:

I added the following line inside the
loop and it worked:

select STDOUT;

I think the code in process() function
should have been modifying the default
output buffer. It was a code written
by somebody else!

I am not sure if this is a problem
with Perl which allows this or the
developer who did not change it back
to the default.

The final code looked like this:

foreach(...)
{
    ...

    foreach(...)
    {
        select STDOUT;

        print("Processing $folder");
        $|=1;
        process($folder);
    }
    ...
}

Thanks all...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

南街女流氓 2024-08-05 08:31:55

很好的侦探工作,追踪这个问题!

我想建议一个替代解决方案。

您可以使用 IO::Handle 到 STDOUT 的接口:

use IO::Handle;

foreach(...)
{
    ...

    foreach(...)
    {
        STDOUT->printflush("Processing $folder");

        process($folder);
    }
    ...
}

Good detective work in tracking down this problem!

I'd like to suggest an alternate solution.

Rather than having select() wars with the author of process(), you could use the IO::Handle interface to STDOUT:

use IO::Handle;

foreach(...)
{
    ...

    foreach(...)
    {
        STDOUT->printflush("Processing $folder");

        process($folder);
    }
    ...
}
倥絔 2024-08-05 08:31:55

我在循环中添加了以下行并且它起作用了:

select STDOUT;

我认为 process() 函数中的代码应该修改默认输出缓冲区。 这是别人写的代码!

我不确定这是否是 Perl 的问题,它允许这样做,还是开发人员没有将其更改回默认值。

最终代码如下所示:

foreach(...)
{
    ...

    foreach(...)
    {
        select STDOUT;

        print("Processing $folder");
        $|=1;
        process($folder);
    }
    ...
}

谢谢大家......

I added the following line inside the loop and it worked:

select STDOUT;

I think the code in process() function should have been modifying the default output buffer. It was a code written by somebody else!

I am not sure if this is a problem with Perl which allows this or the developer who did not change it back to the default.

The final code looked like this:

foreach(...)
{
    ...

    foreach(...)
    {
        select STDOUT;

        print("Processing $folder");
        $|=1;
        process($folder);
    }
    ...
}

Thanks all...

巨坚强 2024-08-05 08:31:55

我的代码如下所示:

#!/usr/bin/perl -w

use strict;
use warnings;
use sigtrap qw/handler signal_handler normal-signals/;
use feature qw(say);

my $datetime;
$datetime = localtime ();

say "tester started $datetime";

while ( 1 ) {
    select STDOUT;
    $datetime = localtime ();
    say "tester output every second $datetime";
    $|=1;
    sleep ( 1 );
}

sub signal_handler {
    die "\nCaught a signal $!\n";
}

My code looks like this:

#!/usr/bin/perl -w

use strict;
use warnings;
use sigtrap qw/handler signal_handler normal-signals/;
use feature qw(say);

my $datetime;
$datetime = localtime ();

say "tester started $datetime";

while ( 1 ) {
    select STDOUT;
    $datetime = localtime ();
    say "tester output every second $datetime";
    $|=1;
    sleep ( 1 );
}

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