How to flush output in backticks In Perl?

て烟熏妆下的殇ゞ 提交于 2020-01-13 09:01:06

问题


If I have this perl app:

print `someshellscript.sh`;

that prints bunch of stuff and takes a long time to complete, how can I print that output in the middle of execution of the shell script?

Looks like Perl will only print the someshellscript.sh result when it completes, is there a way to make output flush in the middle of execution?


回答1:


What you probably want to do is something like this:

open(F, "someshellscript.sh|");
while (<F>) {
    print;
}
close(F);

This runs someshellscript.sh and opens a pipe that reads its output. The while loop reads each line of output generated by the script and prints it. See the open documentation page for more information.




回答2:


The problem here is that escaping with backticks stores your script to a string, which you then print. For this reason, there would be no way to "flush" with print.

Using the system() command should print output continuously, but you won't be able to capture the output:

system "someshellscript.sh";


来源:https://stackoverflow.com/questions/758611/how-to-flush-output-in-backticks-in-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!