Perl 中的行缓冲读取 [英] Line buffered reading in Perl

查看:18
本文介绍了Perl 中的行缓冲读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 perl 脚本,比如在以下上下文中使用的process_output.pl":

I have a perl script, say "process_output.pl" which is used in the following context:

long_running_command |process_output.pl"

long_running_command | "process_output.pl"

process_output 脚本,需要像 unix "tee" 命令一样,它在生成 "long_running_command" 时将输出转储到终端,另外将输出捕获到文本文件,并在 "long_running_command" 的末尾",以文本文件作为输入分叉另一个进程.

The process_output script, needs to be like the unix "tee" command, which dumps output of "long_running_command" to the terminal as it gets generated, and in addition captures output to a text file, and at the end of "long_running_command", forks another process with the text file as an input.

我目前看到的行为是,long_running_command"的输出被转储到终端,只有在它完成时才转储,而不是在生成时转储输出.我需要做一些特别的事情来解决这个问题吗?

The behavior I am currently seeing is that, the output of "long_running_command" gets dumped to the terminal, only when it gets completed instead of, dumping output as it gets generated. Do I need to do something special to fix this?

根据我在其他一些 stackexchange 帖子中的阅读,我在process_output.pl"中尝试了以下内容,但没有太多帮助:

Based on my reading in a few other stackexchange posts, i tried the following in "process_output.pl", without much help:

  1. select(标准输出);$|=1;
  2. select(STDIN);$|=1;# 不确定是否需要
  3. 使用文件句柄;STDOUT->autoflush(1);
  4. stdbuf -oL -eL long_running_command |process_output.pl"

有关如何进一步进行的任何指示.

Any pointers on how to proceed further.

谢谢AB

推荐答案

这更有可能是第一个进程的输出被缓冲的问题,而不是脚本的输入.最简单的解决方案是尝试使用 unbuffer 命令(我相信它是 expect 包的一部分),类似于

This is more likely an issue with the output of the first process being buffered, rather than the input of your script. The easiest solution would be to try using the unbuffer command (I believe it's part of the expect package), something like

unbuffer long_running_command | "process_output.pl"

unbuffer 命令将禁用在输出被定向到非交互位置时正常发生的缓冲.

The unbuffer command will disable the buffering that happens normally when output is directed to a non-interactive place.

这篇关于Perl 中的行缓冲读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆