Based on CB chat, there's some confusion with my solution, so I'll elaborate.

It's my understanding that you want to do something along the lines of

while (<STDIN> or <TNIN>) { if (we_read_from_STDIN()) { print TNOUT do_this($_); } else { print STDOUT do_that($_); } }

Of course, that's not valid Perl. That where IO::Select comes in.

use IO::Select qw( ); my $sel = IO::Select->new(\*STDIN, \*TNIN); MAIN_LOOP: while (my @ready = $sel->can_read()) { for my $fh (@ready) { my $bytes_read = sysread($fh, my $data='', 4096) or last MAIN_LOOP; if ($fh == \*STDIN) { print TNOUT do_this($data); } else { print STDOUT do_that($data); } } }

We can't use <$fh> since that would block when only part of a line has arrived. If you want to do line-based IO, you'll have to do your own line building:

use IO::Select qw( ); my $sel = IO::Select->new(\*STDIN, \*TNIN); my %bufs; MAIN_LOOP: while (my @ready = $sel->can_read()) { for my $fh (@ready) { our $buf; local *buf = \$bufs{$fh}; # Alias my $bytes_read = sysread($fh, $buf, 4096, length($buf)) or last MAIN_LOOP; my $line_end = index($buf, $/); next if $line_end < 0; my $line = substr($buf, 0, $line_end+1, ''); if ($fh == \*STDIN) { print TNOUT do_this($line); } else { print STDOUT do_that($line); } } }

Update:


In reply to Re^2: bidirectional challange by ikegami
in thread bidirectional challange by my_nihilist

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.