in reply to Get live output of exec

I would create a set of pipes, using IO::Pipe between the parent, and the child, and have the child send immediately to the parent with autoflush turned on.

Here is a simple example( not using IO::Pipe ).

#!/usr/bin/perl use warnings; use strict; use IO::Handle; $|++; pipe( PARENT_READER, CHILD_WRITER ); pipe( CHILD_READER, PARENT_WRITER ); PARENT_WRITER->autoflush(1); CHILD_WRITER->autoflush(1); my $pid; if ( $pid = fork() ) { close(CHILD_READER); close(CHILD_WRITER); my $buffer; print PARENT_WRITER 1; while (1) { sysread( PARENT_READER, $buffer, 100 ); print "parent revd: $buffer, and reply with ", $buffer + 1, "\ +n"; sleep(1); print PARENT_WRITER $buffer + 1; } } else { close(PARENT_READER); close(PARENT_WRITER); my $buffer; while (1) { sysread( CHILD_READER, $buffer, 1000 ); print "child revd: $buffer, and reply with ", $buffer + 1, "\n +"; sleep(1); print CHILD_WRITER $buffer + 1; } }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Get live output of exec
by LanX (Saint) on Mar 04, 2014 at 13:35 UTC
    > Here is a simple example( not using IO::Pipe ).

    You are confusing me... :)

    Are there special reasons for recommending this module except its OO interface?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Never used it, but the docs certainly look like it makes clean-up a whole lot easier. Cute misdirection so you don't have to remember the argument order on the constructor. Looks like it's been in CORE since 5.12.


      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.