note
ikegami
<p>Based on CB chat, there's some confusion with my solution, so I'll elaborate.
<p>It's my understanding that you want to do something along the lines of
<c>
while (<STDIN> or <TNIN>) {
if (we_read_from_STDIN()) {
print TNOUT do_this($_);
} else {
print STDOUT do_that($_);
}
}
</c>
<p>Of course, that's not valid Perl. That where [mod://IO::Select] comes in.
<c>
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);
}
}
}
</c>
<p>We can't use <c><$fh></c> 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:
<c>
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);
}
}
}
</c>
<p><b>Update</b>:
<ul>
<li>I'm not sure about the <c>$fh == \*STDIN</c> part. <c>fileno($fh) == fileno(\*STDIN)</c> should definitely work.
<li>Note: <c>%bufs</c> may still contain data when <c>last MAIN_LOOP;</c> is executed.
<li>Bonus: Since <c>can_read</c> accepts a timeout argument, you can probably do away with your alarm!
<li>Won't work on Windows.
</ul>
674081
674087