Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: bidirectional challange

by ikegami (Patriarch)
on Mar 13, 2008 at 22:26 UTC ( [id://674087]=note: print w/replies, xml ) Need Help??


in reply to bidirectional challange

You want to read from STDIN and from TNIN without starving either, so that points to IO::Select.

Replies are listed 'Best First'.
Re^2: bidirectional challange
by ikegami (Patriarch) on Mar 14, 2008 at 04:30 UTC

    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:

    • I'm not sure about the $fh == \*STDIN part. fileno($fh) == fileno(\*STDIN) should definitely work.
    • Note: %bufs may still contain data when last MAIN_LOOP; is executed.
    • Bonus: Since can_read accepts a timeout argument, you can probably do away with your alarm!
    • Won't work on Windows.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://674087]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-23 15:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found