Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flush output in backticks In Perl?

Tags:

perl

backticks

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?

like image 732
Ville M Avatar asked Apr 17 '09 00:04

Ville M


2 Answers

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.

like image 91
Greg Hewgill Avatar answered Oct 09 '22 05:10

Greg Hewgill


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";
like image 31
Stefan Kendall Avatar answered Oct 09 '22 05:10

Stefan Kendall