Perl 打印缓冲刷新
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很好的侦探工作,追踪这个问题!
我想建议一个替代解决方案。
您可以使用 IO::Handle 到 STDOUT 的接口:
Good detective work in tracking down this problem!
I'd like to suggest an alternate solution.
Rather than having
select()
wars with the author ofprocess()
, you could use the IO::Handle interface to STDOUT:我在循环中添加了以下行并且它起作用了:
我认为 process() 函数中的代码应该修改默认输出缓冲区。 这是别人写的代码!
我不确定这是否是 Perl 的问题,它允许这样做,还是开发人员没有将其更改回默认值。
最终代码如下所示:
谢谢大家......
I added the following line inside the loop and it worked:
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:
Thanks all...
我的代码如下所示:
My code looks like this: