1

I have a Perl script

for my $i (1..10) {
    print "How $i\n";
    sleep(1);
}

I want to run it from another perl script and capture its output. I know that I can use qx() or backticks, but they will return all the output simultaneously when the program exits.

But, what I instead want is to print the output of the first script from the second script as soon as they are available ie. for the example code the output of the first script is printed in ten steps from the second script and not in one go.

I looked at this question and wrote my second script as

my $cmd = "perl a.pl";
open my $cmd_fh, "$cmd |";

while(<$cmd_fh>) {
    print "Received: $_\n";
    STDOUT->flush();
}

close $cmd_fh;

However, the output is still being printed simultaneously. I wanted to know a way to get this done ?

1
  • 2
    A simple and specific example of a pseudo-terminal with IPC::Run in this post. There's also an option to use the program unbuffer, if available; see the other answer at the linked page. Commented Jun 13, 2019 at 5:33

1 Answer 1

3

The child sends output in chunks of 4 or KiB or 8 KiB rather than a line at a time. Perl programs, like most programs, flush STDOUT on linefeed, but only when connected to a terminal; they fully buffer the output otherwise. You can add $| = 1; to the child to disable buffering of STDOUT.

If you can't modify the child, such programs can be fooled by using pseudo-ttys instead of pipes. See IPC::Run. (Search its documentation for "pty".)

Sign up to request clarification or add additional context in comments.

3 Comments

Using run \@cmdArr, '>pty>', sub { print "@_" } solved the problem. But, suppose I also want to pass a string to the program I am running as input (the program prompts the user for an input), then how should I modify this statement ?
The command I'm trying to actually run is a ssh command. And the input I am trying to pass is the user password for remote login. I tried this - run \@cmdArr, \$sshpwd, '>pty>', sub { print "@_" } - but this doesn't seem to work, it still asks for the password.
I found my answer here - stackoverflow.com/questions/24454037/…. Instead of using ssh to login into remote system use sshpass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.