Hi, I'm trying to flush two streams (STDOUT, STDERR) so that their content are printed directly as the program uses these streams. However, my results are incorrect. I have 2 applications, where one is the core and the second is the executed.

UPDATE: it seems that my problem is when the process creating is used in a library so that both the Core application is using it and the 'second application' is using it.

Core:

use Library; $| = 1; Library::createProcess('second application');
Application:
use Library; select((select(STDOUT), $|=1)[0]); print STDOUT "Out \n"; print STDERR " -> Err \n"; print STDOUT "Out \n"; print STDERR " -> Err \n"; print STDOUT "Out \n"; print STDERR " -> Err \n"; Library::createProcess('random application');
Library:
use IO::Select; use IPC::Open3; sub createProcess($path) { my $pid = open3(*CMD_IN, *CMD_OUT, *CMD_ERR, $path); my $selector = IO::Select->new(); $selector->add(*CMD_ERR, *CMD_OUT); _parseStreams($selector); close(CMD_IN) or die "Unable to close STDIN: $!"; close(CMD_OUT) or die "Unable to close STDOUT: $!"; close(CMD_ERR) or die "Unable to close STDERR: $!"; waitpid($pid, 0) or die "Waitpid failed: $!"; } sub _parseStreams ($) { my($selector) = @_; my(@ready, $fh, $line); while(@ready = $selector->can_read()) { foreach $fh (@ready) { if(fileno($fh) == fileno(CMD_ERR)) { $line = scalar <CMD_ERR>; print STDERR $line; } else { $line = scalar <CMD_OUT>; print STDOUT $line; } $selector->remove($fh) if eof($fh); } } }
OUTPUT BECOMES:
Out Out Out -> Err -> Err -> Err

In reply to [UPDATED] How to autoflush streams (i.e. open3 calls) by rapide

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.